【发布时间】:2023-01-02 00:29:40
【问题描述】:
我正在尝试通过 random.normal 生成随机数并获取它们的摘要。然后,我尝试将值分配给数组sum 中的每个元素。
我通过 np.zeros 创建了一个零数组(浮点型),然后按以下方式分配值。
我试图利用 numpy 和 matlibplot.pyplot 作为库来执行此操作。
我的代码:
np.random.seed(0)
sum=np.zeros(10,dtype=float)
for i in np.arange(1,11):
X = np.random.normal(size=(10,1))
Y=np.sum(X,axis=1)
sum[i-1]=Y
print(sum)
当我在 Google Colab 上执行此代码时,发生了以下错误。
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-14-33fba8ac5d90> in <module>
6 X = np.random.normal(size=(10,1))
7 Y=np.sum(X,axis=1)
----> 8 sum[i-1]=Y
9 print(sum)
ValueError: setting an array element with a sequence.
你能告诉我如何解决这个错误吗?
【问题讨论】:
标签: python numpy-random