NumPy Basics and Examples

Let’s start with the basics. Import the library, create a list and then convert it to an array with NumPy.

Screen Shot 2016-03-10 at 10.00.40 PM

Let’s create another list, combine the two and then put both of them to an array. The array at this point becomes two dimensional.
Screen Shot 2016-03-10 at 10.09.25 PM

If you want to know the shape of an array, try the follow
Screen Shot 2016-03-10 at 10.12.38 PM

This returns a tuple and it shows my array is 2 by 4. In other words, my array has 2 rows and 4 columns. You can use the same way to create 3, 4, 5, …. n number of rows and columns. You can also easily initialize your array with zeros. There are two ways:
Screen Shot 2016-03-10 at 10.25.49 PM

Finding the data type of an array is easy as follows:
Screen Shot 2016-03-10 at 10.29.02 PM

To create a 5×5 array of 1s, use the following method:
Screen Shot 2016-03-10 at 10.36.22 PM

To create an Identity Matrix, use the eye method like the following:
Screen Shot 2016-03-10 at 10.39.59 PM

arange is a very cool method you can use to initialize your array. Check the documentation for details but here are some examples:
Screen Shot 2016-03-10 at 10.45.52 PM

You can also initialize by providing what value to start at, when to end and how many spaces/steps between values. Here is what I mean:
Screen Shot 2016-03-10 at 10.51.20 PM

Let’s do some scalar operations on array.
Screen Shot 2016-03-10 at 11.16.25 PM

Let’s reshape an array.
Screen Shot 2016-03-11 at 7.51.52 AM

Transposing an array is easy. Here is how:
Screen Shot 2016-03-11 at 7.53.47 AM

Let’s learn about some Universal Array Functions (in short ufuncs). These are functions you can apply to every value in an array. Details at Python ufunc

Screen Shot 2016-03-11 at 8.14.54 AM

You can also compare the values at each index from the two arrays and return the max.
Screen Shot 2016-03-11 at 8.18.31 AM

We’ll plot some graph and it will require some additional graph plotting library called Matplotlib. I have been using iPython Notebook to show all the code and output here. The next concept will also require some iPython Notebook specific coding to display graphs and I will use comments when needed.

Screen Shot 2016-03-12 at 1.16.08 PM

Screen Shot 2016-03-12 at 1.17.27 PM

Try following the code segments below. Try to experiment with each to have better understanding. We will replace all of it with a shorter version.
Screen Shot 2016-03-12 at 2.16.35 PM

You can replace the above code segments with numpy’s where method like the following:
Screen Shot 2016-03-12 at 2.30.58 PM

Let’s create an 5×5 array with random values.
Screen Shot 2016-03-12 at 2.40.06 PM

We’ll use Numpy’s where method to replace any value in the array that is less zero with zero. We’ll leave everything else the same.
Screen Shot 2016-03-12 at 2.46.11 PM

Let’s learn about some very useful array methods.
Screen Shot 2016-03-12 at 3.04.38 PM
Screen Shot 2016-03-12 at 3.29.15 PM
Screen Shot 2016-03-12 at 3.40.38 PM

Let’s create some sample income data, centered around $54,000 with a normal distribution (remember bell-shaped graph?), a standard deviation of 10,000, and with 10,000 data points. We’ll also look at mean and median values.

Screen Shot 2016-03-12 at 4.02.35 PM

You can save the contents of an array to a file and read it back like the following:
Screen Shot 2016-03-12 at 5.52.50 PM

You can save multiple arrays in a zip file.
Screen Shot 2016-03-12 at 6.01.37 PM

Leave a Reply

Your email address will not be published. Required fields are marked *