I want to filter the second index of numpy array, but why can't I save it to filter_arr = []
the code :
import numpy as np
data = [
[0.0,52.0,33.0,44.0,51.0],
[0.0,30.0,45.0,12.0,44.0],
[0.0,67.0,99.0,23.0,78.0]
]
arr = np.array(data)
filter_arr = []
for i in range(0,len(arr)):
if arr[:1,i] > 50:
filter_arr.append(arr)
filter_arr = np.array(filter_arr)
filter_arr
the filter array should be :
array([[[ 0., 52., 33., 44., 51.],
[ 0., 67., 99., 23., 78.]],
CodePudding user response:
You could try below: This will filter whole row by second index.
import numpy as np
data = [
[0.0,52.0,33.0,44.0,51.0],
[0.0,30.0,45.0,12.0,44.0],
[0.0,67.0,99.0,23.0,78.0]
]
arr = np.array(data)
filter_arr = []
for i in range(len(arr)):
if arr[i,1] > 50:
filter_arr.append(arr[i])
filter_arr = np.array(filter_arr)
filter_arr
CodePudding user response:
arr[np.max(arr, axis = 1) > 50,:]
or as a function:
def filter_rows(arr, min=50):
return arr[np.max(arr, axis = 1) > min,:]
based on https://stackoverflow.com/a/70185464/3957794
CodePudding user response:
data = [
[0.0,52.0,33.0,44.0,51.0],
[0.0,30.0,45.0,12.0,44.0],
[0.0,67.0,99.0,23.0,78.0]
]
arr = np.array(data)
np.array([list(arr[i]) for i in range(len(arr)) if arr[i,1] > 50])
array([[ 0., 52., 33., 44., 51.],
[ 0., 67., 99., 23., 78.]])
CodePudding user response:
Don't use loops with numpy, the correct approach here is to use numpy slicing/indexing:
filter_arr = arr[arr[:, 1]>50]
Output:
array([[ 0., 52., 33., 44., 51.],
[ 0., 67., 99., 23., 78.]])