2.2.2. Numerical computing with NumPy

NumPy is the most popular numerical computing package for Python. The following examples illustrate the basic functionality of NumPy.

2.2.2.1. Array construction

import numpy
arr = numpy.array([[1, 2, 3], [4, 5, 6]]) # a 2x3 matrix with elements equal to 1..6
arr = numpy.empty((2, 2)) # a 2x2 matrix with each element equal to a randomly selected number
arr = numpy.zeros((2, 2)) # a 2x2 matrix with each element equal to 0.0
arr = numpy.ones((3, 4)) # a 2x3 matrix with each element equal to 1.0
arr = numpy.full((2, 3), 2.0) # a 2x3 matrix with each element equal to 2.0

2.2.2.2. Concatenation

arr1 = numpy.array([...])
arr2 = numpy.array([...])
numpy.concatenate(arr1, arr2)

2.2.2.3. Query the shape of an array

arr = numpy.array([...])
arr.shape()

2.2.2.4. Reshaping

arr = numpy.array([...])
arr.reshape((n_row, n_col, ...))

2.2.2.5. Selection and slicing

arr = numpy.array([[1, 2, 3], [4, 5, 6]])
arr[1, 2] # get the value at second row and third column
arr[0, :] # get the first row
arr[:, 0] # get the first column

2.2.2.6. Transposition

arr = numpy.array([...])
arr_trans = arr.transpose()

2.2.2.7. Algebra

arr1 = numpy.array([...])
arr2 = numpy.array([...])

arr1 + 2 # element-wise addition
arr1 - 2 # element-wise subtraction
arr1 + arr2 # matrix addition
arr1 - arr2 # matrix subtraction

2. * arr # scalar multiplication
arr1 * arr2 # element-wise multiplication
arr1.dot(arr2) # matrix multiplication

arr ** 2. # element-wise exponentiation

2.2.2.8. Trigonometry

arr = numpy.array([...])

numpy.sin(arr) # element-wise sine
numpy.cos(arr) # element-wise cosine
numpy.tan(arr) # element-wise tangent
numpy.asin(arr) # element-wise inverse sin
numpy.acos(arr) # element-wise inverse cosign
numpy.atan(arr) # element-wise inverse tangent

2.2.2.9. Other mathematical functions

numpy.sqrt(arr) # element-wise square root
numpy.ceil() # element-wise ceiling
numpy.floor() # element-wise floor
numpy.round() # element-wise round

2.2.2.10. Data reduction

arr = numpy.array([...])

arr.all() # determine if all of the values are logically equivalent to `True`
arr.any() # determine if any of the values are logically equivalent to `True`
arr.sum() # sum
arr.mean() # mean
arr.std() # standard deviation
arr.var() # variance
arr.min() # minimum
arr.max() # maximum

2.2.2.11. Random number generation

# set the state of the random number generator to reproducibly generate random values
numpy.random.seed(1)

# select a float, randomly between 0 and 1
numpy.random.rand()

# select a random integer between 0 and 10
numpy.random.randint(10)

# select a random integer according to a Poisson distribution with :math:`\lambda = 2`
numpy.random.poisson(2.)

2.2.2.12. NaN and infinity

arr = numpy.array([...])

numpy.nan
numpy.isnan(arr)

numpy.inf
numpy.isinf(arr)
numpy.isfinite(arr)

2.2.2.13. Exercises

  • Concatenate two 3x1 arrays of zeros and ones, and get its shape

  • Select the first column of a random 2x3 array

  • Transpose a random 2x3 array into a 3x2 array

  • Reshape a random 2x3 array into a 3x2 array

  • Create a random 2x3 array and round it

  • Create a random 100x1 array of Poisson-distributed values with lambda = 10 and calculate its min, max, mean, and standard deviation

  • Calculate the element-wise multiplication of two random arrays of size 3x3

  • Calculate the matrix multiplication of two random arrays of size 2x3 and 3x4

  • Check that infinity is greater than \(10^{10}\)

See intro_to_wc_modeling/concepts_skills/software_engineering/numpy_exercises.py for solutions to these exercises.

2.2.2.14. NumPy introduction for MATLAB users

The NumPy documentation contains a concise summary of the NumPy analog for each MATLAB function.