How
Contents
How#
numpy
is a large library with incredible functionality all of which will not be
listed here. Instead some specific tools will be described so as to give a
sufficient understanding of the capabilities of the library.
How to create an array#
To create a numpy
array we use the numpy.array
tool which takes any iterable.
Tip
numpy.array(iterable)
For example:
import numpy as np
array = np.array((0, 12, 3, 1))
array
array([ 0, 12, 3, 1])
How to create a given number of values between two bounds#
numpy
has a popular tool to create a linear space: numpy.linspace
which take
a lower bound, an upper bounds and a number. It returns the given number of points
uniformly spaced between the bounds.
Tip
numpy.linspace(lower_bound, upper_bound, number)
For example:
import numpy as np
np.linspace(10, 400, 4)
array([ 10., 140., 270., 400.])
How to create an array of zeros#
To create an array of zeros we can use the numpy.zeros
tool. It takes an
argument for the dimension of the array. This can either be a single number in
which case a single dimensional array is created or a tuple with multiple
dimensions.
Tip
numpy.zeros(size)
For example:
import numpy as np
np.zeros(4)
array([0., 0., 0., 0.])
Or:
np.zeros((3, 5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
How to generate random arrays#
numpy
has a powerful random number generator. It can be accessed from
numpy.random
and has multiple tools. The simplest of which is
numpy.random.random
. This takes an optional argument that is the dimension of
the array.
Tip
numpy.random.random(size)
For example:
import numpy as np
np.random.random()
0.09842182789169451
Or:
import numpy as np
np.random.random((3, 5))
array([[0.51389407, 0.82317499, 0.46233661, 0.70457652, 0.13095647],
[0.57201104, 0.05366114, 0.52008317, 0.20140822, 0.11326868],
[0.19790599, 0.58688418, 0.21801245, 0.94041359, 0.93228787]])
How to index arrays#
Multiple dimensional arrays can be indexed in a natural mathematical way using
array[position]
.
Tip
array[position]
For example:
import numpy as np
array = np.array(((1, 2, 3), (4, 5, 6)))
array[1, 2]
6
How to do arithmetic on arrays#
It is possible to directly do arithmetic on arrays in a mathematical way.
Tip
array1 + array2
For example to do element wise multiplication:
array1 = np.array((1, 2, 3, 4))
array2 = np.array((2, 0, -1, 0.5))
array1 * array2
array([ 2., 0., -3., 2.])
How to invert a matrix#
numpy
can invert a matrix using np.linalg.inv
. There are numerous other
linear algebraic tools available in np.linalg
.
Tip
np.linalg.inv(array)
For example:
import numpy as np
matrix = np.array(((1, 2), (3, 1)))
matrix_inverse = np.linalg.inv(matrix)
matrix_inverse
array([[-0.2, 0.4],
[ 0.6, -0.2]])
We can confirm the expected result:
matrix @ matrix_inverse
array([[1., 0.],
[0., 1.]])
How to raise a matrix to a power#
Using the usual operator for exponentiation **
with a numpy
array carries
out element wise exponentiation. To raise a matrix to a power we use
np.linalg.matrix_power
. This takes an array and the exponent.
Tip
np.linalg.ing(array, exponent)
For example:
import numpy as np
matrix = np.array(((1, 2), (3, 1)))
np.linalg.matrix_power(matrix, 3)
array([[19, 18],
[27, 19]])
How to fit a line of best fit#
numpy
can be used to fit polynomials to a points. This is done with the
numpy.polyfit
tool. This takes an array of x values, an array of y values and
a degree. It returns the coefficients of a polynomial of given degree that best
approximates \(f(x)=y\).
Tip
np.polyfit(x, y, degree)
For example, the code below creates y
using a quadratic and recovers the
coefficients of the quadratic:
x = np.array((1, 2, 3, 4))
y = 2 * x ** 2 + 3 * x + 1
a, b, c = np.polyfit(x, y, 2)
a, b, c
(2.0000000000000004, 3.0000000000000013, 1.0000000000000036)