Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Introduction to NumPy!
      
    
You have completed Introduction to NumPy!
Preview
    
      
  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 
matrixdatatype. This is for historical reasons. Learn more about array vs. matrix 
My Notes from Manipulation
## Array Manipulation
* The documentation on [Array Manipulation](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      [MUSIC]
                      0:00
                    
                    
                      You've done an excellent job
learning all about NumPy arrays.
                      0:04
                    
                    
                      You can create, inspect, and modify them.
                      0:08
                    
                    
                      You can manipulate them into new shapes,
and
                      0:10
                    
                    
                      you can filter out
the values you don't want.
                      0:12
                    
                    
                      And now, I'd like to take some to
show off some common use cases for
                      0:14
                    
                    
                      these arrays that we've
been getting to know.
                      0:17
                    
                    
                      But before we dive in here,
                      0:20
                    
                    
                      I'd like to take a second to discuss
a phenomenon known as imposter syndrome.
                      0:21
                    
                    
                      Imposter syndrome happens when
you doubt your skills, and
                      0:28
                    
                    
                      you feel like you really aren't a pro.
                      0:30
                    
                    
                      You feel like a fraud.
                      0:32
                    
                    
                      You feel like everyone
else knows more than you.
                      0:33
                    
                    
                      It's an unfortunate and
quite common feeling, and
                      0:36
                    
                    
                      I want you to know that you
aren't alone in experiencing it.
                      0:38
                    
                    
                      And I especially want you to know
that you aren't actually a fraud and
                      0:41
                    
                    
                      that you can do this.
                      0:45
                    
                    
                      Because of the numerous used cases
of NumPy arrays, you're bound to
                      0:46
                    
                    
                      see lots of math equations and
formulas that you don't yet understand.
                      0:49
                    
                    
                      You might have learned once and
then forgot.
                      0:54
                    
                    
                      What happens is you think,
man I only know this much stuff,
                      0:56
                    
                    
                      look how much everyone else knows.
                      0:59
                    
                    
                      But here's the thing,
very few people know it all.
                      1:01
                    
                    
                      They all know their own little bit,
and you can and
                      1:04
                    
                    
                      will learn the extra little bits
when you actually need them.
                      1:08
                    
                    
                      This is important to remember.
                      1:10
                    
                    
                      That feeling is bound to happen to you and
I just want you to be ready for it.
                      1:12
                    
                    
                      Approach it like, hey, I'm not sure how
to do that, yet, cuz you will learn it.
                      1:16
                    
                    
                      And look at you,
you're doing great already.
                      1:20
                    
                    
                      I want you to approach things
with the growth mindset.
                      1:23
                    
                    
                      Check the teacher's notes for
more of that.
                      1:25
                    
                    
                      We are going to be doing a different
type of programming than we've been
                      1:27
                    
                    
                      doing up until now on our
Python journey together.
                      1:30
                    
                    
                      We are going to be doing what
is known as Array Programming.
                      1:33
                    
                    
                      Instead of our typical way of looping
through a collection of data and
                      1:36
                    
                    
                      pulling out values one by
one to perform operations,
                      1:40
                    
                    
                      what we do in array programming
is to perform the operation on
                      1:42
                    
                    
                      the entire set of values,
all at once, in one fell swoop.
                      1:45
                    
                    
                      Not only is it faster, it's also
expressed much more concisely in code.
                      1:49
                    
                    
                      Array programming code often ends up
looking a lot like a math equation.
                      1:54
                    
                    
                      We've talked about how an array
is referred to as a vector and
                      1:57
                    
                    
                      a two dimensional array as a matrix.
                      2:01
                    
                    
                      Well the reason for
                      2:03
                    
                    
                      that is because we use these arrays to
represent their mathematical equivalent.
                      2:04
                    
                    
                      It's probably a little too abstract.
                      2:09
                    
                    
                      Let me show you what I
mean by that statement.
                      2:11
                    
                    
                      Well, I've got an idea,
let's do some linear algebra.
                      2:13
                    
                    
                      Why the long face?
                      2:17
                    
                    
                      That doesn't sound fun?
                      2:18
                    
                    
                      Did you forget how to do it too?
                      2:20
                    
                    
                      Don't worry.
                      2:21
                    
                    
                      All we have to do is set up the equation
properly using the correct layout, and
                      2:22
                    
                    
                      NumPy is going to do all of the work for
us.
                      2:27
                    
                    
                      I had a moment recently where I had to
take back one of those famous quips that
                      2:29
                    
                    
                      we all make at one point in our lives,
regarding stuff we're learning.
                      2:32
                    
                    
                      You know the one.
                      2:36
                    
                    
                      When am I ever going to need this?
                      2:36
                    
                    
                      Well, probably not very surprising to you
by now, but this example involves tacos.
                      2:39
                    
                    
                      There's a new taco food cart that
just opened by our office, and
                      2:43
                    
                    
                      I like to share my love of
tacos with my coworkers.
                      2:46
                    
                    
                      So I often offer to do pickups for
the office.
                      2:49
                    
                    
                      And if you've ever been in charge of
a taco run, you know that one of the main
                      2:52
                    
                    
                      problems is that you're never quite
sure of the price of the items.
                      2:55
                    
                    
                      So I'm usually just like, just pay me
later, I'll put on my credit card.
                      2:58
                    
                    
                      And sometimes determining that
price from the total is easy.
                      3:02
                    
                    
                      Like in the case where you have
two tacos and the total is $3.
                      3:05
                    
                    
                      You can do that kind of math in your head,
right?
                      3:08
                    
                    
                      So you might not even know how you did it,
but
                      3:11
                    
                    
                      you know that a single taco costs $1.50.
                      3:13
                    
                    
                      That's pretty simple if that
was the only item in the order,
                      3:16
                    
                    
                      but that's never the case, right?
                      3:19
                    
                    
                      So I know one of the larger
orders I bought.
                      3:21
                    
                    
                      It was four tacos, one burrito,
two horchatas and two Mexican cokes,
                      3:23
                    
                    
                      and the total was $20.50.
                      3:26
                    
                    
                      Nothing is better than a Mexi-Coke
in a bottle, real sugar.
                      3:28
                    
                    
                      With that information though,
                      3:34
                    
                    
                      I have no idea how to figure
out the price breakdown.
                      3:35
                    
                    
                      It's definitely not as that two tacos
equation that we did in our head.
                      3:38
                    
                    
                      And this is because there are so
many other variables now.
                      3:41
                    
                    
                      But I seem to remember from math,
that if you have enough equations,
                      3:44
                    
                    
                      you should be able to solve them.
                      3:48
                    
                    
                      So let's see, do I have more data?
                      3:50
                    
                    
                      [LAUGH] Of course I do.
                      3:51
                    
                    
                      I went once, and I got a burrito and
a Mexi-Coke, and that was 10 bucks.
                      3:53
                    
                    
                      And another office pick,
if I get six tacos, one horchata,
                      3:57
                    
                    
                      and two Mexi-Cokes, and that was $14.25.
                      4:00
                    
                    
                      Now, one thing I remember from math is
we're going to need to make sure that
                      4:03
                    
                    
                      everything is lined up.
                      4:07
                    
                    
                      That means I need to make
sure that I enter a 0 for
                      4:08
                    
                    
                      all the items I didn't purchase, and
we get some nice looking equations here.
                      4:11
                    
                    
                      Now, if that doesn't look
like a math equation to you,
                      4:15
                    
                    
                      why don't we change those variables
to their typical looking form.
                      4:18
                    
                    
                      Don't let those freak you out though.
                      4:21
                    
                    
                      Remember, they're just representing
tacos and burritos and whatnot.
                      4:23
                    
                    
                      So, really this left side here
kind of looks like rows and
                      4:26
                    
                    
                      columns almost, doesn't it?
                      4:28
                    
                    
                      In fact, let's make the type
of food the heading here.
                      4:30
                    
                    
                      And what we have left here is the number
of items, or the coefficient.
                      4:33
                    
                    
                      In mathematics, to denote a matrix,
you will see hard brackets like this.
                      4:37
                    
                    
                      And for our totals, we can consider
this as a single dimensional array,
                      4:41
                    
                    
                      or as it's called, a vector.
                      4:45
                    
                    
                      A row base vector to be precise.
                      4:47
                    
                    
                      And with our systems of equations as
a matrix, and our totals as a vector,
                      4:50
                    
                    
                      we should be able to solve for it.
                      4:53
                    
                    
                      There are definitely rules and
                      4:56
                    
                    
                      ways to go about solving that
system of linear equations.
                      4:57
                    
                    
                      But what do you say we let
NumPy do the work for us?
                      5:00
                    
                    
                      In the teacher notes,
attached to this video,
                      5:03
                    
                    
                      I've added the equations
to create our matrix.
                      5:05
                    
                    
                      So I'm gonna copy and
paste it here in this cell.
                      5:07
                    
                    
                      So here is our orders array,
and that's our matrix, right,
                      5:10
                    
                    
                      that's what it looks like.
                      5:13
                    
                    
                      It's an array with a rank of two,
it's got two dimensions, and
                      5:14
                    
                    
                      each row here represents an order, right?
                      5:17
                    
                    
                      And this is the number of tacos,
this first column here is tacos, right.
                      5:21
                    
                    
                      And then the next one is,
next one being burritos, and
                      5:27
                    
                    
                      then horchata, and coke, right.
                      5:32
                    
                    
                      And now we need to get
our constant prices.
                      5:35
                    
                    
                      So that's also in the teacher's notes.
                      5:38
                    
                    
                      Let me just paste that here.
                      5:39
                    
                    
                      So these are the totals
of what came across.
                      5:40
                    
                    
                      Scroll this up a little bit.
                      5:47
                    
                    
                      Here we go, so we get our $3, 20.50 for
the second order, 10, and 14.25.
                      5:49
                    
                    
                      So really what we've done here is
to build the math equation, right?
                      5:54
                    
                    
                      Well, now for the awesome part.
                      6:00
                    
                    
                      We don't actually need to know how to
solve this, we just need to know that it's
                      6:01
                    
                    
                      a system of linear equations
that we're trying to solve for.
                      6:05
                    
                    
                      And we also need to know
that there is a module in
                      6:08
                    
                    
                      the NumPy library for linear algebra.
                      6:12
                    
                    
                      It's shortened to linalg.
                      6:16
                    
                    
                      And here is the beautiful method.
                      6:17
                    
                    
                      Solve, and
we pass [LAUGH] in our coefficient matrix,
                      6:22
                    
                    
                      which was our orders, and
we also pass it our totals.
                      6:24
                    
                    
                      So we say totals like so.
                      6:29
                    
                    
                      And then let's go ahead and
take a look at what prices is.
                      6:31
                    
                    
                      [LAUGH] Look at that,
$1.5 tacos, we knew that,
                      6:34
                    
                    
                      8 bucks burritos, $1.25 horchatas,
and a Mexican Coke for 2 bucks.
                      6:39
                    
                    
                      [LAUGH] Please note here that coke is
more expensive than a taco already.
                      6:44
                    
                    
                      But well worth it.
                      6:49
                    
                    
                      Wait second, if we have this solution,
                      6:50
                    
                    
                      I think we should be able to verify it,
right.
                      6:53
                    
                    
                      I mean, if we just plugged in
these prices to the equations,
                      6:55
                    
                    
                      it should produce a total, right?
                      6:59
                    
                    
                      Shouldn't it?
                      7:02
                    
                    
                      So let's see.
                      7:03
                    
                    
                      Naturally in Python, I guess we would just
loop through the matrix and then loop over
                      7:04
                    
                    
                      the prices and multiply them together,
and then we'd add them together, right?
                      7:09
                    
                    
                      And that should produce the price.
                      7:15
                    
                    
                      Now, we could definitely do that,
but let's remember that
                      7:17
                    
                    
                      one of the benefits of array programming
is that we don't need to rely on loops.
                      7:21
                    
                    
                      Also, if there's a mathematical
way to do something,
                      7:25
                    
                    
                      most likely there's
a way to do it in NumPy.
                      7:27
                    
                    
                      Now what we are talking about here,
multiplying matrices with vectors, and
                      7:29
                    
                    
                      then summing them together is
actually a common math practice.
                      7:33
                    
                    
                      It is referred to as the inner product,
and it's usually represented with a dot.
                      7:36
                    
                    
                      So you would say, A.B, that's exactly
what we would've done in a loop but
                      7:40
                    
                    
                      expressed in mathematical terms,
the notation, right?
                      7:46
                    
                    
                      So in Python though, that dot that I just
created, that doesn't have any meaning.
                      7:50
                    
                    
                      So instead,
we can use the at sign, like so.
                      7:53
                    
                    
                      So if we say orders @ prices,
                      7:58
                    
                    
                      let's see what happens.
                      8:03
                    
                    
                      So check that out.
                      8:07
                    
                    
                      That is what our totals are.
                      8:08
                    
                    
                      Look at that, it adds up.
                      8:09
                    
                    
                      So it took these and
                      8:10
                    
                    
                      ran them through the equations and
ended up with the same totals.
                      8:11
                    
                    
                      So we know that it works.
                      8:15
                    
                    
                      And so really this is shorthand for
                      8:17
                    
                    
                      orders.dot (prices),
see it's the same thing.
                      8:20
                    
                    
                      Pretty cool how we can use these shortcuts
to build and solve equations, right?
                      8:25
                    
                    
                      It's super powerful, and
                      8:30
                    
                    
                      I'm glad that you have all those array
manipulation powers in your bag of tricks.
                      8:31
                    
                    
                      As I know you're aware,
                      8:36
                    
                    
                      linear algebra has many more applications
than just taco price calculation.
                      8:37
                    
                    
                      It's great for times, like we just saw,
when you're trying to solve for
                      8:41
                    
                    
                      missing variables in a bunch of equations.
                      8:45
                    
                    
                      And remember, this is just one formula.
                      8:47
                    
                    
                      There are so many more.
                      8:50
                    
                    
                      And many of those formulas
that are available for
                      8:52
                    
                    
                      you to use will most
likely not be familiar.
                      8:54
                    
                    
                      Remember, that's okay.
                      8:57
                    
                    
                      No one knows them all.
                      8:59
                    
                    
                      You'll learn them as you need them.
                      9:00
                    
                    
                      Don't let the options overwhelm you.
                      9:02
                    
                    
                      Let them make you feel empowered.
                      9:03
                    
                    
                      You don't need to know how to solve them
yourself, and that should feel great.
                      9:05
                    
                    
                      That linalg.solve function
is a bit of an abstraction.
                      9:10
                    
                    
                      It really is doing all that work that
you would have had to do by hand,
                      9:13
                    
                    
                      like you might have done in math class.
                      9:16
                    
                    
                      It's very similar to how we
could've written that dot loop.
                      9:18
                    
                    
                      There's actually a function that
does all the heavy lifting for us.
                      9:22
                    
                    
                      What do you say we take a quick look at
some more common vector based operations
                      9:25
                    
                    
                      that you'll encounter as you progress.
                      9:29
                    
                    
                      Let me show you off NumPy's
universal functions or ufuncs.
                      9:31
                    
                    
                      First though, let's take a quick break.
                      9:36
                    
                    
                      And why don't you jot down some thoughts
and notes about what we just accomplished,
                      9:37
                    
                    
                      all without using the loops.
                      9:41
                    
                    
                      Yeah, quick reminder, make sure to check
out the teacher's notes on this video.
                      9:42
                    
                    
                      I've dropped a lot of information about
where to learn more about linear algebra,
                      9:46
                    
                    
                      if that got you re-invigorated.
                      9:49
                    
                    
                      And like I said,
it's no big deal if it didn't.
                      9:51
                    
                    
                      There are many more use cases for NumPy.
                      9:54
                    
                    
                      See you soon and we'll compare notes.
                      9:57
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up