TypeError:'tuple'对象不能解释为整数

srk*_*kdb 4 python random tuples

我有x0这是一个float64(64,64)数组.每当我尝试这个:

    delta = np.random.randn(x0.shape)
Run Code Online (Sandbox Code Playgroud)

它给出了标题错误.这是如此基本,以至于我把头缠在它周围.我错过了什么?谢谢

完整的追溯如下:

Traceback (most recent call last):

  File "<ipython-input-31-dcd2365ed519>", line 1, in <module>
    delta = np.random.randn(x0.shape)

  File "mtrand.pyx", line 1420, in mtrand.RandomState.randn

  File "mtrand.pyx", line 1550, in mtrand.RandomState.standard_normal

  File "mtrand.pyx", line 167, in mtrand.cont0_array

TypeError: 'tuple' object cannot be interpreted as an integer
Run Code Online (Sandbox Code Playgroud)

R B*_*ian 7

np.random.randn()在表单中需要整数参数randn(64,64).你np.random.randn()在形式上给出了论据randn((64,64)),这是不期望的.相反,如果要构建64x64随机数组,则需要单独传递行数和列数,而不是作为元组传递.


nik*_*ste 6

它看起来像np.random.randn()一个整数而不是一个元组:https: //docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html trynp.random.randn(x0.shape[0], x0.shape[1])

  • 或者`np.random.randn(*x0.shape)` (2认同)