Home > database >  finding rows of an array that all columns has inf value
finding rows of an array that all columns has inf value

Time:06-25

I have an array that some rows have inf value,but I need to find rows that all columns have inf value. I can find one row with inf value but I need that all values of this row be inf. my array is nxm and all m column should have inf to be selected. do you know how can I do this in python? Thanks.

CodePudding user response:

Change equality if need all rows with "inf", written this way for better understanding

What you need to do find the minimum value in each row & check if that is not 'inf' than that row is eligible for output. Below is the example code:

import numpy as np
a = np.array([[np.inf,np.inf,np.inf,np.inf],[5,7,6,9],[1, 2, np.inf, 4]])
a[a.min(axis=1) != np.inf]

Output will be : array([[ 5., 7., 6., 9.], [ 1., 2., inf, 4.]])

Updating as per the OP question in comments - how to get row index of filtered row

np.where([a.min(axis=1) != np.inf])[1]

The output of this will be array([1, 2]), rows with atleast one non 'inf' value (in above example 2 & 3rd row, index 1 & 2).

Similarly

np.where([a.min(axis=1) == np.inf])[1]

This will return array([0]) , index of row (in above example case, 0th row) with all the 'inf' values.

  • Related