鱼C论坛

 找回密码
 立即注册
查看: 2189|回复: 3

[已解决]载入numpy后的数据类型问题

[复制链接]
发表于 2020-9-18 16:40:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在学习matplotlib时候我试图调用np.random.normal生成一组随机点阵用于绘图,在调用时传入了int类型的参数x,但是当我将x*2传入normal函数时报错,具体情况如下:
  1. def test_random(number):
  2.     M = np.random.normal(0, 1, 2 * number)
  3.     M.reshape((2, number))
  4.     print(M[0])
  5.     return M
复制代码
对应的调用为:
  1. M = test_random(int(input('scale:\n')))
复制代码
此时报错:

  1. Traceback (most recent call last):
  2.   File "F:/pytest/matplotlib_1.py", line 61, in <module>
  3.     main()
  4.   File "F:/pytest/matplotlib_1.py", line 53, in main
  5.     Z = test_random(M)
  6.   File "F:/pytest/matplotlib_1.py", line 41, in test_random
  7.     M = np.random.normal(0, 1, 2 * number)
  8.   File "mtrand.pyx", line 1497, in numpy.random.mtrand.RandomState.normal
  9.   File "_common.pyx", line 577, in numpy.random._common.cont
  10. TypeError: 'numpy.float64' object cannot be interpreted as an integer
复制代码
我又试图将2*number强制转化为int:
  1. def test_random(number):
  2.     double_number=int(2*number)
  3.     M = np.random.normal(0, 1, double_number)
  4.     M.reshape((2, number))
  5.     print(M[0])
  6.     return M
复制代码
此时报错:
  1.   File "F:/pytest/matplotlib_1.py", line 54, in main
  2.     Z = test_random(M)
  3.   File "F:/pytest/matplotlib_1.py", line 41, in test_random
  4.     double_number=int(2*number)
  5. TypeError: only size-1 arrays can be converted to Python scalars
复制代码

请问要如何才能将2*number正确的传入normal函数中呢
最佳答案
2020-9-18 16:47:53
看不懂你的意思,按你的代码,输入14,运行成功了啊,你要输入什么数?
  1. import numpy as np

  2. def test_random(number):
  3.     M = np.random.normal(0, 1, 2 * number)
  4.     M.reshape((2, number))
  5.     print(M[0])
  6.     return M


  7. M = test_random(int(input('scale:\n')))
  8. 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
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-18 16:47:53 | 显示全部楼层    本楼为最佳答案   
看不懂你的意思,按你的代码,输入14,运行成功了啊,你要输入什么数?
  1. import numpy as np

  2. def test_random(number):
  3.     M = np.random.normal(0, 1, 2 * number)
  4.     M.reshape((2, number))
  5.     print(M[0])
  6.     return M


  7. M = test_random(int(input('scale:\n')))
  8. 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
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-18 17:10:42 | 显示全部楼层
疾风怪盗 发表于 2020-9-18 16:47
看不懂你的意思,按你的代码,输入14,运行成功了啊,你要输入什么数?

迷惑。。我自己用python3.7.0跑会报错emm等下我把整块都贴上来看一下orz
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-18 17:14:21 | 显示全部楼层
Pliosauroidea 发表于 2020-9-18 17:10
迷惑。。我自己用python3.7.0跑会报错emm等下我把整块都贴上来看一下orz

啊绝了,原来是我调用时候不小心把返回值重新调用了一次,导致报错emmmm
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 22:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表