I need to convert several numpy arrays according to this rule. Get several arrays. Do elementwise comparison. If the given array at this position has value greater than 0.5 and is the greatest among all arrays at this index, then the value in this position of the corresponding output array is one. Otherwise - zero.
import pandas as pd
import numpy as np
def max_is_greater_than_half_1d(*args):
df = pd.DataFrame(dict({'col_'+str(i+1): val for i, val in enumerate(args)}))
max_val = df.apply(max, axis=1)
df = df.apply(lambda x: (x > 0.5) & (max_val == x), axis=0).astype(int)
return [np.array(df[col].values) for col in df.columns]
in_1=np.array([0.4, 0.7, 0.8, 0.3, 0.3])
in_2=np.array([0.9, 0.8, 0.6, 0.4, 0.4])
in_3=np.array([0.5, 0.5, 0.5, 0.2, 0.6])
out_1, out_2, out_3 = max_is_greater_than_half(in_1, in_2,in_3)
# out_1: [0, 0, 1, 0, 0]
# out_2: [1, 1, 0, 0, 0]
# out_3: [0, 0, 0, 0, 1]
This works. But how can I do the same operation on several 2d arrays?
example:
in_1=np.array([[0.4, 0.7], [0.8, 0.3]])
in_2=np.array([[0.9, 0.8], [0.6, 0.4])
out_1 = [[0, 0], [1, 0]] and out_2 = [[1, 1], [0, 0]]
In my case I have six 2000x2000 arrays, so an elementwise operation is going to be too slow. An operation on a whole array is preferable.