How to get the value from each column in a Numpy matrix

I would like to get the index of the value for each column in the matrix M For instance:

 M = matrix([[0, 1, 0], [4, 2, 4], [3, 4, 1], [1, 3, 2], [2, 0, 3]]) 

In pseudo code, I would like to do something like this:

 for col in M: idx = numpy.where(M[col]==0) # Only for columns! 

and idx 0 , 4 , 0 for each column.

I tried using where , but I don't understand the return value, which is a tuple of matrices.

+5
source share
5 answers

A tuple of matrices is a collection of elements suitable for indexing. The output will take the form of indexing matrices (or arrays), and each element at the output will be selected from the original array, using the first array as the index of the first dimension, the second as the index of the second dimension, and so on. In other words, it is:

 >>> numpy.where(M == 0) (matrix([[0, 0, 4]]), matrix([[0, 2, 1]])) >>> row, col = numpy.where(M == 0) >>> M[row, col] matrix([[0, 0, 0]]) >>> M[numpy.where(M == 0)] = 1000 >>> M matrix([[1000, 1, 1000], [ 4, 2, 4], [ 3, 4, 1], [ 1, 3, 2], [ 2, 1000, 3]]) 

The sequence may be up to you. It passes in a flattened order - therefore, M[0,2] appears second, not third. If you need to reorder, you can do this:

 >>> row[0,col.argsort()] matrix([[0, 4, 0]]) 

It may also be useful for you to use arrays instead of matrices. This way you can manipulate the shape of arrays, which is often useful! Also note the ajcr trick, which is probably preferable to using argsort .

Finally, there is also a nonzero method that does the same as where in this case. Now use the transpose trick:

 >>> (M == 0).T.nonzero() (matrix([[0, 1, 2]]), matrix([[0, 4, 0]])) 
+3
source

As an alternative to np.where you can use np.argwhere to return an array of indices where the array satisfies the condition:

 >>> np.argwhere(M == 0) array([[[0, 0]], [[0, 2]], [[4, 1]]]) 

This tells each index in the format [row, column] where the condition was met.

If you want the format of this output array to be grouped by column rather than row (ie [column, row] ), simply use the transpose method of the array:

 >>> np.argwhere(MT == 0).squeeze() array([[0, 0], [1, 4], [2, 0]]) 

I also used np.squeeze here to get rid of axis 1, so we are left with a 2D array. The sequence you want is the second column, i.e. np.argwhere(MT == 0).squeeze()[:, 1] .

+2
source

The result of where(M == 0) will look something like this:

(matrix([[0, 0, 4]]), matrix([[0, 2, 1]])) The first matrix tells the rows where 0 is, and the second matrix tells the columns where 0 .

 Out[4]: matrix([[0, 1, 0], [4, 2, 4], [3, 4, 1], [1, 3, 2], [2, 0, 3]]) In [5]: np.where(M == 0) Out[5]: (matrix([[0, 0, 4]]), matrix([[0, 2, 1]])) In [6]: M[0,0] Out[6]: 0 In [7]: M[0,2] #0th row 2nd column Out[7]: 0 In [8]: M[4,1] #4th row 1st column Out[8]: 0 
0
source

This is not something new in what has already been proposed, but a one-line solution:

 >>> np.where(np.array(MT)==0)[-1] array([0, 4, 0]) 

(I agree that NumPy matrix objects have more problems than they are worth it).

0
source
 >>> M = np.array([[0, 1, 0], ... [4, 2, 4], ... [3, 4, 1], ... [1, 3, 2], ... [2, 0, 3]]) >>> [np.where(M[:,i]==0)[0][0] for i in range(M.shape[1])] [0, 4, 0] 
0
source

Source: https://habr.com/ru/post/1209124/


All Articles