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
Let's take a look at a detailed example that uses the new array programming paradigm. We'll have NumPy do some linear algebra for us!
Code
orders = np.array([
[2, 0, 0, 0],
[4, 1, 2, 2],
[0, 1, 0, 1],
[6, 0, 1, 2]
])
totals = np.array([3, 20.50, 10, 14.25])
Learn More
 Build a Learning Mindset
 Wikipedia  Array Programming
 Khan Academy  Linear Algebra <== Wonderful courses
 NumPy also has a
matrix
datatype. This is for historical reasons. Learn more aboutarray
vs.matrix
My Notes from Manipulation
## Array Manipulation
* The documentation on [Array Manipulation](https://docs.scipy.org/doc/numpy/reference/routines.arraymanipulation.html) is a good one to keep bookmarked.
* `ndarray.reshape` creates a view with a new shape
* You can use `1` as a value to infer the missing dimension
* `ndarray.ravel` returns a single dimensional view of the array.
* `ndarray.flatten` can be used to make a single dimensional copy.
* `np.lookfor` is great for searching docstrings from within a notebook.

0:00
[MUSIC]

0:04
You've done an excellent job learning all about NumPy arrays.

0:08
You can create, inspect, and modify them.

0:10
You can manipulate them into new shapes, and

0:12
you can filter out the values you don't want.

0:14
And now, I'd like to take some to show off some common use cases for

0:17
these arrays that we've been getting to know.

0:20
But before we dive in here,

0:21
I'd like to take a second to discuss a phenomenon known as imposter syndrome.

0:28
Imposter syndrome happens when you doubt your skills, and

0:30
you feel like you really aren't a pro.

0:32
You feel like a fraud.

0:33
You feel like everyone else knows more than you.

0:36
It's an unfortunate and quite common feeling, and

0:38
I want you to know that you aren't alone in experiencing it.

0:41
And I especially want you to know that you aren't actually a fraud and

0:45
that you can do this.

0:46
Because of the numerous used cases of NumPy arrays, you're bound to

0:49
see lots of math equations and formulas that you don't yet understand.

0:54
You might have learned once and then forgot.

0:56
What happens is you think, man I only know this much stuff,

0:59
look how much everyone else knows.

1:01
But here's the thing, very few people know it all.

1:04
They all know their own little bit, and you can and

1:08
will lean the extra little bits when you actually need them.

1:10
This is important to remember.

1:12
That feeling is bound to happen to you and I just want you to be ready for it.

1:16
Approach it like, hey, I'm not sure how to do that, yet, cuz you will learn it.

1:20
And look at you, you're doing great already.

1:23
I want you to approach things with the growth mindset.

1:25
Check the teacher's notes for more of that.

1:27
We are going to be doing a different type of programming than we've been

1:30
doing up until now on our Python journey together.

1:33
We are going to be doing what is known as Array Programming.

1:36
Instead of our typical way of looping through a collection of data and

1:40
pulling out values one by one to perform operations,

1:42
what we do in array programming is to perform the operation on

1:45
the entire set of values, all at once, in one fell swoop.

1:49
Not only is it faster, it's also expressed much more concisely in code.

1:54
Array programming code often ends up looking a lot like a math equation.

1:57
We've talked about how an array is referred to as a vector and

2:01
a two dimensional array as a matrix.

2:03
Well the reason for

2:04
that is because we use these arrays to represent their mathematical equivalent.

2:09
It's probably a little too abstract.

2:11
Let me show you what I mean by that statement.

2:13
Well, I've got an idea, let's do some linear algebra.

2:17
Why the long face?

2:18
That doesn't sound fun?

2:20
Did you forget how to do it too?

2:21
Don't worry.

2:22
All we have to do is set up the equation properly using the correct layout, and

2:27
NumPy is going to do all of the work for us.

2:29
I had a moment recently where I had to take back one of those famous quips that

2:32
we all make at one point in our lives, regarding stuff we're learning.

2:36
You know the one.

2:36
When am I ever going to need this?

2:39
Well, probably not very surprising to you by now, but this example involves tacos.

2:43
There's a new taco food cart that just opened by our office, and

2:46
I like to share my love of tacos with my coworkers.

2:49
So I often offer to do pickups for the office.

2:52
And if you've ever been in charge of a taco run, you know that one of the main

2:55
problems is that you're never quite sure of the price of the items.

2:58
So I'm usually just like, just pay me later, I'll put on my credit card.

3:02
And sometimes determining that price from the total is easy.

3:05
Like in the case where you have two tacos and the total is $3.

3:08
You can do that kind of math in your head, right?

3:11
So you might not even know how you did it, but

3:13
you know that a single taco costs $1.50.

3:16
That's pretty simple if that was the only item in the order,

3:19
but that's never the case, right?

3:21
So I know one of the larger orders I bought.

3:23
It was four tacos, one burrito, two horchatas and two Mexican cokes,

3:26
and the total was $20.50.

3:28
Nothing is better than a MexiCoke in a bottle, real sugar.

3:34
With that information though,

3:35
I have no idea how to figure out the price breakdown.

3:38
It's definitely not as that two tacos equation that we did in our head.

3:41
And this is because there are so many other variables now.

3:44
But I seem to remember from math, that if you have enough equations,

3:48
you should be able to solve them.

3:50
So let's see, do I have more data?

3:51
[LAUGH] Of course I do.

3:53
I went once, and I got a burrito and a MexiCoke, and that was 10 bucks.

3:57
And another office pick, if I get six tacos, one horchata,

4:00
and two MexiCokes, and that was $14.25.

4:03
Now, one thing I remember from math is we're going to need to make sure that

4:07
everything is lined up.

4:08
That means I need to make sure that I enter a 0 for

4:11
all the items I didn't purchase, and we get some nice looking equations here.

4:15
Now, if that doesn't look like a math equation to you,

4:18
why don't we change those variables to their typical looking form.

4:21
Don't let those freak you out though.

4:23
Remember, they're just representing tacos and burritos and whatnot.

4:26
So, really this left side here kind of looks like rows and

4:28
columns almost, doesn't it?

4:30
In fact, let's make the type of food the heading here.

4:33
And what we have left here is the number of items, or the coefficient.

4:37
In mathematics, to denote a matrix, you will see hard brackets like this.

4:41
And for our totals, we can consider this as a single dimensional ray,

4:45
or as it's called, a vector.

4:47
A row base vector to be precise.

4:50
And with our systems of equations as a matrix, and our totals as a vector,

4:53
we should be able to solve for it.

4:56
There are definitely rules and

4:57
ways to go about solving that system of linear equations.

5:00
But what do you say we let NumPy do the work for us?

5:03
In the teacher notes, attached to this video,

5:05
I've added the equations to create our matrix.

5:07
So I'm gonna copy and paste it here in this cell.

5:10
So here is our orders array, and that's our matrix, right,

5:13
that's what it looks like.

5:14
It's an array with a rank of two, it's got two dimensions, and

5:17
each row here represents an order, right?

5:21
And this is the number of tacos, this first column here is tacos, right.

5:27
And then the next one is, next one being burritos, and

5:32
then horchata, and coke, right.

5:35
And now we need to get our constant prices.

5:38
So that's also in the teacher's notes.

5:39
Let me just paste that here.

5:40
So these are the totals of what came across.

5:47
Scroll this up a little bit.

5:49
Here we go, so we get our $3, 20.50 for the second order, 10, and 14.25.

5:54
So really what we've done here is to build the math equation, right?

6:00
Well, now for the awesome part.

6:01
We don't actually need to know how to solve this, we just need to know that it's

6:05
a system of linear equations that we're trying to solve for.

6:08
And we also need to know that there is a module in

6:12
the NumPy library for linear algebra.

6:16
It's shortened to linalg.

6:17
And here is the beautiful method.

6:22
Solve, and we pass [LAUGH] in our coefficient matrix,

6:24
which was our orders, and we also pass it our totals.

6:29
So we say totals like so.

6:31
And then let's go ahead and take a look at what prices is.

6:34
[LAUGH] Look at that, $1.5 tacos, we knew that,

6:39
8 bucks burritos, $1.25 horchatas, and a Mexican Coke for 2 bucks.

6:44
[LAUGH] Please note here that coke is more expensive than a taco already.

6:49
But well worth it.

6:50
Wait second, if we have this solution,

6:53
I think we should be able to verify it, right.

6:55
I mean, if we just plugged in these prices to the equations,

6:59
it should produce a total, right?

7:02
Shouldn't it?

7:03
So let's see.

7:04
Naturally in Python, I guess we would just loop through the matrix and then loop over

7:09
the prices and multiply them together, and then we'd add them together, right?

7:15
And that should produce the price.

7:17
Now, we could definitely do that, but let's remember that

7:21
one of the benefits of array programming is that we don't need to rely on loops.

7:25
Also, if there's a mathematical way to do something,

7:27
most likely there's a way to do it in NumPy.

7:29
Now what we are talking about here, multiplying matrices with vectors, and

7:33
then summing them together is actually a common math practice.

7:36
It is referred to as the inner product, and it's usually represented with a dot.

7:40
So you would say, A.B, that's exactly what we would've done in a loop but

7:46
expressed in mathematical terms, the notation, right?

7:50
So in Python though, that dot that I just created, that doesn't have any meaning.

7:53
So instead, we can use the at sign, like so.

7:58
So if we say orders @ prices,

8:03
let's see what happens.

8:07
So check that out.

8:08
That is what our totals are.

8:09
Look at that, it adds up.

8:10
So it took these and

8:11
ran them through the equations and ended up with the same totals.

8:15
So we know that it works.

8:17
And so really this is shorthand for

8:20
orders.dot (prices), see it's the same thing.

8:25
Pretty cool how we can use these shortcuts to build and solve equations, right?

8:30
It's super powerful, and

8:31
I'm glad that you have all those array manipulation powers in your bag of tricks.

8:36
As I know you're aware,

8:37
linear algebra has many more applications than just taco price calculation.

8:41
It's great for times, like we just saw, when you're trying to solve for

8:45
missing variables in a bunch of equations.

8:47
And remember, this is just one formula.

8:50
There are so many more.

8:52
And many of those formulas that are available for

8:54
you to use will most likely not be familiar.

8:57
Remember, that's okay.

8:59
No one knows them all.

9:00
You'll learn them as you need them.

9:02
Don't let the options overwhelm you.

9:03
Let them make you feel empowered.

9:05
You don't need to know how to solve them yourself, and that should feel great.

9:10
That linalg.solve function is a bit of an abstraction.

9:13
It really is doing all that work that you would have had to do by hand,

9:16
like you might have done in math class.

9:18
It's very similar to how we could've written that dot loop.

9:22
There's actually a function that does all the heavy lifting for us.

9:25
What do you say we take a quick look at some more common vector based operations

9:29
that you'll encounter as you progress.

9:31
Let me show you off NumPy's universal functions or ufuncs.

9:36
First though, let's take a quick break.

9:37
And why don't you jot down some thoughts and notes about what we just accomplished,

9:41
all without using the loops.

9:42
Yeah, quick reminder, make sure to check out the teacher's notes on this video.

9:46
I've dropped a lot of information about where to learn more about linear algebra,

9:49
if that got you reinvigorated.

9:51
And like I said, it's no big deal if it didn't.

9:54
There are many more use cases for NumPy.

9:57
See you soon and we'll compare notes.
You need to sign up for Treehouse in order to download course files.
Sign up