FAQ: Introduction to Statistics with NumPy - Calculating the Mean of 2D Arrays

This community-built FAQ covers the “Calculating the Mean of 2D Arrays” exercise from the lesson “Introduction to Statistics with NumPy”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Introduction to Statistics with NumPy

FAQs on the exercise Calculating the Mean of 2D Arrays

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello,

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

allergy_trials = np.array([[6, 1, 3, 8, 2],
[2, 6, 3, 9, 8],
[5, 2, 6, 9, 9]])

total_mean = np.mean([allergy_trials])

trial_mean = np.mean([allergy_trials, axis=1])

patient_mean = np.mean([allergy_trials, axis=0])

I receive the follow error: ‘SyntaxError: invalid syntax’. The error is corrected by removing the inner brackets as follows:

trial_mean = np.mean(allergy_trials, axis=1)

and

patient_mean = np.mean(allergy_trials, axis=0)

However, why is no syntax error generated when both parentheses and brackets is used for the following:

total_mean = np.mean([allergy_trials])

Thank you in advance!

1 Like

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)-

np.mean([[1], [2], [3]])
1 Like