BKS
BKS

Reputation: 2333

iterating over numpy matrix columns

I have the following numpy matrix:

        0      1      2      3      4      5      6      7      8      9      
0       0.0    1.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0   
1       0.0    0.0    5.0    0.0    9.0    0.0    0.0    0.0    0.0    0.0   
2       0.0    0.0    0.0    0.0    0.0    0.0    2.0    0.0    0.0    0.0   
3       0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    5.0    0.0   
4       0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0   
5       0.0    0.0    7.0    0.0    1.0    0.0    0.0    0.0    0.0    0.0   
6       0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0   
7       5.0    0.0    0.0    0.0    0.0    0.0    0.0    6.0    0.0    0.0   
8       2.0    0.0    0.0    0.0    3.0    0.0    6.0    0.0    8.0    0.0   
9       0.0    0.0    1.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0   
10      0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0    0.0   

I want to calculate the non-zero values average of every row and column separately. So my result should be something like this:

average_rows = [1.0,7.0,2.0,5.0,0.0,4.0,0.0,5.5,4.75,1.0,0.0]
average_cols = [3.5,1.0,4.33333,0.0,4.33333,0.0,4.0,6.0,6.5,0.0]

I can't figure out how to iterate over them, and I keep getting TypeError: unhashable type

Also, I'm not sure if iterating is the best solution, I also tried something like R[:,i] to grab each column and sum it using sum(R[:,i]), but keep getting the same error.

Upvotes: 0

Views: 737

Answers (1)

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8097

It is better to use 2d np.array instead of matrix.

import numpy as np
data = np.array([[1, 2, 0], [0, 0, 1], [0, 2, 4]], dtype='float')
data[data == 0] = np.nan
# replace all zeroes with `nan`'s to skip them
# [[  1.   2.  nan]
# [ nan  nan   1.]
# [ nan   2.   4.]]
np.nanmean(data, axis=0)
# array([ 1. ,  2. ,  2.5])
np.nanmean(data, axis=1)
# array([ 1.5,  1. ,  3. ])

Upvotes: 1

Related Questions