Learn some of the many operations (methods) that are available for you in NumPy arrays. You can look into operations such as transposing, truth values, sum product and more. Also learn how to save an array to disk and load it back.
- [Instructor] Apart from math operations and slicing, there are many other things that numpy arrays can do. In python, when we want to see what attributes and methods are in an object, we use the built in dir command. So let's create a vector. V equals np arrange of 12. And let's reshape it again to be four rows and three columns. Now we can do dir of v. And we get a long list of attributes that this vector has. Attributes that start with two underscores have a special naming in python.
We call them dunder methods. Dunder stands for double underscore. We usually ignore them. For example, when I type v and hit tab in Jupyter, v dot and then tab, Jupyter won't show me these dunder methods. Let's talk about solve these attributes. When we do, the dot t attribute, it will return the transport of the matrix. Basically flip a key in the diagonal. There are also v dot any, and v dot all. V dot any return true if one element is true.
As a reminder, everything in python is true except for zero value numbers, empty collections, none, and of course, false itself. So what would be the value of v dot any? It will be true since all the values except zero are true. As a compliment we have v dot all which will return true if all the values are true. In our case the answer will be v dot all, false. Since we have zero at the initial position, then the truth value of zero is false.
In the most recent versions of numpy, we'll raise an exception when trying to get the truth value of an array. So we'll do if v, let's print v is true. And we will get an exception. The reason for the exception is that it's unclear if you'd like the value of all, or any, when asking for the truth value of an array. Python has a zen, which is easily accessible via this this ist array. We write import this, and we get a zen of python. We in the python community regard the zen as a step of guidelines to follow.
A lot of times when I do code review, I'll reference to zen when making comments. I encourage you to read through the zen in detail later, but right now let's look at in the face of ambiguity, refuse the temptation to guess. This is something that a lot of engineers have a hard time with, but from experience I can tell you that when you're not sure what the user is intending, it's better to be safe than to guess and potentially produce a wrong computation. This is why numpy refused to guess the truth value of an array for you. We need to be explicit, and state if you want any or all.
We also have some math methods like min, max, mean, std, sum, prod, and others. For example, v dot prod, which is the product, and we'll get back zero. Since we have zero at the first place. Those methods will work by default on the whole array, but you can specify which axis you would like the operation to happen. For example, if you do v dot sum, and say the axis is one, it will work row-wise, and you will get a new array with the sum of each row.
Similarly, if you do v dot sum, and you say that the axis is zero, you'll get the sum of each column. This will work on an array with more than two dimensions as well. Just provide the right axis. Remember that numpy works hard not to copy anything. However, sometimes we do want to copy and mutate the array without affecting the original one. For this we can use the copy method. So we'll do v one is a copy, that is a v dot copy, and then we're going to change v one, at zero zero to be a thousand, and then we look at v itself.
We see that v was not changed. The last two methods we're going to look at are for serialization. Sometimes we like to save an array to a file, send it over a circuit, or send it to a database for later retrieval. There are many solutions for serialization of numpy arrays. Some of them, like hd of five, can handle terabytes, and even petabyte, scale of data, but for a quick and easy solution, we use load and dump. Load and dump uses python's picker particle which can serialize almost anything.
There are two flavors for these methods. Dump and load will serialize and de-serialize from a file-like object. While dumps and loads will work with bytes. The s version which works with bytes is useful when you'd like to send arrays over circuits, save them to a database, and many other scenarios. Let's have a look. So data is v dot dump s. And if we look at data, we see that it's a sequence of bytes. Then a few days later, or on the other side of receiving the data, we will do v two equal numpy dot load s of data.
We take a look at v two now, and it's exactly what we sent over. We use np dot loads to load the data since we don't have an array. It will construct an array for us. Like np dot loads, when you see that there are functions similar to each method of an array. For example, instead of v dot prod, we can do np dot prod of v. It's up to you to decide which works for you.
- Working with Jupyter notebooks
- Using code cells
- Extensions to the Python language
- Markdown cells
- Editing notebooks
- NumPy basics
- Broadcasting, array operations, and ufuncs
- Folium and Geo
- Machine learning with scikit-learn
- Plotting with matplotlib and bokeh
- Branching into Numba, Cython, deep learning, and NLP