NumPy and SciPy#
import numpy
from mirdotcom import mirdotcom
mirdotcom.init()
The quartet of NumPy, SciPy, Matplotlib, and IPython is a popular combination in the Python world. We will use all of these libraries in this website.
Tutorial#
NumPy is one of the most popular libraries for numerical computing in the world. It is used in several disciplines including image processing, finance, bioinformatics, and more. This entire website is based upon NumPy and its derivatives.
If you are new to NumPy, follow this NumPy Tutorial.
SciPy is a Python library for scientific computing which builds on top of NumPy. If NumPy is like the Matlab core, then SciPy is like the Matlab toolboxes. It includes support for linear algebra, sparse matrices, spatial data structions, statistics, and more.
While there is a SciPy Tutorial, it isn’t critical that you follow it for this website.
Special Arrays#
print(numpy.arange(5))
[0 1 2 3 4]
print(numpy.linspace(0, 5, 10, endpoint=False))
[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5]
print(numpy.zeros(5))
[0. 0. 0. 0. 0.]
print(numpy.ones(5))
[1. 1. 1. 1. 1.]
print(numpy.ones((5, 2)))
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]
print(numpy.random.randn(5)) # random Gaussian, zero-mean unit-variance
[ 0.01520245 0.31760492 0.48029266 0.52087331 -1.01995976]
print(numpy.random.randn(5, 2))
[[-0.70059077 1.21971537]
[-1.990304 0.11238149]
[ 0.92774587 0.57935903]
[ 0.18602804 1.14806358]
[ 1.173989 0.23839278]]
Slicing Arrays#
x = numpy.arange(10)
print(x[2:4])
[2 3]
print(x[-1])
9
The optional third parameter indicates the increment value:
print(x[0:8:2])
[0 2 4 6]
print(x[4:2:-1])
[4 3]
If you omit the start index, the slice implicitly starts from zero:
print(x[:4])
[0 1 2 3]
print(x[:999])
[0 1 2 3 4 5 6 7 8 9]
print(x[::-1])
[9 8 7 6 5 4 3 2 1 0]
Array Arithmetic#
x = numpy.arange(5)
y = numpy.ones(5)
print(x + 2 * y)
[2. 3. 4. 5. 6.]
dot computes the dot product, or inner product, between arrays or matrices.
x = numpy.random.randn(5)
y = numpy.ones(5)
print(numpy.dot(x, y))
-0.7717245984355092
x = numpy.random.randn(5, 3)
y = numpy.ones((3, 2))
print(numpy.dot(x, y))
[[-1.38872379 -1.38872379]
[-0.43601265 -0.43601265]
[-2.43252325 -2.43252325]
[-1.88940286 -1.88940286]
[-2.11360515 -2.11360515]]
Boolean Operations#
x = numpy.arange(10)
print(x < 5)
[ True True True True True False False False False False]
y = numpy.ones(10)
print(x < y)
[ True False False False False False False False False False]
Distance Metrics#
from scipy.spatial import distance
print(distance.euclidean([0, 0], [3, 4]))
print(distance.sqeuclidean([0, 0], [3, 4]))
print(distance.cityblock([0, 0], [3, 4]))
print(distance.chebyshev([0, 0], [3, 4]))
5.0
25.0
7
4
The cosine distance measures the angle between two vectors:
print(distance.cosine([67, 0], [89, 0]))
print(distance.cosine([67, 0], [0, 89]))
0.0
1.0
Sorting#
NumPy arrays have a method, sort, which sorts the array in-place.
x = numpy.random.randn(5)
print(x)
x.sort()
print(x)
[ 0.36507228 0.42198342 -0.29441486 0.12176386 -0.31285356]
[-0.31285356 -0.29441486 0.12176386 0.36507228 0.42198342]
numpy.argsort returns an array of indices, ind, such that x[ind] is a sorted version of x.
x = numpy.random.randn(5)
print(x)
ind = numpy.argsort(x)
print(ind)
print(x[ind])
[ 1.47703112 0.22232594 -0.99845825 1.24360623 0.9456978 ]
[2 1 4 3 0]
[-0.99845825 0.22232594 0.9456978 1.24360623 1.47703112]