简体   繁体   English

为什么使用 numpy.reshape 的数组未正确存储?

[英]Why is the array using numpy.reshape not being stored properly?

Let's consider an n-by-m array of values that I want to reshape into n*m-by-1 array for storage and plotting.让我们考虑一个 n×m 值数组,我想将其重塑为n*m-by-1数组以进行存储和绘图。 The n-by-m array is the result of some analyses that takes place in a for loop that gets written over each iteration. n×m 数组是在每次迭代中写入的 for 循环中进行的一些分析的结果。

Each time I construct the n*m-by-1 array, I want to store the stacked columns into a separate variable and then increment the column number so I eventually end up with an array that is n*m-by-k where k is how many separate Excel files I'm working with.每次我构造n*m-by-1数组时,我想将堆叠的列存储到一个单独的变量中,然后增加列号,所以我最终得到一个n*m-by-k的数组,其中 k我正在使用多少个单独的 Excel 文件。

The code below will replicate the error and illustrate the point.下面的代码将复制错误并说明这一点。

import numpy as np

number = [2,4]

new_a = np.zeros((3,6))
vec_for_plot = np.zeros((3,6))

a = np.random.random((3,6))

i = 0

for n in range(0,len(number)):
    new_a = a + number[n]
    vec_for_plot = np.reshape(new_a,(-1,1))
    i = i + 1

If I try to assemble the n*m-by-1 array into the vec_for_plot variable using:如果我尝试使用以下方法将n*m-by-1数组组装到vec_for_plot变量中:

vec_for_plot[:,i] = np.reshape(new_a,(-1,1))

then the following error shows up.然后出现以下错误。

ValueError: could not broadcast input array from shape (18,1) into shape (18)

I've tried np.column_stack, switching the indices, initializing empty matrices, matrices of zeros, etc.我试过 np.column_stack、切换索引、初始化空矩阵、零矩阵等。

Can someone please provide some guidance on where my indexing is going wrong?有人可以就我的索引出错的地方提供一些指导吗?

In [40]: arr = np.zeros((3,6))

When I index a 'column' of this array, I get a 1d array, shape (3,):当我索引这个数组的“列”时,我得到一个一维数组,形状(3,):

In [41]: arr[:,0]                 
Out[41]: array([0., 0., 0.])
In [42]: _.shape
Out[42]: (3,)     

I can assign another (3,) shape array to it:我可以为其分配另一个 (3,) 形状数组:

In [43]: arr[:,0] = np.arange(3)
In [44]: arr
Out[44]: 
array([[0., 0., 0., 0., 0., 0.],
       [1., 0., 0., 0., 0., 0.],
       [2., 0., 0., 0., 0., 0.]])

I can also assign a (1,3) shaped array:我还可以分配一个 (1,3) 形状的数组:

In [45]: arr[:,1] = np.ones((1,3))
In [46]: arr
Out[46]: 
array([[0., 1., 0., 0., 0., 0.],
       [1., 1., 0., 0., 0., 0.],
       [2., 1., 0., 0., 0., 0.]])

But I cannot assign a (3,1) shape.但我无法分配 (3,1) 形状。 That's what you are trying to do:这就是你想要做的:

In [47]: arr[:,1] = np.ones((3,1))*2
Traceback (most recent call last):
  File "<ipython-input-47-d7829ffd8ee7>", line 1, in <module>
    arr[:,1] = np.ones((3,1))*2
ValueError: could not broadcast input array from shape (3,1) into shape (3,)

This is a variant on the basic broadcasting rules.这是基本broadcasting规则的变体。 When adding a (3,) to (3,1) I get a (3,3).将 (3,) 添加到 (3,1) 时,我得到 (3,3)。 That's because the (3,) gets promoted to (1,3) (auto add of a leading dimension):这是因为 (3,) 被提升为 (1,3) (自动添加前导维度):

In [48]: Out[41]+np.ones((3,1))
Out[48]: 
array([[1., 2., 3.],
       [1., 2., 3.],
       [1., 2., 3.]])

But that kind of promotion is not allowed when assigning.但是分配时不允许这种提升。

Don't skip too much of the basic numpy documentation.不要跳过太多基本的numpy文档。 Otherwise details like this will confuse you.否则这样的细节会让你感到困惑。

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

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