This is MATLAB syntax, not numpy:
A = [1 0 0 2; 3 3 3 2; 3 3 0 2; 3 4 4 4]
though np.matrix
emulates it with:
In [172]: A = np.matrix('1 0 0 2; 3 3 3 2; 3 3 0 2; 3 4 4 4')
In [173]: A
Out[173]:
matrix([[1, 0, 0, 2],
[3, 3, 3, 2],
[3, 3, 0, 2],
[3, 4, 4, 4]])
Your task is 2 fold, finding that most frequent element, and then replacing all the others. Neither action depends on the matrix being 2d, or being matrix
as opposed to array.
In [174]: A1=A.A1
In [175]: A1
Out[175]: array([1, 0, 0, 2, 3, 3, 3, 2, 3, 3, 0, 2, 3, 4, 4, 4])
np.unique
can give us the frequency count, so we can fine the most frequent value with (unique
requires the 1d):
In [179]: u,c = np.unique(A1, return_counts=True)
In [180]: u
Out[180]: array([0, 1, 2, 3, 4])
In [181]: c
Out[181]: array([3, 1, 3, 6, 3])
In [182]: np.argmax(c)
Out[182]: 3
In [183]: u[np.argmax(c)]
Out[183]: 3
I'm surprised that Divakar use the scipy
mode
instead of unique
. He's something of an expert in using unique
. :)
Divakar's use of np.where
may be the simplest way of performing the replace.
Just for the fun of it, here's a masked array approach:
In [196]: np.ma.MaskedArray(A, A!=3)
Out[196]:
masked_matrix(data =
[[-- -- -- --]
[3 3 3 --]
[3 3 -- --]
[3 -- -- --]],
mask =
[[ True True True True]
[False False False True]
[False False True True]
[False True True True]],
fill_value = 999999)
In [197]: _.filled(0)
Out[197]:
matrix([[0, 0, 0, 0],
[3, 3, 3, 0],
[3, 3, 0, 0],
[3, 0, 0, 0]])
Or an inplace change:
In [199]: A[A!=3] = 0
In [200]: A
Out[200]:
matrix([[0, 0, 0, 0],
[3, 3, 3, 0],
[3, 3, 0, 0],
[3, 0, 0, 0]])