TenMinuteTutor

Coding, maths and art

Creating numpy arrays

There are several ways to create numpy arrays.

Creating an array of zeros

You can create an array of zeros using the zeros function, supplying the required array length:

>>> import numpy as np
>>> a = np.zeros(5)
>>> print(a)
[ 0.  0.  0.  0.  0.]

This creates an array with 5 elements, each set to 0. The array elements are floating point values - this is the default, but you can create an array with different typed elements (eg integers), using the dtype parameter:

>>> a = np.zeros(5, dtype=np.int32)
>>> print(a)
[0 0 0 0 0]

This creates an array of 32 bit integers. There are various different types, discussed here

You can also create 2 dimensional arrays. Instead of supplying a length, you should supply a tuple containing the number of rows and columns:

>>> a = np.zeros((5, 4))
>>> print(a)
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

You can use 3 dimensions or even higher:

>>> a = np.zeros((2, 3, 4))
>>> print(a)
[[[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]

 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]]

Creating an array of ones

The ones function creates an array with all elements set to 1, which is sometimes useful:

>>> a = np.ones(7)
>>> print(a)
[ 1.  1.  1.  1.  1.  1.  1.]

As with the zeros function, you can use dtype to change the type, and you can create multidimensional arrays.

Empty arrays

The empty function creates an array where the elements haven’t been initialised at all, they just contains whatever happened to be in the memory.

>>> a = np.empty(4)
>>> print(a)
[  1.18553402e-311   1.18554384e-311   0.00000000e+000   0.00000000e+000]

You can use this if you are creating an array where the initial values don’t matter (if you intend to fill it with other data). empty is slightly faster than zeros because it doesn’t need to fill the memory with values.

Note that the data isn’t necessarily random. It might contain actual data left over from whenever the memory was last used.

Creating arrays from lists

You can create a numpy array from existing data in a list (or any sequence), using the array function:

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

By default, the type of the array will be the same type as the items in the list. For example, if all the items are ints, the array will use default sized ints (normally this will be 32-bit integers). If some or all of the values are floats, the array will use default sized floats (normally double precision). If the list is mixed, the type will be the float, with all the ints converted to float (the elements of a numpy array must all be the same type).

You can change this using the dtype parameter. For example, in the case above we might want an array of bytes (8-bit ints), so we could do this:

>>> a = np.array([1, 2, 3, 4], dtype=np.int8)
>>> print(a)
[1 2 3 4]

The result looks the same as before, but it now an array of bytes.

You can create a multidimensional array from a multidimensional list:

>>> m = np.array([[1, 2], [3, 4], [5, 6]])
>>> print(m)
[[1 2]
 [3 4]
 [5 6]]