[Numpy-discussion] How to remove any row or column of a numpy matrix whose sum is 3?
Chris Barker
chris.barker at noaa.gov
Mon Jun 4 12:51:10 EDT 2012
On Mon, Jun 4, 2012 at 9:21 AM, bob tnur <bobtnur78 at gmail.com> wrote:
> Hello every body. I am new to python.
> How to remove any row or column of a numpy matrix whose sum is 3.
> To obtain and save new matrix P with (sum(anyrow)!=3 and sum(anycolumn)!=3
> elements.
well, one question is -- do you want to remove the particular rows
first, then remove the particular columns, or compute the sums of both
with all in place, then remove rows and columns -- but some ideas:
In [357]: m
Out[357]:
array([[ 1., 1., 1., 1., 0.],
[ 1., 0., 1., 1., 1.],
[ 1., 1., 1., 0., 0.],
[ 1., 1., 1., 1., 1.]])
# which rows, columns sum to 3?
In [363]: rows_3 = np.argwhere(m.sum(axis=1) == 3)
In [364]: rows_3
Out[364]: array([[2]])
In [365]: cols_3 = np.argwhere(m.sum(axis=0) == 3)
In [366]: cols_3
Out[366]:
array([[1],
[3]])
# but it's probably easier to know which do not sum to 3:
In [367]: rows_3 = np.argwhere(m.sum(axis=1) != 3)
In [368]: rows_3
Out[368]:
array([[0],
[1],
[3]])
In [371]: cols_3 = np.argwhere(m.sum(axis=0) != 3)
In [372]: cols_3
Out[372]:
array([[0],
[2],
[4]])
now build the new array:
m2 = In [415]: m2 = m[rows_3[:,0]][:, cols_3[:,0]]
In [416]: m2
Out[416]:
array([[ 1., 1., 0.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
some trickery there:
the [:,2] index is because argwhere() returns a 2-d (nXm)array of
indexes -- where m is the rank of the input array -- in this case one,
so we want the single column (it's done this way so you can do:
arr[ argwhere(arr == something) ]
for n-dim arrays
I also found I need to pull out the rows first, then the columns, because:
arr[a_1, a_2]
is interpreted as two arrays of individual indexes, not as indexes to
the roes an columsn, i.e.:
In [428]: arr
Out[428]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [430]: arr[(1,2), (2,3)]
Out[430]: array([ 6, 11])
you got the elements at: (1,2) and (2,3)
There may be a way to do that a bit cleaner -- it escapes me at the moment.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list