numpy数组:基本问题

时间:2016-04-28 07:17:49

标签: python arrays numpy

抱歉,如果这个问题是如此基本

  

A = np.arange(64).reshape(2,32)

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
        49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]])
  

A.reshape(4,4,4)

array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]],
       [[16, 17, 18, 19],
        [20, 21, 22, 23],
        [24, 25, 26, 27],
        [28, 29, 30, 31]],

       [[32, 33, 34, 35],
        [36, 37, 38, 39],
        [40, 41, 42, 43],
        [44, 45, 46, 47]],

       [[48, 49, 50, 51],
        [52, 53, 54, 55],
        [56, 57, 58, 59],
        [60, 61, 62, 63]]])

现在,我会喜欢像A [2]或A [2,:]或A [2,:,]这样的东西来回报矩阵

[[32, 33, 34, 35],
 [36, 37, 38, 39],
 [40, 41, 42, 43],
 [44, 45, 46, 47]]

A[2,2,2]返回42例如

但是我收到了这个错误

IndexError: too many indices for array

2 个答案:

答案 0 :(得分:2)

你必须做

A = A.reshape(4,4,4)

而不是

A.reshape(4,4,4)

因为重塑不在原地,所以你需要这样做。然后就可以了

A[2,2,2]
Out[301]: 42

答案 1 :(得分:0)

在{{1}}之后,A不会改变