简体   繁体   English

Numpy - 平均2D阵列的多个列

[英]Numpy - Averaging multiple columns of a 2D array

Right now I am doing this by iterating, but there has to be a way to accomplish this task using numpy functions. 现在我通过迭代来做这个,但必须有一种方法来使用numpy函数完成这个任务。 My goal is to take a 2D array and average J columns at a time, producing a new array with the same number of rows as the original, but with columns/J columns. 我的目标是一次采用2D数组和平均J列,生成一个与原始行数相同的新数组,但是使用列/ J列。

So I want to take this: 所以我想接受这个:

J = 2 // two columns averaged at a time

[[1 2 3 4]
 [4 3 7 1]
 [6 2 3 4]
 [3 4 4 1]]

and produce this: 并产生这个:

[[1.5 3.5]
 [3.5 4.0]
 [4.0 3.5]
 [3.5 2.5]]

Is there a simple way to accomplish this task? 有没有一种简单的方法来完成这项任务? I also need a way such that if I never end up with an unaveraged remainder column. 我还需要一种方法,如果我永远不会得到一个不平衡的余数列。 So if, for example, I have an input array with 5 columns and J=2, I would average the first two columns, then the last three columns. 因此,例如,如果我有一个包含5列且J = 2的输入数组,我将平均前两列,然后是最后三列。

Any help you can provide would be great. 你能提供的任何帮助都会很棒。

data.reshape(-1,j).mean(axis=1).reshape(data.shape[0],-1)

If your j divides data.shape[1] , that is. 如果你的j data.shape[1] ,那就是。

Example: 例:

In [40]: data
Out[40]: 
array([[7, 9, 7, 2],
       [7, 6, 1, 5],
       [8, 1, 0, 7],
       [8, 3, 3, 2]])

In [41]: data.reshape(-1,j).mean(axis=1).reshape(data.shape[0],-1)
Out[41]: 
array([[ 8. ,  4.5],
       [ 6.5,  3. ],
       [ 4.5,  3.5],
       [ 5.5,  2.5]])

First of all, it looks to me like you're not averaging the columns at all, you're just averaging two data points at a time. 首先,在我看来,你根本就没有对列进行平均,你只是一次平均两个数据点。 Seems to me like your best off reshaping the array, so your that you have a Nx2 data structure that you can feed directly to mean . 对我而言似乎是最好的重塑数组,所以你有一个Nx2数据结构,你可以直接提供mean You may have to pad it first if the number of columns isn't quite compatible. 如果列数不完全兼容,您可能必须先填充它。 Then at the end, just do a weighted average of the padded remainder column and the one before it. 然后在最后,只需对填充的剩余列和前一列进行加权平均。 Finally reshape back to the shape you want. 最后重塑回你想要的形状。

To play off of the example provided by TheodrosZelleke: 要播放TheodrosZelleke提供的示例:

In [1]: data = np.concatenate((data, np.array([[5, 6, 7, 8]]).T), 1)

In [2]: data
Out[2]: 
array([[7, 9, 7, 2, 5],
       [7, 6, 1, 5, 6],
       [8, 1, 0, 7, 7],
       [8, 3, 3, 2, 8]])

In [3]: cols = data.shape[1]

In [4]: j = 2

In [5]: dataPadded = np.concatenate((data, np.zeros((data.shape[0], j - cols % j))), 1)

In [6]: dataPadded
Out[6]: 
array([[ 7.,  9.,  7.,  2.,  5.,  0.],
       [ 7.,  6.,  1.,  5.,  6.,  0.],
       [ 8.,  1.,  0.,  7.,  7.,  0.],
       [ 8.,  3.,  3.,  2.,  8.,  0.]])

In [7]: dataAvg = dataPadded.reshape((-1,j)).mean(axis=1).reshape((data.shape[0], -1))

In [8]: dataAvg
Out[8]: 
array([[ 8. ,  4.5,  2.5],
       [ 6.5,  3. ,  3. ],
       [ 4.5,  3.5,  3.5],
       [ 5.5,  2.5,  4. ]])

In [9]: if cols % j:
    dataAvg[:, -2] = (dataAvg[:, -2] * j + dataAvg[:, -1] * (cols % j)) / (j + cols % j)
    dataAvg = dataAvg[:, :-1]
   ....:     

In [10]: dataAvg
Out[10]: 
array([[ 8.        ,  3.83333333],
       [ 6.5       ,  3.        ],
       [ 4.5       ,  3.5       ],
       [ 5.5       ,  3.        ]])

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

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