FAQ: Introduction to NumPy - Review


#1

This community-built FAQ covers the “Review” exercise from the lesson “Introduction to 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 Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!


#2

There is not a lot of discussion on some of the tasks asked to be performed in here, specifically 6 and 7 which could be considered difficult to a newer python learner.

6. “Hmmm, interesting” the researcher mumbles, “can you also give me the 6:00 AM data for Thursday and Friday?” Save this data to a new array thursday_friday_morning .

If you look at the two-dimensional array of:

[[ 46.6  48.1  61.8  56. ]
 [ 50.   47.5  61.3  55.6]
 [ 49.7  47.2  60.9  55.2]
 [ 49.5  47.1  60.6  54.9]
 [ 49.2  46.9  60.2  54.5]]

Then start the process by printing out something like this where we slice a start of 3, with a not inclusive stop of 5

print(temperatures_fixed[3:5])

You will get, something like below, which

[[ 49.5 47.1 60.6 54.9] [ 49.2 46.9 60.2 54.5]]

Good start, but not what the lesson wants, it only wants the 6am temperature for Thursday/Friday, not all the temperatures. You will use ‘,’ in the slice to select the element of the second dimension, 6am is element 1.

thursday_friday_morning = temperatures_fixed[3:5,1]

or

thursday_friday_morning = np.array(temperatures_fixed[3:5,1])

both will work

7. Select all temperatures that are under 50 degrees or over 60 degrees and save them to a new array temperature_extremes .

Past in some looping related tutorials they have shown that you can create a list from another list with some conditions using:

var = [l for l in list if <some condition with l>]

You can also perform a nested loop by doing:

var = [l for l in list for ll in l if <condition>]

So with that understanding you can do the following:

temperature_extremes = [float(t) for tf in temperatures_fixed for t in tf if t < 50 or t > 60]

Which will give you the output and the correct answer:

[46.6, 48.1, 61.8, 47.5, 61.3, 49.7, 47.2, 60.9, 49.5, 47.1, 60.6, 49.2, 46.9, 60.2]