载入numpy后的数据类型问题
在学习matplotlib时候我试图调用np.random.normal生成一组随机点阵用于绘图,在调用时传入了int类型的参数x,但是当我将x*2传入normal函数时报错,具体情况如下:def test_random(number):M = np.random.normal(0, 1, 2 * number)
M.reshape((2, number))
print(M)
return M对应的调用为:
M = test_random(int(input('scale:\n')))此时报错:
Traceback (most recent call last):
File "F:/pytest/matplotlib_1.py", line 61, in <module>
main()
File "F:/pytest/matplotlib_1.py", line 53, in main
Z = test_random(M)
File "F:/pytest/matplotlib_1.py", line 41, in test_random
M = np.random.normal(0, 1, 2 * number)
File "mtrand.pyx", line 1497, in numpy.random.mtrand.RandomState.normal
File "_common.pyx", line 577, in numpy.random._common.cont
TypeError: 'numpy.float64' object cannot be interpreted as an integer我又试图将2*number强制转化为int:
def test_random(number):
double_number=int(2*number)
M = np.random.normal(0, 1, double_number)
M.reshape((2, number))
print(M)
return M此时报错:
File "F:/pytest/matplotlib_1.py", line 54, in main
Z = test_random(M)
File "F:/pytest/matplotlib_1.py", line 41, in test_random
double_number=int(2*number)
TypeError: only size-1 arrays can be converted to Python scalars
请问要如何才能将2*number正确的传入normal函数中呢
看不懂你的意思,按你的代码,输入14,运行成功了啊,你要输入什么数?
import numpy as np
def test_random(number):
M = np.random.normal(0, 1, 2 * number)
M.reshape((2, number))
print(M)
return M
M = test_random(int(input('scale:\n')))
print(M)
scale:
14
-0.009342555457168914
[-0.00934256 -0.561340320.938719410.7672819-0.06824285 -0.05190735
1.748271890.7951845 0.405503050.46697624 -0.599519131.39413893
0.82887424 -1.870819020.821800170.46254866 -0.21679419 -0.14561513
0.748973890.320060240.823068991.027081880.312475720.17292185
2.016926530.7196225-0.36370474 -0.21091174]
Process finished with exit code 0 疾风怪盗 发表于 2020-9-18 16:47
看不懂你的意思,按你的代码,输入14,运行成功了啊,你要输入什么数?
迷惑。。我自己用python3.7.0跑会报错emm等下我把整块都贴上来看一下orz Pliosauroidea 发表于 2020-9-18 17:10
迷惑。。我自己用python3.7.0跑会报错emm等下我把整块都贴上来看一下orz
啊绝了,原来是我调用时候不小心把返回值重新调用了一次,导致报错emmmm
页:
[1]