- Read Tutorial
- Watch Guide Video
Right here I have opened in the browser the python package index and this is also called the Cheese Shop. If you scroll all the way down you can see that there is a little reference right down here to an old Monty Python sketch related to a cheese shop.
And that is how the python package index got its name. If you're curious about it and you want to know more just google Python package index Cheese Shop and you'll be able to see the entire Monty Python sketch.
Now with all that being said let's actually get into the code. So what I want to do is I want to show you how you can find and then learn more about different packages that are available on this index. The very first one that we're going to go with is called NumPy. And so what NumPy is, is its a very powerful package that allows you to process numbers, records, and objects like you can see right here.
This is a basic description. It also gives an associated weight and the weight has to deal with a number of criteria. Usually, the popularity of it has quite a bit to do with it, so if you click on this. This is one of the most popular libraries in the entire python community. So it is definitely a good one to become familiar with and here on the show page, you can see that there is a much longer description.
Essentially, I'll give you a summary NumPy allows you to process large collections of data in a very efficient manner. So usually things that would take you many lines of code to write numPy has those processes built directly in the library and you can simply call them and then use them in your own programs. This is something that you're going to find incredibly helpful when you start implementing machine learning algorithms or when you start building out complex APIs that deal with large collections of complex data NumPy is going to be a very good friend for you.
So the way that we can install it is with pip if you did not install pip or you do not want to you can also download NumPy directly to your system and use it directly the same way that we have used our own custom modules that we've worked with up until this time. I personally will highly recommend that you use PIP for this and the way you can do it is if you come to your terminal type pip install and then the name of the library. So, in this case, it's going to be NumPy. Now, if you already have a library installed on your system then it's going to skip the installation process. So if I hit return because I already have this on my system it says requirement already satisfied. Numpy in and then it shows the path in the system where my version of NumPy is installed.
So if you have that installed, now we can start up the python repl so I'm going to start Python up and let's create a cool little process here where we leverage arrays and let's imagine that you've been tasked with building out a system that takes a number and then from that number it builds a variable number of list items and if that isn't clear at all let me write up a little visual right here. So say if I wanted to have a few items in a list so I wanted something like 1, 2, 3, 4, 5, 6, 7, and 8 and then I asked you to build out a function that would return this and it would say have the one list that contained a nested list of 1, 2 and then another nested list of 3, 4 and etc. etc. And it needed to be variables so you couldn't hard code any of these elements in that would be a little bit of a time-consuming process to build.
But what I'm gonna show you is how we can leverage NumPy in order to make this possible. I'm going to import NumPy and I'm going to alias it, so import NumPy as np and that works so as long as after you hit return right there, as long as you did not get an error that means that we have successfully imported the NumPy library and we've aliased it as np.
Now I can say I'm going to create a variable here so I'm gonna say num_range and then say np.arange which is what this is going to do is it's going to allow me to pass in a number, so I'm going to say 16 and then this is going to generate a range with an array.
So it's going to say OK there are going to be 16 elements and I'm going to generate an array of 16 elements and so it takes in a range where it's going to start at zero and go all the way up to 15 so it's going to have 16 elements. And if you run this and then type num_range you can see that we now have an array that is zero to 15.
If you're wondering what an array is, an array is very similar to a list. The NumPy library created the concept of an array which is in many other programming languages. So if you came from JavaScript or Ruby then you're already very familiar with what arrays are. If Python is your very first programming language and you've never heard of an array just know that a list is very similar to an array and NumPy has created the second level of abstraction where it's created this concept of an array so that you can perform even more advanced tasks and that's what we're going to do here.
So remember at the very beginning of this guide I showed you what I wanted? I wanted the ability to take in any kind of arbitrary set of numbers and to have a breakdown of nested arrays inside of that. And so if I call num_range and then call the function reshape I can pass in two different numbers here so if I pass in four in four. What this is going to give me as you can see right here is a nested set of four nested arrays inside of that master array and inside each one of those arrays are four elements.
This gives us the ability to create an entire matrix of values. And this is going to be incredibly necessary when you when it comes to implementing some of the popular machine learning algorithms they will require you to perform steps like this where you are going to have to create an entire collection of data and then from that point, you're going to have to slice that data up into usable components such as small nested array elements exactly like how we have right here.
Imagine if I asked you to do that without using a library like this, it's absolutely possible. Obviously, it is because NumPy is simply a python library so it's obviously possible to perform everything that we just did right here in pure python code without a library but see how much easier it is when you leverage outside tools. And so what you're able to do whenever you bring in libraries like NumPy and like some of the others that were going to walk through instead of you having to start from scratch.
You're actually able to stand on the entire python community and thousands upon thousands of developers who put in countless hours of time building out these kinds of tools so that you can focus on reaching your end goal in implementing the behavior that you want to build as opposed to writing small little boilerplate code elements that would take you to this point instead of having to build every single feature completely from scratch.
And as you go through your development journey whether you are going on the machine learning industry side of things or if you're learning Python to build out complex web or mobile API's you're going to see that it's going to be very rare that you're not going to be using packages like this in some form or another. And so that is our introduction to the NumPy library in Python. I highly recommend for you to go and look at the documentation and explore other functions and what we went through does not even touch the tip of the iceberg with how many different functions and packages are available in NumPy.
If you go back to that cheese shop page and you go down you can see that there is a home page. So if you click on this it has everything from getting to know it all the way through having an entire NumPy tutorial.
so this has a number of the popular functions and I highly recommend that you go through that, try them out in the repl.
Imagine the kinds of is and different levels of functionality that you can build into your program by leveraging what is included in this package.
Code
import numpy as np num_range = np.arange(16) num_range # array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) num_range.reshape(4, 4) # array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])