I have a 2D numpy array. Lets say:
abc = np.arange(40).reshape(10,4)
Now I am accepting a user input string variable 'mm'. This variable determines what I want to do with the array. It has the following 4 possible values:
'min' : Returns a column with all the minimum values
'max' : Returns a column with all the maximum values
'mean': Returns a column with all the mean values
'time' : Returns a column with the value at a particular column. It also needs an additional input from the user; variable 'idx' with the column number.
I have included the first three possibilities by defining a dictionary variable that chooses the relevant function.
dict = {'mean':np.mean, 'max':np.max, 'min':np.min}
fn = dict[mm]
Therefore, my output variable will be:
op = fn(abc,axis=1)
But I am stuck when I try to include the 4th possibility of slicing a particular column based on user input. Is it possible to define any numpy function within the dictionary 'dict' that can include this operation as well?
I would prefer not to use an if-else condition, as it makes the code too long. The actual variables are much larger and more complicated to handle.
The expected outcome when the user variable 'mm' has the different values:
op_min = np.min(abc,axis=1)
op_max = np.max(abc,axis=1)
op_mean = np.mean(abc,axis=1)
op_time = abc[:,2] # assuming the user input for idx is 2
np.takemight do the job