1

I want to calculate the mode of columns in a numpy array, excluding a specific value (0) from the calculation.

Example numpy array:

n=np.array([[0,2,1], [0,1,3], [1,2,3]])
>array([[0, 2, 1],
   [0, 1, 3],
   [1, 2, 3]])

create mask for where values dont equal 0

m_mask = n != 0    
>array([[False,  True,  True],
   [False,  True,  True],
   [ True,  True,  True]])

Apply the mask and calculate mean on axis 0:

from scipy.stats import mode

new_m = np.ma.array(n, mask = m_mask)
m=mode(new_m, axis=0)
m[0]  #access the values not the count
>array([[0, 2, 3]])

Seems like scipy.stats.mean may be ignoring the masked array?

Any ideas on how I can accomplish this?

1 Answer 1

3

I think np.ma.array(...) doesn't fit here. You can replace the line, where you assign new_m:

new_m = np.where(m_mask, n, np.nan)

(scipy.mode(...) will ignore nan-s). Output:

[[1. 2. 3.]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.