numpy如何迭代数组的列?

问题:numpy如何迭代数组的列?

假设我有和mxn数组。我想将此数组的每一列传递给函数,以对整个列执行一些操作。如何遍历数组的列?

例如,我有一个4 x 3的数组

1  99 2
2  14 5
3  12 7
4  43 1

for column in array:
  some_function(column)

其中列在第一次迭代中将为“ 1,2,3,4”,在第二次迭代中为“ 99,14,12,43”,在第三次迭代中为“ 2,5,7,1”。

Suppose I have and m x n array. I want to pass each column of this array to a function to perform some operation on the entire column. How do I iterate over the columns of the array?

For example, I have a 4 x 3 array like

1  99 2
2  14 5
3  12 7
4  43 1

for column in array:
  some_function(column)

where column would be “1,2,3,4” in the first iteration, “99,14,12,43” in the second, and “2,5,7,1” in the third.


回答 0

只需遍历数组的转置即可:

for column in array.T:
   some_function(column)

Just iterate over the transposed of your array:

for column in array.T:
   some_function(column)

回答 1

这应该给你一个开始

>>> for col in range(arr.shape[1]):
    some_function(arr[:,col])


[1 2 3 4]
[99 14 12 43]
[2 5 7 1]

This should give you a start

>>> for col in range(arr.shape[1]):
    some_function(arr[:,col])


[1 2 3 4]
[99 14 12 43]
[2 5 7 1]

回答 2

对于三维数组,您可以尝试:

for c in array.transpose(1, 0, 2):
    do_stuff(c)

请参阅有关array.transpose工作原理的文档。基本上,您要指定要移动的尺寸。在这种情况下,我们将第二维(例如列)移动到第一维。

For a three dimensional array you could try:

for c in array.transpose(1, 0, 2):
    do_stuff(c)

See the docs on how array.transpose works. Basically you are specifying which dimension to shift. In this case we are shifting the second dimension (e.g. columns) to the first dimension.


回答 3

for c in np.hsplit(array, array.shape[1]):
    some_fun(c)
for c in np.hsplit(array, array.shape[1]):
    some_fun(c)

回答 4

您还可以使用解压缩来遍历各列

for col in zip(*array):
   some_function(col)

You can also use unzip to iterate through the columns

for col in zip(*array):
   some_function(col)

回答 5

例如,您要查找矩阵中每一列的平均值。让我们创建以下矩阵

mat2 = np.array([1,5,6,7,3,0,3,5,9,10,8,0], dtype=np.float64).reshape(3, 4)

均值的函数是

def my_mean(x):
    return sum(x)/len(x)

执行所需的操作并将结果存储在结肠向量“结果”中

results = np.zeros(4)
for i in range(0, 4):
    mat2[:, i] = my_mean(mat2[:, i])

results = mat2[1,:]      

结果是:array([4.33333333,5.,5.66666667,4.])

For example you want to find a mean of each column in matrix. Let’s create the following matrix

mat2 = np.array([1,5,6,7,3,0,3,5,9,10,8,0], dtype=np.float64).reshape(3, 4)

The function for mean is

def my_mean(x):
    return sum(x)/len(x)

To do what is needed and store result in colon vector ‘results’

results = np.zeros(4)
for i in range(0, 4):
    mat2[:, i] = my_mean(mat2[:, i])

results = mat2[1,:]      

The results are: array([4.33333333, 5. , 5.66666667, 4. ])


回答 6

或者,您可以使用enumerate。它也为您提供列号和列值。

for num, column in enumerate(array.T):
    some_function(column) # column: Gives you the column value as asked in the question
    some_function(num) # num: Gives you the column number 

Alternatively, you can use enumerate. It gives you the column number and the column values as well.

for num, column in enumerate(array.T):
    some_function(column) # column: Gives you the column value as asked in the question
    some_function(num) # num: Gives you the column number