In [1]:
import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa
The quartet of NumPy, SciPy, Matplotlib, and IPython is a popular combination in the Python world. We will use each of these libraries in this workshop.
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 workshop 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 workshop.
In [2]:
print numpy.arange(5)
In [3]:
print numpy.linspace(0, 5, 10, endpoint=False)
In [4]:
print numpy.zeros(5)
In [5]:
print numpy.ones(5)
In [6]:
print numpy.ones((5,2))
In [7]:
print scipy.randn(5) # random Gaussian, zero-mean unit-variance
In [8]:
print scipy.randn(5,2)
In [9]:
x = numpy.arange(10)
print x[2:4]
In [10]:
print x[-1]
The optional third parameter indicates the increment value:
In [11]:
print x[0:8:2]
In [12]:
print x[4:2:-1]
If you omit the start index, the slice implicitly starts from zero:
In [13]:
print x[:4]
In [14]:
print x[:999]
In [15]:
print x[::-1]
In [16]:
x = numpy.arange(5)
y = numpy.ones(5)
print x+2*y
dot
computes the dot product, or inner product, between arrays or matrices.
In [17]:
x = scipy.randn(5)
y = numpy.ones(5)
print numpy.dot(x, y)
In [18]:
x = scipy.randn(5,3)
y = numpy.ones((3,2))
print numpy.dot(x, y)
In [19]:
x = numpy.arange(10)
print x < 5
In [20]:
y = numpy.ones(10)
print x < y
In [21]:
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])
The cosine distance measures the angle between two vectors:
In [22]:
print distance.cosine([67, 0], [89, 0])
print distance.cosine([67, 0], [0, 89])
NumPy arrays have a method, sort
, which sorts the array in-place.
In [23]:
x = scipy.randn(5)
print x
x.sort()
print x
numpy.argsort
returns an array of indices, ind
, such that x[ind]
is a sorted version of x
.
In [24]:
x = scipy.randn(5)
print x
ind = numpy.argsort(x)
print ind
print x[ind]