The Question Mark - blog by Mark Volkmann

NumPy library

Overview

NumPy is a Python library for scientific computing. It is primarily implemented in optimized C code. It is an alternative to MatLab for use in Python.

NumPy supports many operations on matrices. SciPy builds on NumPy to add support for even more mathematical operations. NumPy is also a dependency of the pandas package.

If you have installed either SciPy or pandas, you already have NumPy. Otherwise, install it by entering pip install numpy. To use it, import numpy as np.

A primary feature of NumPy is that it uses NumPy arrays to store and operate on data. Typically the data in these arrays is numbers, but it can also be any other type. NumPy arrays have many advantages over Python lists including:

  • Type checking of each element is not needed because NumPy arrays are homogeneous.
  • Memory requirements are smaller because the representation of each data value requires less bytes than the equivalent Python representation.
  • NumPy array elements are stored in contiguous memory, so access is faster.
  • Some operations on element values can be performed in parallel by taking advantage of Single Instruction, Multiple Data (SIMD) vector processing.
  • Memory caching can be more effective utilized which results in faster element access.

Creating a NumPy Array

Many function described below accept a dtype argument to specify the data type of the array elements. When this is not specified, the data type is inferred or has a default.

In all function below that accept a number of rows and columns, any number of dimension sizes can be specified, including one.

To create …Use …
array from CSV filea = np.genfromtxt(file_path, delimiter=',')
1D array from a lista = np.array([v1, v2, ...])
1D array from a tuplea = np.array((v1, v2, ...))
2+D array from listsa = np.array([[v1, v2, ...], [w1, w2, ...]])
2+D array from tuplesa = np.array(((v1, v2, ...), (w1, w2, ...)))
array with all zero valuesa = np.zeros((rows, cols), dtype=np.type)
array with all one valuesa = np.ones((rows, cols), dtype=np.type)
array with all a specific valuea = np.full((rows, cols), value, dtype=np.type)
array with same shape as another
and all a specific value
a = np.full_like(other_array, v), value, dtype=np.type) or
a = np.full(other_array.shape, v, dtype=np.type)
array with uninitialized valuesa = np.empty((rows, cols), dtype=np.type)
Values come from whatever happens to be at the memory location.
1D array with range of valuesa = np.arange(start, end, step, dtype=np.type)
Values can be integer or float.
start is inclusive and defaults to 0.
end is exclusive and has no default.
step defaults to 1.
1D array with evenly spaced valuesa = np.linspace(start, end, count, dtype=np.type)
end is inclusive.
count is the number values to produce.
array with random float valuesa = np.random.rand(rows, cols)
Values are between 0 (inclusive) and 1 (exclusive).
array with random integer valuesa = np.random.randint(min, max, size=(rows, cols))
min defaults to zero and is inclusive.
max has no default and is exclusive.
identity matrix (creates n x n array)a = np.identity(n, dtype=np.type)
copy of existing arrayb = a.copy()
copy of existing array, but use specified data typeb = a.astype(np.type)
truncates if necessary
repeated copies elements in existing array`b = np.repeat(a, times, axis={0

Getting Information About an Array

InformationAttribute
number of dimensionsa.dim
shape (size of each dimension)a.shape
data type of elementsa.dtype
number of elements (product of dimension sizes)a.size
size of each element in bytesa.itemsize
total size (a.size * a.itemsize)a.nbytes

Element Access

In all function below that accept row and column indexes, any number of indexes can be specified, including one.

In the tables that follow, some abbreviations are used for arguments.

  • Row index values below are abbreviated as ri.
  • Column index values below are abbreviated as ci.
  • Row start indexes (rsi) and column start indexes (csi) are inclusive and default to zero.
  • Row end indexes (rei) and column end indexes (cei) are exclusive and default to length.
  • Negative index values can be used to count from the end of a dimension where -1 is the last element.
OperationCode
get element valuea[ri, ci]
get entire rowa[ri, :]
get entire columna[:, ci]
get specific columns from a rowa[ri, [ci1, ci2, ...]]
get range of columns from a rowa[ri, csi:cei:step]
set element valuea[ri, ci] = v
set all elements in a row to a valuea[ri, :] = v
set all elements in a column to a valuea[:, ci] = v
set all elements in a row to valuesa[ri, :] = [v1, v2, ...]
must provide all values
set all elements in a column to valuesa[:, ci] = [v1, v2, ...]
must provide all values
set range of elements in a row to valuesa[ri, csi:cei] = [v1, v2, ...]
set range of elements in a column to valuesa[rsi:rei, ci] = [v1, v2, ...]

Operations on One Array

NumPy supports many operations that produce new arrays from the elements in existing arrays.

Here is a sampling of some that create a new array from the elements of an existing array.

OperationCode
add and create new arrayb = a + v
add in placea += v
subtractb = a - v
multiplyb = a \* v
divideb = a / v
reciprocalb = np.reciprocal(a)
works with floats, but not integers
exponentiationb = a \*\* v
squareb = np.square(a)
square rootb = np.sqrt(a)
sineb = np.sin(a)
cosineb = np.cos(a)
tangentb = np.tan(a)
absolute valueb = np.absolute(a) or
b = np.fabs(a)
round to nearest integerb = np.rint(a)
floorb = np.floor(a)
ceilingb = np.ceil(a)
truncateb = np.trunc(a)
sign (-1, 0, or 1)b = np.sign(a)
convert degrees to radiansb = np.radians(a) or b = np.deg2rad(a)
convert radians to degreesb = np.degrees(a) or b = np.rad2deg(a)
create array of same shape
containing boolean values
b = {condition involving a}
can use all relational and logical operators
example: b = a > 50
example: b = (a > 50) & (a < 100)
create 1D array of elements that meet criteriab = a[{condition involving a}]
example: b = a[a > 50]
create 1D array of elements
in a 1D array at specified indexes
b = a[[i1, i2, ...]]
determine if any value in a row meets criteriab = np.any({condition involving a}, axis=1)
returns 1D array of booleans for each row
determine if any value in a column meets criteriab = np.any({condition involving a}, axis=0)
returns 1D array of booleans for each column
determine if all values in a row meets criteriab = np.all({condition involving a}, axis=1)
returns 1D array of booleans for each row
determine if all values in a column meets criteriab = np.all({condition involving a}, axis=0)
returns 1D array of booleans for each column

Operations on Two Arrays

Here is a sampling of operations that combine corresponding elements of two arrays to create elements in a new array.

OperationCode
add elementsb = np.add(a1, a2)
subtract elementsb = np.subtract(a1, a2)
multiply elementsb = np.multiply(a1, a2)
divide elementsb = np.divide(a1, a2)
maximumb = np.maximum(a1, a2) or
b = np.fmax(a1, a2)
element-wise only for 1D arrays
minimumb = np.minimum(a1, a2) or
b = np.fmin(a1, a2)
element-wise only for 1D arrays
greatest common divisorb = np.gcd(a1, a2)
can also pass two numbers
lowest common multipleb = np.lcm(a1, a2)
can also pass two numbers

Here is a sampling of other operations on two arrays.

OperationCode
minimum elementv = np.min(a)
maximum elementv = np.max(a)
sum of elementsv = np.sum(a)
determinantv = np.linalg.det(a)
matrix multiplicationb = np.matmul(a1, a2)
The number of columns in a1 must
equal the number of rows in a2.

The np.linalg package provides many more functions in addition to det. See Linear algebra (numpy.linalg).