Feature Engineering Workout 1
- 02:40
How to use the numpy where function in a Jupiter notebook.
Downloads
No associated resources to download.
Glossary
Feature Engineering Machine Learning NumPyTranscript
In your Jupyter Notebook, use the where function to define a new investor data tier change series.
In any case, where prior tier is equal to invite tier return none. Those two values are the same, so we want to indicate that there was no tier change.
Otherwise you're going to use another where function to say, where prior tier is participant, then return promoted.
We've already covered the case where both tiers are the same.
So if prior tier is participant and the next one is not the same, that means the next one is bookrunner, which means that that investor was promoted.
Otherwise, if neither of those two cases are true, that means that the first tier was bookrunner.
And the second one is different, meaning the second one was participant, which means they were demoted.
So in that case, return demoted.
Once you've built that logic using the where function and created the new tier change series, display the first three rows of your data frame to verify that everything was done correctly.
To do that, we're gonna start by defining a new series name called tier change, and we're gonna set that equal to this conditional logic using the NumPy where function.
So start by NumPy where, and we could write this all in one line.
I've broken it up into multiple lines just for readability, but it's not absolutely necessary to do that.
So we're gonna start with this one NumPy where function, and the first argument is going to be the condition to meet.
So our condition is when the prior tier is equal to the invite tier, meaning that there's been no change from the prior transaction, then we want to show none.
There has been no tier change.
Otherwise we want to insert another where inside.
It's nested inside our first wear function.
So the condition to meet for the second wear function is if the prior tier is participant. So we know at this point we've already covered the case where we're the same.
So in this case, if the prior tier is participant, that means that the invite tier must be bookrunner, meaning that the investor was promoted.
So if that is true, then the next argument says return promoted.
If that's false, that means that the investor started as a bookrunner and then was demoted to participant.
And so if it's false, we want to show demoted and then we come back here, close the nested where function, and then close the original where function.
And that's gonna be the definition of our new tier change series.
And then to confirm that we did this correctly, we're just displaying the first three rows using the head function, and you should get the same results that you see right here.