如何通过对值求和来组合 numpy 中矩阵的两列/行? [英] How to combine two columsn/rows of a matrix in numpy by summing values?
问题描述
我正在使用 numpy 并试图找出我在使用矩阵时遇到的问题.假设我有一个 3 x 3 矩阵,并希望通过将第一列和第二列和行相加将其转换为 2 x 2 矩阵.例如,给定:
I'm using numpy and am trying to figure out an issue I'm having with matrices. Say I have a 3 x 3 matrix and want to transform it into a 2 x 2 matrix through summing the first and second columns and rows together. For instance, given:
import numpy as np
a = [[1,2,3],[4,5,6],[7,8,9]]
m = np.array(a)
显示 m 产量:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
我想先对第一行和第二行求和:
I want to first sum the first and second rows to get:
array([5, 7, 9],
[7, 8, 9])
然后将第一列和第二列相加得到:
And then sum the first and second columns to get:
array([12, 9],
[15, 9])
这只是一个玩具示例,但实际上我有一个 24 x 24 的矩阵,我想将其精简为 4 x 4 的矩阵.我有 4 个不同的整数列表,每个列表代表整个 24 x 24 矩阵中的不同索引.我想要做的是遍历每个列表并组合在列表中找到索引的行和列.例如,如果一个列表具有索引 [3,4,5],我想遍历该列表并组合第 3、4 和 5 行/列.知道如何在 numpy 中完成此操作吗?
This is just a toy example, but in practice I have a 24 x 24 matrix that I want to slim down to a 4 x 4 matrix. I have 4 different lists of integers, each list representing different indices in the overall 24 x 24 matrix. What I want to do is iterate through each list and combine the rows and columns whose indices are found in the lists. For instance if one list has the indices [3,4,5], I want to iterate through this list and combine rows/columns 3, 4 and 5. Any idea how this can be done in numpy?
推荐答案
您可以使用整数数组索引和 numpy sum 中的axis"选项来完成此操作.例如,开头:
You can do this using integer array indexing and the "axis" option in numpy's sum. For example, starting with:
import numpy as np
a = np.arange(16).reshape(4,4)
list0 = [0,1,2]
list1 = [3]
初始数组为
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
索引列表可用于仅从数组中选择"您想要的行.例如,
The lists of indices can be used to "select" only the rows you want from an array. For example,
a[list0,:]
是
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
此外,可以使用 numpy 求和中的axis=0"参数将数组的行相加,结果是一维数组.例如,
Also, rows of an array can be added together using the "axis=0" argument in numpy's sum, and the result is a 1D array. For example,
np.sum(a[list0,:],axis=0)
结果
array([12, 15, 18, 21])
像这样求和我们可以逐行构建行求和数组:
Summing like this we can build the row-summed array row by row:
b = np.zeros((2,a.shape[1]))
b[0,:] = np.sum(a[list0,:],axis=0)
b[1,:] = np.sum(a[list1,:],axis=0)
结果数组为
array([[12., 15., 18., 21.],
[12., 13., 14., 15.]])
可以对列执行类似的操作,但使用axis=1"来添加列:
A similar thing can be done for the columns, but with "axis=1" to add the columns:
c = np.zeros((2,2))
c[:,0] = np.sum(b[:,list0],axis=1)
c[:,1] = np.sum(b[:,list1],axis=1)
产生 2x2 数组:
array([[45., 21.],
[39., 15.]])
如果您想以更紧凑的方式进行操作,您可以遍历列表并使用 vstack 将它们组合成一个数组:
If you want to do it in a more compact way, you could loop over the lists and combine them into a single array using vstack:
idx_lists = [list0, list1]
b = np.vstack([np.sum(a[idx_lists[n],:],0) for n in range(2)])
将列堆叠在一起有点棘手,因为 sum 返回一个一维数组,该数组被视为一行.一种解决方案是将行垂直堆叠在一起并在最后进行转置:
Stacking together the columns is a little trickier, since sum returns a 1D array, which is treated as a row. One solution is to vertically stack the rows together and take a transpose at the end:
c = np.vstack([np.sum(b[:,idx_lists[n]],1) for n in range(2)]).T
这篇关于如何通过对值求和来组合 numpy 中矩阵的两列/行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!