Where are all those values I need?

You can ask NumPy.

Just ask `np.where()`?

Let's see how this function works through an example…

👇🪡🧵

#python #numpy

1/
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/ Image
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/ Image
`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!

14/

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Stephen Gruppetta

Stephen Gruppetta Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @s_gruppetta_ct

Jun 9
You know about the `for` loop, I know.

But do you?

Are you sure?

Let's break the `for` loop down to see what's happening just a bit underneath the surface

Read on…

👇🪡🧵

#Python

/1
OK, so I'll assume you're very familiar with this:

>>> numbers = [4, 6, 8, 12, 15]
>>> for number in numbers:
... print(number)

Let's explore what's happening here by creating our own class called `Test`:

/2 Image
"'Test' object is not iterable" is the error message you got.

You cannot use a `Test` object in a `for` loop.

Not yet, anyway.

When a for loop starts, it tries to convert the object you're looping through into an iterator.

/3
Read 19 tweets
Jun 7
User—>High Level Programmer—>Low Level Programmer: An Analogy

Everyone is a user of computer programs.

But many non-programmers don't quite understand what's the role of a programmer.

They see programming as black magic

Let's look at an analogy: A House

👇🪡🧵

#Python

1/
Bob and Kate live in a house.

They're the **users** of the house.

When they needed a house to live in, they bought or rented one.

2/
Bob and Kate don't know anything about how to build a house. That's the builder's job.

The builder is the **programmer**.

The builder learnt the skills needed to build a house just as programmers learn how to code.

3/
Read 15 tweets
Apr 27
itertools.takewhile()

one of those good-to-know-about-functions

I decided to explore it today…

Read on if you've not come across takewhile before…

👇🪡🧵

#Python #coding #100daysofcode

1/7
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…

3/7
Read 7 tweets
Apr 26
When I trained as a scientist, #programming was a required tool in some select areas of #physics & #maths but not much beyond that…

That's no longer the case. #Coding is rapidly becoming a must-have skill in all scientific fields

Read on…

👇🪡🧵

#ScientificComputing

1/4
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
Read 4 tweets
Apr 25
The anatomy of a 2D Python game as seen through pictures and videos…

Let's have a look at the steps needed to create this game: thepythoncodingbook.com/2022/04/24/pyt…

👇🪡🧵

#python #100daysofcode #coding #2DGame

1 of 12
1. Create the window and game background

2 of 12
2. Create the lunar module

3 of 12
Read 12 tweets
Apr 25
Happy Birthday 🎂 ThePythonCodingBook.com
You're turning 1 🕯️ in a few days' time.

I'm still shocked how almost 20,000 people visit every month just from organic traffic.

A number of posts have done extremely well.

Here are the top five:

#python #coding #100daysofcode

1/8
In #5th place:

The White Room analogy that explains a lot of what's happening in #programming using a mental image that everyone can relate to.

thepythoncodingbook.com/understanding-…

#python #100daysofcode

2/8
In #4th place:

This fun project designed to practise using lists, tuples, dictionaries, and sets in a fun turtle-based animation.

thepythoncodingbook.com/2021/10/31/usi…

#python #turtle #datastructures

3/8
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(