Python 无法重塑 numpy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20253163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:01:36  来源:igfitidea点击:

Can't reshape numpy array

pythonnumpymultidimensional-arrayreshape

提问by Nick Gilbert

I have a function that is supposed to take a 1D array of integers and shapes it into a 2D array of 1x3 arrays. It then is supposed to take each 1x3 array and shift it into a 3x1 array. The result is supposed to be a 2D array of 3x1 arrays. Here is my function

我有一个函数,它应该采用一维整数数组并将其整形为一个 1x3 数组的二维数组。然后它应该获取每个 1x3 数组并将其移动到一个 3x1 数组中。结果应该是 3x1 数组的二维数组。这是我的功能

def RGBtoLMS(rgbValues, rgbLength): #Method to convert from RGB to LMS
    print rgbValues
    lmsValues = rgbValues.reshape(-1, 3)
    print lmsValues
    for i in xrange(len(lmsValues)):
        lmsValues[i] = lmsValues[i].reshape(3, 1)

    return lmsValues

The issue rises when I try to change the 1x3 arrays to 3x1 arrays. I get the following output assuming rgbValues = [14, 25, 19, 24, 25, 28, 58, 87, 43]

当我尝试将 1x3 数组更改为 3x1 数组时,问题就出现了。我得到以下输出假设 rgbValues = [14, 25, 19, 24, 25, 28, 58, 87, 43]

[14 25 19 ..., 58 87 43]
[[14 25 19]
 [24, 25, 28]
 [58 87 43]]

ValueError [on line lmsValues[i] = lmsValues[i].reshape(3, 1)]: could not broadcast input array from shape (3,1) into shape (3)

How can I avoid this error?

我怎样才能避免这个错误?

采纳答案by askewchan

As mentioned in the comments, you are really always just modifying one array with different shapes. It doesn't really make sense in numpy to say that you have a 2d array of 1 x 3arrays. What that really is is actually a n x 3array.

正如评论中提到的,您实际上总是只是修改一个具有不同形状的数组。在 numpy 中说你有一个二维数组是没有意义的1 x 3。这实际上是一个n x 3数组。

We start with a 1d array of length 3*n(I've added three numbers to your example to make the difference between a 3 x nand n x 3array clear):

我们从一个长度为一维的数组开始3*n(我在您的示例中添加了三个数字以明确a3 x nn x 3数组之间的区别):

>>> import numpy as np

>>> rgbValues = np.array([14, 25, 19, 24, 25, 28, 58, 87, 43, 1, 2, 3])
>>> rgbValues.shape
(12,)

And reshape it to be n x 3:

并将其重塑为n x 3

>>> lmsValues = rgbValues.reshape(-1, 3)
>>> lmsValues
array([[14, 25, 19],
       [24, 25, 28],
       [58, 87, 43],
       [ 1,  2,  3]])
>>> lmsValues.shape
(4, 3)

If you want each element to be shaped 3 x 1, maybe you just want to transpose the array. This switches rows and columns, so the shape is 3 x n

如果您希望对每个元素进行 shape 3 x 1,也许您只想转置数组。这会切换行和列,所以形状是3 x n

>>> lmsValues.T
array([[14, 24, 58,  1],
       [25, 25, 87,  2],
       [19, 28, 43,  3]])

>>> lmsValues.T.shape
(3, 4)

>>> lmsValues.T[0]
array([14, 24, 58,  1])

>>> lmsValues.T[0].shape
(4,)

If you truly want each elementin lmsValuesto be a 1 x 3array, you can do that, but then it has to be a 3d array with shape n x 1 x 3:

如果你真的想每个元素lmsValues是一个1 x 3数组,你可以这样做,但后来它必须是一个三维阵列形状n x 1 x 3

>>> lmsValues = rgbValues.reshape(-1, 1, 3)
>>> lmsValues
array([[[14, 25, 19]],

       [[24, 25, 28]],

       [[58, 87, 43]],

       [[ 1,  2,  3]]])

>>> lmsValues.shape
(4, 1, 3)

>>> lmsValues[0]
array([[14, 25, 19]])

>>> lmsValues[0].shape
(1, 3)