The last time Hackerfall tried to access this page, it returned a not found error.
A cached version of the page is below, or click here to continue anyway

Oh the amazing things you can do with Numpy.

NumPy is a blazing fast maths library for Python with a heavy emphasis on arrays. It allows you to do vector and matrix maths within Python and as a lot of the underlying functions are actually written in C, you get speeds that you would never reach in vanilla Python.

Numpy is an absolutely key piece to the success of scientific Python and if you want to get into Data Science and or Machine Learning in Python, it’s a must learn. NumPy is well built in my opinion and getting started with it is not difficult at all.

This is the second post in a series of posts on scientific Python, don’t forget to check out the others too.

NumPy revolves around these things called arrays. Actually ndarrays, but we don’t need to worry about that. With these arrays we can do all sorts of useful things like vector and matrix maths at lightning speeds. Get your linear algebra on! (Just kidding we won’t be doing any heavy maths)

# 1D Array

a = np.array([0, 1, 2, 3, 4])

b = np.array((0, 1, 2, 3, 4))

c = np.arange(5)

d = np.linspace(0, 2*np.pi, 5)

print(a) # >>>[0 1 2 3 4]

print(b) # >>>[0 1 2 3 4]

print(c) # >>>[0 1 2 3 4]

print(d) # >>>[ 0. 1.57079633 3.14159265 4.71238898 6.28318531]

print(a[3]) # >>>3

The above code shows 4 different ways of creating an array. The most basic way is just passing a sequence to NumPy’s array() function; you can pass it any sequence, not just lists like you usually see.

Notice how when we print an array with numbers of different length, it automatically pads them out. This is useful for viewing matrices. Indexing on arrays works just like that of a list or any other of Python’s sequences. You can also use slicing on them, I won’t go into slicing a 1D array here, if you want more information on slicing, check out this post.

The above array example is how you can represent a vectorwith NumPy, next we will take a look at how we can represent matrices and more with multidimensional arrays.

# MD Array,

a = np.array([[11, 12, 13, 14, 15],

[16, 17, 18, 19, 20],

[21, 22, 23, 24, 25],

[26, 27, 28 ,29, 30],

[31, 32, 33, 34, 35]])

print(a[2,4]) # >>>25

To create a 2D array we pass the array() function a list of lists (or a sequence of sequences). If we wanted a 3D array we would pass it a list of lists of lists, a 4D array would be a list of lists of lists of lists and so on.

Notice how with a 2D array (with the help of our friend the space bar), is arranged in rows and columns. To index a 2D array we simply reference a row and a column.

To understand this properly, we should really take a look at what vectors and matrices are.

A vector is a quantity that has both direction and magnitude. They are often used to represent things such as velocity, acceleration and momentum. Vectors can be written in a number of ways although the one which will be most useful to us is the form where they are written as an n-tuple such as (1, 4, 6, 9). This is how we represent them in NumPy.

A matrixis similar to a vector, except it is made up of rows and columns; much like a grid. The values within the matrix can be referenced by giving the row and the column that it resides in. In NumPy we make arrays by passing a sequence of sequences as we did previously.

print(a[0, 1:4]) # >>>[12 13 14]

print(a[1:4, 0]) # >>>[16 21 26]

print(a[::2,::2]) # >>>[[11 13 15]

print(a[:, 1]) # >>>[12 17 22 27 32]

The diagram below illustrates what the given example slices do.

a = np.array([[11, 12, 13, 14, 15],

print(type(a)) # >>><class 'numpy.ndarray'>

b = np.array([10, 62, 1, 14, 2, 56, 79, 2, 1, 45,

4, 92, 5, 55, 63, 43, 35, 6, 53, 24,

The dot() function is something called the dot product. The best way to understand this is to see how it is calculated.

There are also some useful operators provided by NumPy for processing an array.

print(a.cumsum()) # >>>[ 0 1 3 6 10 15 21 28 36 45]

‘Fancy indexing’ is a useful way of picking out specific array elements that you want to work with.

print(a) # >>>[ 0 10 20 30 40 50 60 70 80 90]

import matplotlib.pyplot as plt

a = np.linspace(0, 2 * np.pi, 50)

plt.plot(a[mask], b[mask], 'bo')

mask = (b >= 0) & (a <= np.pi / 2)

plt.plot(a[mask], b[mask], 'go')

The example produces the following plot: We use the conditions to select different points on the plot. The blue points (which in the diagram also include the green points, but the green points cover up the blue ones), show all the points that have a value greater than 0. The green points show all points that have a value greater than 0 and that are less than half pi.

print(b) # >>>[ 0 10 20 30 40]

print(c) # >>>[50 60 70 80 90]

print(b) # >>>(array([0, 1, 2, 3, 4]),)

Remember, don't forget to share this post so that other people can see it too!Also, make sure you subscribe to this blog's mailing list, follow me on Twitter and add me on Google+so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!