2.2 Introduction to NumPy
: 40 minutes
We will use NumPy throughout this course to manipulate tensors—called ndarrays
(n-dimensional arrays or simply arrays) in NumPy. NumPy, short for Numerical Python, is the central package for scientific computations with Python.
NumPy arrays provide fast, vectorized arithmetic operations and flexible broadcasting capabilities. Such operations are almost 10 to 50 times faster than their loop based alternatives!
Import NumPy into your workspace in the following way:
NumPy Arrays
NumPy offers its N-dimensional array object, or ndarray
, to represent a tensor and to facilitate efficient operations with them. Throughout this course, an array will refer to a NumPy N-dimensional array object, and a tensor its mathematical counterpart.
Recall that the matrix \bold{A}=\begin{bmatrix}1.5 & -0.1 & 3\\ 0 & -3 & 6.5\end{bmatrix} is a 2nd-order tensor with shape (2, 3).
To give you a taste of how NumPy works, we create an array named data
to represent \bold{A}:
One can then perform mathematical operations with data
:
dtype
An array is a generic container for homogeneous data. Unlike a Python list
, all of the elements of an array must have the same type. NumPy offers several data types. For most numeric purposes in this course, we will use numpy.float64
for double-precision floating points, int64
for long integers, and numpy.bool
for boolean.
shape
Every array has a shape
: the shape of the tensor it represents. NumPy shape
is a tuple indicating the size of each dimension or axis.
reshape
The reshape
function can be applied on an array to change its shape.
Array Creation
One can use the function np.array
to very conveniently create a new array from an iterable Python object like a list, list of lists, and dictionary.
From a list
As an optional argument, you can pass the desired data type.
From a list
of lists
Probably you have noticed while running the above code chuck, NumPy adds the decimal places (.
) after each of the printed values. Since we have a floating point namely 1.2
in our input list, NumPy decides to automatically set dtype
to numpy.float64
. See it for yourself by running B.dtype
.
It is often best to let NumPy infer the data type rather than passing a data type. Try the following code to see what happens.
numpy.arange
Very much like the usual range()
in Python, numpy.arange
creates arrays with incremental values. The general form of the function is np.arange(start, end, step)
, with integer start, end, and step value.
Note that the start and step values are optional arguments with the defaults values 0
and 1
, respectively.
numpy.linspace
Unlike numpy.arange
, numpy.linspace
guarantees the number of elements and the inclusion of both start and end values.
The function linspace(start, end, cardinality)
creates arrays with a given number of elements, and spaced equally between the specified start and end values.
numpy.eye
This function creates a 2D identity array (or matrix) of a given dimension.
numpy.diag
numpy.diag
can be used to create a 2D array (or matrix) with a prescribed vector as its main diagonal as the shown in the following:
An optional argument offset
can also be used to move away from the main diagonal in either direction, depending on the sign of offset
.
Alternatively, when supplied with a 2D array, numpy.diag
returns the diagonal vector.
numpy.zeros
and numpy.ones
For a desired shape an array can be created filled with either zeros or ones. For example,
Random Number Generator
The NumPy package or library offers all sorts of (pseudo) random number generators through its numpy.random
module. The module can be used as an alternative to Python’s built-in package random
.
Using the random number generator default_rng
from numpy.random
, one can also populate an array of a desired shape with random numbers.
Method | Description |
---|---|
permulation |
Return a random permutation of a sequence, or return a permuted range |
shuffle |
Randomly permute a sequence in place |
uniform |
Draw samples from a uniform [0, 1) distribution |
integers |
Draw random integers from a given low-to-high range |
standard_normal |
Draw samples from a normal distribution with mean 0 and standard deviation 1 |
binomial |
Draw samples from a binomial distribution |
normal |
Draw samples from a normal (Gaussian) distribution |
beta |
Draw samples from a beta distribution |
chisquare |
Draw samples from a chi-square distribution |
gamma |
Draw samples from a gamma distribution |
For more, read the documentation.