python - deleting rows in numpy array -
i have array might this:
anovainputmatrixvaluesarray = [[ 0.96488889, 0.73641667, 0.67521429, 0.592875, 0.53172222], [ 0.78008333, 0.5938125, 0.481, 0.39883333, 0.]]
notice 1 of rows has 0 value @ end. want delete row contains zero, while keeping row contains non-zero values in cells.
but array have different numbers of rows every time populated, , zeros located in different rows each time.
i number of non-zero elements in each row following line of code:
numnonzeroelementsinrows = (anovainputmatrixvaluesarray != 0).sum(1)
for array above, numnonzeroelementsinrows
contains: [5 4]
the 5 indicates possible values in row 0 nonzero, while 4 indicates 1 of possible values in row 1 zero.
therefore, trying use following lines of code find , delete rows contain 0 values.
for q in range(len(numnonzeroelementsinrows)): if numnonzeroelementsinrows[q] < numnonzeroelementsinrows.max(): p.delete(anovainputmatrixvaluesarray, q, axis=0)
but reason, code not seem anything, though doing lot of print commands indicates of variables seem populating correctly leading code.
there must easy way "delete row contains 0 value."
can show me code write accomplish this?
the simplest way delete rows , columns arrays numpy.delete
method.
suppose have following array x
:
x = array([[1,2,3], [4,5,6], [7,8,9]])
to delete first row, this:
x = numpy.delete(x, (0), axis=0)
to delete third column, this:
x = numpy.delete(x,(2), axis=1)
so find indices of rows have 0 in them, put them in list or tuple , pass second argument of function.
Comments
Post a Comment