Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
There are a ton of routines that are already written for you, saving the work for you. Let's take a look at how to find them. Also let's dive into reduction a bit.
Learn More
My Notes for Universal Functions
## Universal Functions
* [ufuncs](https://docs.scipy.org/doc/numpy/reference/ufuncs.html) are commonly needed vectorized functions
* Vectorized functions allow you to operate element by element without using a loop
* The standard math and comparison operations have all been overloaded so that they can make use of vectorization
* Values can be [broadcasted](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html), or stretched to be applied to the ufuncs.

0:00
So here's my notes on ufuncs, or universal functions.

0:04
They are commonly needed in vectorized functions, again,

0:07
which allow you to operate element by element instead of using a loop, and

0:12
standard mathing comparison operators like plus, minus, multiply, and

0:16
greater than, greater than equal to they've all been overloaded so

0:20
that they can make use of vectorization, and values can be broadcasted or

0:24
stretched to be applied to the vector.

0:27
So, remember that two got stretched all the way across the scaler, or

0:30
we did it by rows.

0:31
Awesome, so we saw some super powerful ufuncs, and let's go take a look at

0:36
some higher level routines that make use of them for common tasks.

0:41
Now, all this talk of trigonometry is making me want to go back and

0:44
take a look at one of those first multidimensional arrays that we created,

0:49
that students_gpas.

0:51
That was way up here at the top, wasn't it?

0:53
Let's get back up here.

0:56
We've done a lot in this course.

0:58
All right, so, here we go.

1:00
Here's our students_gpas.

1:01
Let's go ahead, let's take a look again one more time at what that is.

1:06
So we'll say students_gpas.

1:13
Right, so the zero if row of this is me,

1:16
and then we had Vlada, and then we had Quesy.

1:21
Awesome.

1:23
One thing that we can do is we can find out the average or mean of this data.

1:28
So the way that you do that is just call a function on it.

1:32
Say students_gpas.mean.

1:35
Whoops.

1:37
[LAUGH] That returned all of our scores averaged together,

1:41
which 3.805 is not bad for our cohort average,

1:44
however, I was hoping to get the mean of each row of these students.

1:49
Now, the great news is that there is an access argument that we can pass and

1:54
it will do what we want.

1:57
The parameter though has been known to trip people up, so

2:00
let's focus a bit on the issue.

2:01
So, we have a twodimensional array.

2:05
Our first dimension is students, and

2:08
our second dimension is of GPA by year in school.

2:12
So we want to have the mean of the second dimension.

2:19
We want this dimension, this is what we want, the gpas is what we want.

2:23
So that would be axis one.

2:26
Remember that they are zero based, so it's access zero, is the other way, so

2:29
access one is this.

2:30
So let's go ahead and do that.

2:32
Let's say, the students_gpas.mean(axis=1).

2:43
And since we've got three results here, and we only have three students,

2:46
we know that it did the right thing.

2:47
It went across and did the average there, so there is 3.69.

2:50
Let's go ahead and say 3.7, and then there's 3.75 and

2:55
3.97, and by the way, that 3.7 didn't really mean anything.

3:00
Now a common mistake is that people think that they want to work with each row, so

3:06
they choose the axis zero, but really what happens with axis zero, lets go ahead and

3:13
do that, we'll say (axis=0), is it ends up going this way, right?

3:18
So it's averaging axis this way, cuz it's reducing the values.

3:24
It's summing all these values up, but we want to go this way, so

3:26
when you think about the axis, remember it's what direction you're moving in.

3:30
Totally common hiccup.

3:32
Just remember to imagine the function happening across the dimension.

3:36
Now you might want this sometimes though right?

3:40
This (axis=0) will give you the average of all students by year.

3:45
That's what you want, and then if you want to you can do (axis=1), and

3:50
it gives you average of all years by student.

3:55
This type of function is known as a reduction operation.

3:58
The function reduces a set of values down to one.

4:02
The concept is that there is a function that takes two values,

4:04
a total value of all operations and the next value in the array like object.

4:11
It performs the operation and

4:12
returns the total to be used in the next iteration, recursively.

4:16
It might sound complicated, but it's actually what you would do in your head if

4:20
I asked you to add up all the values in this list.

4:22
It's probably easier to just see it in action, so let's do it.

4:26
All functions that are ufuncs, have the ability to do this, built into it.

4:31
Here, let's go back down to the hundred days of code study minutes list.

4:36
Where is this at?

4:37
Let's go down to where we have the very last one of them.

4:44
Here we go, study_minutes list, here we go.

4:50
All right, so I'm going to add one below this.

4:55
Remember that our study_minutes array is a two dimensional array.

4:58
The first dimension represents rounds or attempts, and

5:02
the second dimension is the minutes per day, and there are 100 days, so

5:06
let's simplify things first by using a single dimension.

5:09
I'm gonna grab the first round here, so we'll say study_minutes[0].

5:15
Now, if I asked you to total these minutes up, I bet you'd just start adding, and

5:20
remembering like this.

5:21
You'd say okay, so 150 + 60, that's 210, and

5:26
now I go 210 + 80, that's 290, and then I take the total of 290 and

5:32
I add 60 to get 350, and so on, and so on, and so on.

5:36
That is reducing in a nutshell.

5:39
If we continue all the way through the array, we'll have a total.

5:42
Now, I said that all ufuncs had the ability to do this reduction, and

5:46
the way they provide this functionality is by exposing some functions

5:50
off of the ufunc itself.

5:52
That sentence was pretty funky.

5:54
What we were doing was adding all the values up.

5:57
In that case, the ufunc that we would like to use is add.

6:01
So, let's do it.

6:03
So we'll say np.add.reduce, and

6:08
then we'll pass in our array.

6:14
And there it is, 440, and it did just like we were doing.

6:19
If you want to actually see each step, there is a function for

6:22
that available too on each ufunc.

6:24
So np.add.accumulate, and this will show you each step through.

6:27
So if we do, again, if we do study_minutes[0],

6:36
we'll see that we have 150, 210, 290, 350, and

6:39
then actually you'll see all of the zero adds that we had to do, and

6:45
yikes, you can see the waste of time that we made this do by adding all the zeros.

6:49
We could have filtered them out.

6:50
More in the teacher's notes.

6:51
Now, we want to get the sum of all these values together and

6:55
there is of course a routine that's super common, and it is called, sum.

7:00
So if we just make this, well let's make a new one,

7:04
we'll save that there for us, np.sum(study_minutes[0]).

7:10
We'll see that we get 440, which is exactly what we did when we did

7:14
the reduce, and the reduction works on multi dimensions as well.

7:19
So we can just say np.sum(study_minutes), and

7:23
it will get, wow, 10,000 hours, must be a pro.

7:28
I think that's what Macklemore said, or Malcom Gladwell,

7:31
I can't remember which one said that.

7:33
Reduction functions will almost always define an access parameter.

7:38
So in this case we want to see the sum of all minutes by round.

7:42
So, that's axis=1, And

7:48
there we know that we did it right, because there are three results turn back,

7:51
and 440 was what we get out when we're getting for the first one.

7:54
Awesome.

7:55
Pretty handy, right?

7:57
And as you can imagine that mean function that we were just using is probably

8:02
using this sum function under the covers since, to calculate the mean, what

8:07
you do is you add all of the values and then divide by the total amount of values.

8:11
But, what's nice is that you don't need to remember that formula.

8:16
Even though it is simple,

8:18
it's been extracted away from you by simply calling the mean function.

8:22
You'll find that there are lots of formulas extracted away from you in

8:26
the library.

8:27
In fact, let's pop over real quick to another popular page in the documentation.

8:32
I'm just gonna Google statistics numpy.

8:40
Here we go.

8:42
There are tons of functions available for you here.

8:49
Since it's statistics, a bunch of these are reductionbased.

8:52
They reduce all the values down to one, and here's one that you'll see everywhere,

8:57
std, and while it's actually known to spread itself around, it's short for

9:01
standard deviation.

9:03
Here, let's pop in, so

9:04
it computes the standard deviation along the specified access.

9:08
The measure of the spread of a distribution, which is great.

9:12
And if we scroll down here in the notes,

9:15
we can see that this is what has been calculated.

9:19
This is the formula.

9:21
Now, I kinda remember doing that in math class,

9:23
but the point is here, you don't need to know how to calculate it.

9:28
You want to a why to use it,

9:30
as we discussed when we first introduced the grade point averages or GPAs.

9:34
People struggle with math concepts when they are first introduced to them, and

9:38
I'm under the belief it's the memorization of the formula that most people

9:43
struggle with.

9:44
Typically, that's what you're tested on,

9:46
not the actual way to use the function in the real world.

9:49
Current learning science says that if you don't use it, you will lose it.

9:53
So if these equations feel a bit rusty and you haven't used them recently,

9:57
don't fret, you're brain is just working correctly.

10:00
Most of the time, you hardly get a chance to see why in your math class.

10:04
You just focused on the how.

10:06
So with that said, let's pop up a couple levels here in our bookmarks to Routines.

10:14
This page here is a really great overview of how powerful this library is,

10:20
and a great look at some common abstractions.

10:23
So if we look down here, here is some Discreet Fourier Transforms.

10:30
Here's some financial functions, linear algebra,

10:35
input and output, logic functions,

10:39
polynomials, statistics, there's a lot in here.

10:44
Remember, you don't need to know all of these,

10:47
just be aware that what you are trying to do most likely already exist.

10:52
As you can see,

10:52
there are tons of directions that you can head with this library.

10:56
So stand on the shoulders of giants who built things out for you.

11:01
We talked way back when about how all sorts of different libraries accept and

11:06
return numpy arrays.

11:08
Let's take a quick break and

11:09
take a look at one common use case, plotting values on a graph.

11:14
Well, that is, right after we jot down some notes.

11:17
Why don't you talk a bit about some common routines that you saw, and

11:20
talk a bit about reduction.
You need to sign up for Treehouse in order to download course files.
Sign up