I’ve recently completed the ‘Calculating the Mean of 2D Arrays’ exercise. I’m a bit confused when it is appropriate to use parenthesis and/or brackets. I used the following code:
import numpy as np
It might be worth having a quick once over on some lessons on Python functions to properly understand this or you may find some unexpected errors with further coding. It looks like a misunderstanding with Python rather than numpy itself.
In general avoid adding parantheses of any form unless they are necessary or increase readability. You seem to have added the square brackets to your code either without reason or without understanding what they are for.
You can pass an entire list or, in fact, any object as a single argument.
Your line-
patient_mean = np.mean([allergy_trials, axis=0])
is passing the list [allergy_trials, axis=1] as the first argument of the function. This will throw an error as this is not possible to simplify to an array of values. Without confining these items to a list, allergy_trials is sent as the first argument and axis=0 is sent as a keyword argument which works as intended.
The following will work as the argument for the mean function takes an array or ‘array like’ argument and it will simplify the argument you gave.
np.mean([allergy_trials])
Something like the following would technically work (though you’d be better to simplify it beforehand)-