简体   繁体   English

使用Numpy.Reshape重塑数组

[英]Reshaping arrays using Numpy.Reshape

I am trying to reshape an array using numpy.reshape but always come across the index error 我正在尝试使用numpy.reshape重塑数组,但总是遇到索引错误

"IndexError: index 15484 is out of bounds for axis 0 with size 7231"

I then printed out the shape of the array which was 然后我打印出数组的形状

(7231,80,60,4)

My code is 我的代码是

X = np.array([i[0] for i in train]).reshape(-1,80,60,1)

(im trying to reshape all of my image to (-1,80,60,1)) (我试图将我的所有图像重塑为(-1,80,60,1))

I thought -1 autocompleted the dimensions, so i am confused as to why I am getting this error? 我以为-1自动完成了尺寸,因此我对为什么收到此错误感到困惑?

train is: 火车是:

    train = train_data[:-500]

and train_data is an array with tuples of image pixels and labels 和train_data是一个数组,其中包含图像像素和标签的元组

Can someone help me? 有人能帮我吗?

Be careful when reshaping. 重塑时要小心。 Even if it works, the arrangement of elements may not be what you want. 即使可行,元素的排列也可能不是您想要的。

Start with a simple array that we can visualize: 从一个简单的数组开始,我们可以将其可视化:

In [805]: x = np.arange(24).reshape(3,2,4)
In [806]: x
Out[806]: 
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]]])

reshape to (-1,2,1) - but lets drop the last 1 for a more compact display: 重塑为(-1,2,1)-但放下最后一个为更紧凑的显示:

In [807]: x.reshape(-1,2)
Out[807]: 
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]])

Notice how the original [0,1,2,3] line gets split into 2 lines. 注意原始的[0,1,2,3]行如何被分成两行。

Another way of redistributing the last dimension of size 4 is: 重新分配大小4的最后一个维度的另一种方法是:

In [808]: np.vstack([x[...,i] for i in range(4)])
Out[808]: 
array([[ 0,  4],
       [ 8, 12],
       [16, 20],
       [ 1,  5],
       [ 9, 13],
       [17, 21],
       [ 2,  6],
       [10, 14],
       [18, 22],
       [ 3,  7],
       [11, 15],
       [19, 23]])

That may be clearer if we used np.stack and got (4,3,2) shape 如果我们使用np.stack并得到(4,3,2)形状,那可能会更清楚

array([[[ 0,  4],
        [ 8, 12],
        [16, 20]],
  ....

x.transpose(2,0,1) produces the same thing. x.transpose(2,0,1)产生相同的结果。

reshape preserves the ravelled/flattened order of elements. reshape保留元素的散乱/展平顺序。 Transpose changes it. 转置更改它。

In [812]: x.transpose(2,0,1).ravel()
Out[812]: 
array([ 0,  4,  8, 12, 16, 20,  1,  5,  9, 13, 17, 21,  2,  6, 10, 14,...])
In [813]: x.reshape(-2,2).ravel()
Out[813]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, ...])

The code you have doesn't do what you think it does. 您拥有的代码并没有实现您认为的那样。 i[0] gets the 0th element in the first axis, which is your 80 , that's not what you want. i[0]在第一个轴上获得第0个元素,即您的80 ,这不是您想要的。

Anyway, what you really what is just to select the first slice in the fastest direction, so just do: 无论如何,您真正要选择的是沿最快方向选择第一个切片,所以请执行以下操作:

X = train[:,:,:,0:1]

If your data is not actually the size you say it is, then try: 如果您的数据实际上不是您所说的大小,请尝试:

X = np.array([i.reshape(80, 60, 4)[:,:,0:1] for i in train])

Reshaping an array of shape (7231,80,60,4) -> (-1,80,60,1) does "just work": 重塑形状数组(7231,80,60,4) -> (-1,80,60,1)可以“正常工作”:

train = np.arange(np.prod((7231,80,60,4))).reshape(7231,80,60,4)
print(train.shape)

X = train.reshape(-1,80,60,1)
print(X.shape)

Output: 输出:

(7231, 80, 60, 4)
(28924, 80, 60, 1)

So the issues you're having must not directly stem from the reshape that you're trying to do. 因此,您遇到的问题一定不能直接源于您尝试进行的重塑。 My guess is that your problem might relate to the form/contents of your train_data array (or the array you try to create from it with np.array([i[0] for i in train]) ). 我的猜测是您的问题可能与train_data数组(或您尝试使用np.array([i[0] for i in train])从中创建的数组)的形式/内容有关。 Of course, the problem could also be in a section of your code that you didn't post in your question. 当然,问题也可能出在您未在问题中发布的代码部分中。 It would probably be helpful if you posted a bit more of your actual code. 如果您发布了更多的实际代码,可能会有所帮助。

In particular, when you got the error message: 特别是,当您收到错误消息时:

IndexError: index 15484 is out of bounds for axis 0 with size 7231

it should have included a stack trace that pointed directly back to the problematic line in your code. 它应该包括直接指向代码中有问题的行的堆栈跟踪。 Did the stack trace say that the error was raised from the line in which you create X : 堆栈跟踪是否表明错误是从创建X的行引发的:

X = np.array([i[0] for i in train]).reshape(-1,80,60,1)

or did it point to a different line in your code? 还是指向代码中的另一行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM