FAQ: Introduction to NumPy - Review

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

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!

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]
3 Likes

I am curious what the code would be for task 6 if they had asked us for 6:00am data for Wednesday & Friday instead so I can learn what the formatting would be when two rows can’t be expressed as a range.

I came here looking for the source .csv so I can play with the data on my own Python interpreter… anyone have a link to the NOAA .csv data that this exercise references?

Meh.
I played with the code and get it up on github as a colab notebook. I hope it helps someone! Jupyter notebooks are awesome for learning code!

Finally, the persistent researcher now wants all the high and low temperatures over the week.

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

I don’t understand the reason why if i do this
temperature_extremes = temperatures_fixed[(temperatures_fixed < 50) & (temperatures_fixed > 60)]
print(’\n’)
print(temperature_extremes)

that is, I want the array list of temperatures that are less than 50 and the temperatures that are more than 60 from the temperature fixed array but it gives me empty array

If i change the logic and say i want the array of temperatures that are less than 50 or the temperatures above 60 it gives me the result i wanted for the AND operator i.e

temperature_extremes = temperatures_fixed[(temperatures_fixed < 50) | (temperatures_fixed > 60)]
print(’\n’)
print(temperature_extremes)

THIS THREW ME OFF BALANCE AND GET ME MORE CONFUSED

1 Like

I’m bit late, and you might have figured this out by now, but…

this assumption is wrong:
temperatures_fixed[(temperatures_fixed < 50) & (temperatures_fixed > 60)]
that is, I want the array list of temperatures that are less than 50 and the temperatures that are more than 60 from the temperature fixed array

what you are actually asking is “give me all temperatures that are below 50 and above 60, AT THE SAME TIME.” as you can imagine, there is no temperature that can satisfy both conditions, so it returns nothing.

when you use | (‘OR’), you are now asking “give me all temperatures that are EITHER below 50 or above 60”. in this case, you are excluding all temperatures that fall between 50 and 60, and returning only the extremes.

Just tried doing #7 with:
temperature_extremes=temperatures_fixed([temperatures_fixed<50.0] | [temperatures_fixed>60.0])

and the answer is:
temperature_extremes=temperatures_fixed[(temperatures_fixed<50.0) | (temperatures_fixed>60.0)]

Can someone tell me how the brackets versus parentheses makes a difference?

[…] brackets are used to create lists.
In this case, you are basically storing your logical np.array in a list, which is not the expected type within the context of your OR expression.

you can use type() to check what you are working with:

type(temperatures_fixed<50.0) #<class 'numpy.ndarray'>
type([temperatures_fixed<50.0]) #<class 'list'>

how do you get the or expression in numpy ?
It must be a special character, but what number ?
Thanks

Thank you for sharing this - task 6 really threw me off because of the confusion with the syntax of accessing specific columns!