马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在学习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[0])
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[0])
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[0])
return M
M = test_random(int(input('scale:\n')))
print(M)
scale:
14
-0.009342555457168914
[-0.00934256 -0.56134032 0.93871941 0.7672819 -0.06824285 -0.05190735
1.74827189 0.7951845 0.40550305 0.46697624 -0.59951913 1.39413893
0.82887424 -1.87081902 0.82180017 0.46254866 -0.21679419 -0.14561513
0.74897389 0.32006024 0.82306899 1.02708188 0.31247572 0.17292185
2.01692653 0.7196225 -0.36370474 -0.21091174]
Process finished with exit code 0
|