0

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
2
  • 1
    np.take might do the job Commented Mar 26, 2019 at 15:05
  • Yes, that looks like a function that does the task which I wanted. However, it has an additional argument compared to np.max, np.min etc. But I think it can still be overridden in the same manner that I did for the answer posted by me. Thanks a lot anyway! Commented Mar 26, 2019 at 15:48

2 Answers 2

2

If you can transpose the matrix , this function can select a row:

abc = np.arange(40).reshape(10,4)
row_num = 2
f = operator.itemgetter(row_num)
f(abc)

Also you can define one line function by lambda for selecting a column:

f = lambda x: x[:,column_num]
f(abc)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I can transpose the matrix. And this seems like exactly the kind of function I was looking for. But I still have one problem, how to go around the axis parameter in np.mean, np.max etc. Coz itemgetter does not accept any axis keyword argument, but the other functions require this keyword.
0

Okay, I found a solution. I transposed the matrix as suggested by user3371603 and used the function operator.getitem. Then I used an index variable (idx) with a default value of 0. So,

import operator
dict = {'mean':np.mean, 'max':np.max, 'min':np.min, 'time':operator.getitem}
fn = dict[mm]

idx = 0
if mm == 'time': idx=2     # or user input

cba = np.swapaxes(abc,0,1)
op = fn(cba, idx)

Special thanks to user3371603!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.