Home > Software engineering >  I want to filter the columns with condition '>0' and den find minimum value along filte
I want to filter the columns with condition '>0' and den find minimum value along filte

Time:09-16

I am looking for this solution using Numpy, So anyone wish to do this operation/steps with minimal code it would help me a lot.

Step 1. This is the given array arr_a=

 [[0, 1.2, -0.3, 8, 3, -4],
 [1.3, -0.9, 3.2, -7.2, -4.2, 5.1], 
 [-2.1, 3.2, -3, -0.4, 4.3, 98],
 [6.1, 3.1, 4, -0.43, 2, -21], 
 [3.4, -9.8, 3, 4.2, -2.5, 33],
 [-2.1, 4, -9, 45, 3.2, -0.30],
 [3, -4.5, -5.0, -8, 5.6, -0.3]]

Step 2: I want to filter each column of array 'arr_a' with an expression '>0' it should return an array as below.

[[False, True, False, True, True, False],
 [True, False, True, False, False, True], 
 [False, True, False, False, True, True],
 [True, True, True, False, True, False], 
 [True, False, True, True, False, True],
 [False, True, False, True, True, False],
 [True, False, False, False, True, False]]

Step 3: From the above filtered array find the minimum number along the column skipping the values's which are 'False'

vector_min =

[1.3, 1.2, 3, 4.2, 2, 5.1]

Step 4:

sort them in descending order

f_ordered =

[5.1, 4.2, 3, 2, 1.3, 1.2]

Step 5: Get the indices of the columns after ordering.

index of column =

[5, 3, 2, 4, 0, 1]

CodePudding user response:

You can use:

# step2
m = arr_a>0
# array([[False,  True, False,  True,  True, False],
#        [ True, False,  True, False, False,  True],
#        [False,  True, False, False,  True,  True],
#        [ True,  True,  True, False,  True, False],
#        [ True, False,  True,  True, False,  True],
#        [False,  True, False,  True,  True, False],
#        [ True, False, False, False,  True, False]])

# step 3
vector_min = np.nanmin(np.where(m, arr_a, np.nan), axis=0)
# array([1.3, 1.2, 3. , 4.2, 2. , 5.1])

# step 5
index_of_column = np.argsort(-vector_min)
# array([5, 3, 2, 4, 0, 1])

# step 4
f_ordered = vector_min[index_of_column]
# array([5.1, 4.2, 3. , 2. , 1.3, 1.2])
  • Related