python - Mask minimum values in matrix rows -


i have 3x3 matrix:

a=array([[ 1, 11,  5],    [ 3,  9,  9],    [ 5,  7, -3]]) 

i need mask minimum values in each row in order calculate mean of each row discarding minimum values. there general solution? have tried

a_masked=np.ma.masked_where(a==np.ma.min(a,axis=1),a) 

which masks minimum value in first , third row, not second row?

i appreciate help. thanks!

the issue because comparison a == a.min(axis=1) comparing each column minimum value of each row rather comparing each row minimum values. because a.min(axis=1) returns vector rather matrix behaves nx1 array. such, when broadcasting, == operator performs operation in column-wise fashion match dimensions.

a == a.min(axis=1)  # array([[ true, false, false], #        [false, false, false], #        [false, false,  true]], dtype=bool) 

one potential way fix resize result of a.min(axis=1) column vector (e.g. 3 x 1 2d array).

a == np.resize(a.min(axis=1), [a.shape[0],1])  # array([[ true, false, false], #        [ true, false, false], #        [false, false,  true]], dtype=bool) 

or more @colonelbeuvel has shown:

a == a.min(axis=1)[:,none] 

now applying entire line of code.

a_masked = np.ma.masked_where(a == np.resize(a.min(axis=1),[a.shape[0],1]), a)  # masked_array(data = #   [[-- 11 5] #   [-- 9 9] #   [5 7 --]], #        mask = #           [[ true false false] #            [ true false false] #            [false false  true]], #           fill_value = 999999) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -