A teacher asks his students to take a test twice, a few weeks apart.
2/
He thinks this is a fair system to use:
• If the difference in scores between the first and second tests is less than five marks, he'll keep the result of the first test
• Otherwise, he'll take the highest mark
3/
If you're not convinced that's a fair test, neither am I.
But that's what the teacher settled on…
Let's help the teacher write code to work this out using NumPy:
4/
The arrays `first_test` and `second_test` contain the marks for 5 students.
Let's look at how you use `np.where()` bit by bit.
The function takes three arguments.
In the code snippet above, the three arguments are shown on separate lines.
5/
The first argument is the condition you want to use to decide which value you want to keep.
In this case, you're checking whether the difference between the first and second test scores is less than 5.
6/
If this condition is `True`, the value from the second argument is used.
In this example, if the difference is less than 5 marks, the score from the first test is used.
7/
However, if the condition in the first argument in `False`, the value used is the one in the third argument.
In this example, this is the highest of the two scores.
8/
`np.maximum()` returns an array where each element is the maximum value from the corresponding elements in the arrays passed as arguments.
It's not the same as `np.max()` which returns the maximum value from one array.
9/
The second and third arguments you use in `np.where()` can be either arrays of the same shape and size, or ones which can be broadcast using NumPy broadcasting rules.
Here's an example:
10/
`a` is an array of shape (7, ). For broadcasting purposes, this is the same as (1, 7)
`b` has shape (7, 1)
The output from `np.where()` is a (7, 7) array
11/
In the first row of the output, the values in `a` are compared to the first value of `b` which is 3.
When the value of `a` is less than 3, then `a`'s value is used. Otherwise 3 is used.
The same applies for the other rows.
12/
So, if you're wondering where to find the data you need, you may want to ask NumPy…
`np.where()` is one of the many tools you have in NumPy that allow you to explore your data.
13/
You'll get lots of NumPy stuff if you follow on @s_gruppetta_ct…
…and Matplotlib, and general Python fundamentals, and…
… examples that take the `turtle` module to the limit!
takewhile takes successive items from a sequence for as long as a certain condition is true.
It's the equivalent of using a _while_ loop to _take_ successive items from the sequence until the while condition is not met–hence the name takewhile.
2/7
In this example, the challenge is to see how many successive heads I can get when I flip a coin…
In recent years, I have introduced #Python#coding to chemists, biologists, psychologists, medical scientists, geologists, well, the list goes on.
Suffice to say that it spans all areas of science.
Why?
2/4
All of these teams and individuals realise that either they have large amounts of data they need to analyse in all sorts of different ways, or they need to simulate experiments and create computational models.
#Python is often the language of choice in many of these fields.
3/4