Numpy stands for Numerical Python, is the foundational package for scientific computing in Python. It provides
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
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
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
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
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])
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)
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
In [ ]:
from IPython.core.display import HTML
HTML("<iframe src=http://www.numpy.org/ width=800 height=350></iframe>")
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)
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)
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)
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
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
In [ ]:
r1 = np.random.normal(0, 10, 1000)
_ = plt.hist(r1, bins=50, normed=True)
In [ ]:
x = np.random.normal(0, 10, 100)
y = np.random.normal(5, 3, 100)
plt.scatter(x, y)
In [ ]:
r1 = np.random.normal(0, 10, 100)
r2 = np.random.gamma(1, 2, 100)
r3 = r1 + r2
_ = plt.boxplot(r3)
In [ ]:
HTML("<iframe src=https://matplotlib.org/index.html width=800 height=350></iframe>")
SciPy is a collection of packages addressing a number of different standard problem domain in scientific computing. Here is some packages you may use:
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
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
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()
In [ ]:
HTML("<iframe src=https://www.scipy.org/scipylib/index.html width=800 height=350></iframe>")
In [ ]:
a = np.array([1, 3, 5, 7, 8, 9])
In [ ]:
In [ ]:
In [ ]: