The NumPy Library

Try me

Open In ColabBinder

The Numpy (Numerical Python) is a package of numerical functions to effectively work with multidimensional data structures in Python. In Python, it is possible to work with nested lists to work with multidimensional structures (arrays and matrix), but this is not efficient. The Numpy library defines the numpy array object to provide an efficient and convenient object to define multidimensional structures.

To use Numpy in your Notebooks and programs, you first need to import the package (in this example we use the alias np):

[3]:
import numpy as np

The Numpy Array

The numpy array uses a similar structure to a Python list, although as mentioned above, it provides additional functionalities to easily create and manipulate multidimensional data structures. The data in an array are called elements, and they are accessed using brackets, just as with Python lists. The dimensions of a numpy array are called axes. The elements within an axe are separated using commas and surrounded by brackets. Axes are also separated by brackets, so that a numpy array is represented as an nested python list. The rank is the number of axis of an array. The shape is a list representing the number of elements in each axis. The elements of a numpy array can be of any numerical type.

[6]:
b = np.array([[1,2,3,4],[5,6,7,8]]) #This creates a 2-dimensional (rank 2) 2x4 array
print("My first Numpy array:")
print(b)

print("element in position (1,2) is:")
print(b[1,2])

print("Number of dimensions:")
print(b.ndim) #number of dimensions or rank

print("Shape of array:")
print(b.shape) #shape (eg n rows, m columns)

print("Total number of elements:")
print(b.size) #number of elements
My first Numpy array:
[[1 2 3 4]
 [5 6 7 8]]
element in position (1,2) is:
7
Number of dimensions:
2
Shape of array:
(2, 4)
Total number of elements:
8

Create Numpy Arrays

Numpy includes several functions for creating numpy arrays initialized with convenient ranks, shapes, or elements with constant or random values.

Some examples:

[8]:
o = np.ones((3,2)) # array of 3x2 1s
print(o)

b=np.zeros((3,4))  # array of 3x4 zeroes
print(b)

c=np.random.random(3) #array of 3x1 random numbers
print(c)

d=np.full((2,2),12)  # array of 2x2 12s
print(d)

id =np.eye(3,3) # identity array of size 3x3
print(id)

[[1. 1.]
 [1. 1.]
 [1. 1.]]
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[0.71574091 0.54968971 0.72723399]
[[12 12]
 [12 12]]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Creating sequences

Some useful functions for creating lists are arange and linspace:

  • arange(start, end, step): creates a numpy array with elements ranging from start to end incrementing by step. Only end is required, using only end will create an evenly spaced range from 0 to end.

  • linspace(start,end,numvalues): creates a numpy array with numvalues elements with evenly distributed values ranging from start to end. The increment is calculated by the function so that the resulting number of elements matches the numvalues input parameter.

[10]:
a = np.arange(0, 10, 2)
print(a)

b=np.linspace(0,10,6)
print(b)

[0 2 4 6 8]
[ 0.  2.  4.  6.  8. 10.]

Arithmetic operations

You can apply element-wise arithmetic and logical calculations to numpy arrays using arithmetic or logical operators. The functions np.exp(), np.sqrt(), or np.log() are other examples of functions that operate in the elements of a numpy array. You can check the entire list of available functions in the official Numpy documentation. Some examples:

[11]:
x =np.array([[1,2,3,4],[5,6,7,8]])
y =np.array([[9,10,11,12],[13,14,15,16]])
print(x+y)
print(y-x)
print(np.sqrt(y))
print(np.log(x))
print(x**2)
print(x+5)
[[10 12 14 16]
 [18 20 22 24]]
[[8 8 8 8]
 [8 8 8 8]]
[[3.         3.16227766 3.31662479 3.46410162]
 [3.60555128 3.74165739 3.87298335 4.        ]]
[[0.         0.69314718 1.09861229 1.38629436]
 [1.60943791 1.79175947 1.94591015 2.07944154]]
[[ 1  4  9 16]
 [25 36 49 64]]
[[ 6  7  8  9]
 [10 11 12 13]]

Note that in the last examples, we are adding a scalar value to a numpy array. In general, we can apply arithmetic operations on array of different dimensions, given that the smallest dimension between the operands is one, or that the arrays have the same dimensions. When this condition is met, numpy will expand the smaller array to match the shape of the larger array with an operation called broadcasting.

Array functions

Numpy also provides an extensive list of array functions:

  • sum(): Returns the sum of all elements.

  • min(): Returns the minimum value within the array

  • max(): Returns the maximum value within the array

  • mean(): Returns the mean of an array

  • median(): Returns the median value of the array

  • cumsum(): Returns the cumulative sum of the elements of the array.

All the functions above support the additional axis parameter to work on a specific dimension.

[13]:
x =np.array([[1,2,3,4],[5,6,7,8]])
y =np.array([[9,10,11,12],[13,14,15,16]])

print("sum of all elements in x:")
print(np.sum(x))

print("mean value of y:")
print(np.mean(y))
sum of all elements in x:
36
mean value of y:
12.5

Other functions take two arrays as arguments and perform element wise operations:

  • minimum(): Returns an array with the minimum value in each position of the array

  • maximum(): Returns an array with the maximum value in each position of the array

[14]:
b=np.linspace(0,1,10)
r = c=np.random.random(10)
print(np.minimum(b,r))
[0.         0.11111111 0.22222222 0.0069694  0.44444444 0.3403326
 0.19167794 0.71257103 0.78045669 0.64287305]
[ ]: