Numpy

Numpy stands for Numerical Python, is the foundational package for scientific computing in Python. It provides

  • A fast and efficient multidimensional arrary object ndarray
  • Functions for performing element-wise computation with arrays and mathematical operations between arrays
  • Linear algebra operations, Fourier transform, and random number generation

In [ ]:
import numpy as np

In [ ]:
a = np.array([1, 2, 3, 4])
a

In [ ]:
type(a)

In [ ]:
2*a  # multiple ndarray by number

In [ ]:
b = np.array([2, 3, 4, 5])
print(a)
print(b)

a+b  # two array summation

In [ ]:
a*b

In [ ]:
np.log(a)  # apply functions to array
  • index and slicing
  • var[lower:upper:step]

In [ ]:
a

In [ ]:
a[1]

In [ ]:
a[1:3]

In [ ]:
# omitted boundaries are assumed to be the beginning (or end) of the array

a[:3]  # grap first three elements

In [ ]:
a[::2] # element with step size 2
  • Multi-Dimensional Arrays

In [ ]:
c = np.array([[0, 1, 2, 3],
          [10, 11, 12, 13]])
c

In [ ]:
c[0, 3]  # get the first row and fourth number

In [ ]:
c[1]  # get the second row

In [ ]:
c[0, 2:4]  # get the first rown and slicing the first row
  • Basic info of array

In [ ]:
print(a)
a.shape

In [ ]:
b = a.reshape((2, 2))
b

In [ ]:
b.T

In [ ]:
c[:, 2:4] # slice the row

In [ ]:
print(c)
c.shape

In [ ]:
a.size

In [ ]:
c.size
  • Joining arrays

In [ ]:
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])

# Stack arrays in sequence vertically (row wise).
np.vstack([a, b])

In [ ]:
# Stack arrays in sequence horizontally (column wise).
np.hstack([a, b])
  • Array Calculation Methods

In [ ]:
a = np.array([[1, 2, 3],
            [4, 5, 6]])
a

In [ ]:
np.sum(a)

In [ ]:
np.sum(a, axis=0)

In [ ]:
np.sum(a, axis=1)

In [ ]:
np.max(a)

In [ ]:
np.min(a, axis=0)

In [ ]:
np.prod(a)
  • Array creation Functions

In [ ]:
np.arange(4)  # arange(start, stop=None, step=1)

In [ ]:
np.arange(1, 5, 2)

In [ ]:
np.ones(4)  # ones(shape)

In [ ]:
np.ones((2, 3))

In [ ]:
np.zeros((2, 3)) # zeros(shape)

In [ ]:
np.linspace(0, 1, 5)  # Generate N evenly spaced elements between start and stop values

For more detail of numpy please refer to Numpy.org


In [ ]:
from IPython.core.display import HTML
HTML("<iframe src=http://www.numpy.org/  width=800 height=350></iframe>")

matplotlib

  • matplotlib for producing plots and other 2D data visualizations. It is well-suited for creating plots suitable for publication.

In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

In [ ]:
x = np.linspace(0, 2*np.pi, 20)
y = np.sin(x)
plt.xlabel('x label')  # set label for x coordinate
plt.ylabel('y label')  # set label for y coordinates
plt.title('Sin Function')
plt.plot(x, y)
  • Draw two plots on one coordinate

In [ ]:
x = np.linspace(0, 2*np.pi, 20)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1)
plt.plot(x, y2)

Working with multiple figures and axes

  • figure represents the whole picture
  • axes represents each plot with independ coordinate
  • The subplot() command specifies (numrows, numcols, fignum) where fignum ranges from 1 to numrows * numcols.

In [ ]:
x = np.linspace(0, 2*np.pi, 20)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure()
ax1 = plt.subplot(211)
ax1.plot(x, y1)

ax2 = plt.subplot(212)
ax2.plot(x, y2)
  • Show the labels of each plot
  • Specify color of each plot

In [ ]:
x = np.linspace(0, 2*np.pi, 20)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure()
ax1 = plt.subplot(211)
ax1.plot(x, y1, label='Sin', color='b')
ax1.legend()  # show the label of plots on ax1

ax2 = plt.subplot(212)
ax2.plot(x, y2, label='Cos', color='r')
ax2.legend()  # show the label of plotson ax2
  • Set boundary of each coordinate

In [ ]:
x = np.linspace(0, 2*np.pi, 20)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure()
ax1 = plt.subplot(211)
ax1.plot(x, y1, label='Sin', color='b')
ax1.legend()  # show the label on ax1
ax1.set_xlim(-10, 10)  # set the boundary of x coordinate

ax2 = plt.subplot(212)
ax2.plot(x, y2, label='Cos', color='r')
ax2.legend()  # show the label on ax2
ax2.set_ylim(-2, 2)  # set the boundary of y coordinate

Histogram


In [ ]:
r1 = np.random.normal(0, 10, 1000)
_ = plt.hist(r1, bins=50, normed=True)

Scatterplot


In [ ]:
x = np.random.normal(0, 10, 100)
y = np.random.normal(5, 3, 100)
plt.scatter(x, y)

Boxplots


In [ ]:
r1 = np.random.normal(0, 10, 100)
r2 = np.random.gamma(1, 2, 100)
r3 = r1 + r2
_ = plt.boxplot(r3)

For more detail of matplotlib please refer to matplotlib.org


In [ ]:
HTML("<iframe src=https://matplotlib.org/index.html width=800 height=350></iframe>")

SciPy

SciPy is a collection of packages addressing a number of different standard problem domain in scientific computing. Here is some packages you may use:

  • scipy.integrate: numerical integration routines and differential equation solvers
  • scipy.linalg: linear algebra routines and matrix decompositions
  • scipy.optimize: function optimizers and root finding algorithms
  • scipy.stats: standard probability distributions (cdf, pdf, sampler and etc)
  • Example of linear algebra operations

In [ ]:
from scipy import linalg  # import linear algebra operations

arr = np.array([[1, 2],
               [3, 4]])
linalg.det(arr)  # compute the determinant of a square matrix

In [ ]:
linalg.inv(arr)  # compute the inverse of a squar matrix
  • Example of optimization algorithm

In [ ]:
from scipy import optimize
def f(x):
    return x**2 + 10*np.sin(x)

x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x))

In [ ]:
result = optimize.minimize(f, x0=0)
result.x

In [ ]:
plt.plot(x, f(x))
plt.axvline(result.x)  # plot a vertical lines at result x
  • Example of probability distributions method

In [ ]:
from scipy import stats

samples = np.random.normal(size=100)
plt.hist(samples, bins = 20, normed=True, label='Histogram of samples')
x_space = np.linspace(*plt.xlim())

pdf = stats.norm.pdf(x_space)
plt.plot(x_space, pdf, label='PDF')
plt.legend()

For more detail of matplotlib please refer to scipy.org


In [ ]:
HTML("<iframe src=https://www.scipy.org/scipylib/index.html width=800 height=350></iframe>")

Exercises 1

Write a Python program to reverse an array (first element becomes last)


In [ ]:
a = np.array([1, 3, 5, 7, 8, 9])

Exercises 2

Write a Python program to create a (5*5) 2d array with 1 on the border and 0 inside.


In [ ]:

Exercises 3

Write a Python program to sample a distribution $\epsilon_1 + 2\epsilon_2$ with 100 samples, where $\epsilon_1 \sim N(0, 3)$ and $\epsilon_2 \sim N(2, 1)$. Then plot the histgram of the sample.


In [ ]:

Exercises 4

Write a Python program to get the values and indices of the elements that are bigger than 10 in a given array.

Original array: [[ 0 10 20] [20 30 40]] Values bigger than 10 = [20 20 30 40] Their indices are (array([0, 1, 1, 1]), array([2, 0, 1, 2]))


In [ ]: