An introduction into key libraries and their usage in Python.
- Learn about Python scope rules.
- Learn the basics of scientific and vectorized computing using numpy.
- Learn the basics of plotting with matplotlib.pyplot.
- Learn the basics of random numbers with the random module
- Learn how to measure time.
def increment(n):
n += 1
return n
n = 1
while n < 10:
n = increment(n)
print(n)
ml = [12,33,55,77,99,31]
ml.sort()
print(ml)
ml.reverse()
print(ml)
ml.remove(77)
ml
ml.append(11)
ml.append(3)
ml.append(4)
ml.append(3)
ml
ml.remove(3)
print(ml)
class MyList(list):
def remove_min(self) :
self.remove(min(self))
def remove_max(self) :
self.remove(max(self))
x = [1,7,8,9,33,11]
y = MyList(x)
dir(x)
dir(y)
y
y.remove_max()
y
y.remove_min()
y
y.append('suraj')
y
Introduction to NumPy Arrays¶
import numpy as np
zero_vector = np.zeros(5)
zero_vector
zero_matrix = np.zeros((5,6))
zero_matrix
type(zero_vector)
type(zero_matrix)
ones_matrix = np.ones((5,6))
ones_matrix
empty_matrix = np.empty((5,3))
empty_matrix
x = np.array([1,3,5,6,7,8,])
x
Y = np.array([[1,2,3],[4,5,6]])
Y
Y.transpose()
Y
x = np.array([[3,6],[5,7]])
y = x.transpose()
print(y)
x[1,:]
y[1,:]
y[:,0]
y[1]
y1 = [4,5,5,6,7,8,9,0]
y1[:5]
np.array([4,5,6,7]) + np.array([1,3,6,7]) + y1[:4]
x = np.array([1,2,5])
x[1:2]
z1 = np.array([1,3,5,79])
z2 = z1 + 1
z2
ind = [0,2,3]
z1[ind]
z2 > 6
z2[z2 > 6]
z1[z2>6]
np.linspace(0,100,10)
np.logspace(np.log10(250),np.log10(500), 10)
z1.shape
z2.shape
z1.size
x = 20
not np.any([x%i == 0 for i in range(2, x)])
Matplotlib and Pyplot¶
import matplotlib.pyplot as plt
plt.plot([0,3,4,6,17]);
x = np.linspace(0,10,20)
type(x)
y = x**2.0
x
y
plt.plot(x,y)
y1 = x ** 1.5
plt.plot(x,y, 'bo-', linewidth = 2, markersize=12)
plt.plot(x,y1, 'gs-', linewidth = 2, markersize=12)
plt.plot([0,1,2],[0,1,4],"rd-")
plt.plot(x,y, 'bo-', linewidth = 2, markersize=8, label="First")
plt.plot(x,y1, 'gs-', linewidth = 2, markersize=8, label="Second")
plt.xlabel('$X$')
plt.ylabel('$Y$')
xmin = -0.5
xmax = 10.5
ymin = -5
ymax = 105
plt.axis([xmin, xmax, ymin, ymax])
plt.legend(loc = "upper left")
plt.savefig('myplot.pdf')
Plotting Using Logarithmic Axes¶
plt.loglog(x,y, 'bo-', linewidth = 2, markersize=8, label="First")
plt.loglog(x,y1, 'gs-', linewidth = 2, markersize=8, label="Second")
plt.xlabel('$X$')
plt.ylabel('$Y$')
plt.legend(loc = "upper left")
x = np.linspace(-1,1,40)
y = x ** 3
y1 = x ** 4
plt.loglog(x,y, 'bo-', linewidth = 2, markersize=8, label="First")
plt.loglog(x,y1, 'gs-', linewidth = 2, markersize=8, label="Second")
plt.xlabel('$X$')
plt.ylabel('$Y$')
plt.legend(loc = "upper left")
x = np.logspace(0,1,10)
y = x**2
plt.loglog(x,y,"bo-")
Generating Histograms¶
x = np.random.normal(size = 1000)
plt.hist(x)
plt.hist(x, normed = True, bins = np.linspace(-5, 5, 21));
x = np.random.gamma(2, 3, 100000)
plt.hist(x, bins = 30)
plt.hist(x, bins = 30, normed = True)
plt.hist(x, bins = 30, cumulative = True, normed = True, histtype = 'step')
Multiple Histograms into one Figure¶
plt.figure()
plt.subplot(221)
plt.hist(x, bins = 30)
plt.subplot(222)
plt.hist(x, bins = 30, normed = True)
plt.subplot(223)
plt.hist(x, bins = 30, cumulative = 30)
plt.subplot(224)
plt.hist(x, bins = 30, normed = True, cumulative = True, histtype = 'step')