The Array

The main object that you throw around in NumPy is called a multidimensional array. Typically you store numbers in it. Each “dimension” is called an axes. For example, a single co-ordinate in 3D space could be stored as:

This has one axis (one dimension).

A 2D rotation transformation could be described with:

This has two axes.

Creating An Array

NumPy arrays can be created with standard Python lists:

If we wanted to create a 2 axis array we could pass in a list of lists:

You can continue to nest lists within lists to create an array with any number of axes (dimensions).

You can also create arrays with special values, such as arrays full of 1’s, arrays full of zero’s, arrays full of random numbers and arrays with 1’s on the diagonal (like identity matrices).

An array of 1’s:

An array with 1’s on the diagonal:

Another really useful way of creating arrays is with np.arange(). This does exactly what is says, it creates an array with a range of values:

np.linspace() is another great array creating tool, which creates an array of linearly spaced numbers. The following example creates 5 numbers, linearly spaced from 4.0 to 10.0:


Indexing And Reading/Writing

NumPy arrays have one index per axis, forming a tuple. The indexed are zero-indexed, like all sensible languages/libraries :-D.

Reading from a 1 axis array:

Reading from a 2 axis array:

Writing to a 2-axis array:

Doing Basic Operations With Arrays

NumPy arrays can be added element wise with the + operator:

They can be multiplied element-wise with the * operator (this is the same as np.multiply):

A dot-product of two arrays can be done with

The cross-product of two arrays can be done with np.cross():




Dot product of two arrays.

Returns an array with 1’s on the diagonal and 0’s elsewhere (also known as an identity matrix).


Returns a flattened array.

Posted: June 26th, 2018 at 9:28 am
Last Updated on: June 27th, 2018 at 5:30 am