Exercise 11.1
from numpy import * import matplotlib.pyplot as plt x = linspace(0, 2, 200) y = power(sin(x - 2), 2) * exp(0-x*x) plt.plot(x, y, 'r-', label = '$x ^ 2$') plt.xlabel('x label') plt.ylabel('y label') plt.title('Exercises 11.1') plt.legend() plt.savefig('exp.png')
Exercise 11.2
import numpy as np import matplotlib.pyplot as plt X = np.random.random((20,10)) b = np.random.random((10, 1)) z = np.random.normal(size = (20,1)) y = np.dot(X, b) + z X = np.mat(X) est_b = np.dot(X.I, y) plt.plot(b, 'rx', label = 'True coefficients') plt.plot(est_b, 'bo', label = 'Estimated coefficients') plt.ylim(-2, 2) plt.ylabel('value') plt.xlabel('index') plt.legend() #plt.show() plt.savefig('pra.png')
Exercise 11.3
import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt z = norm.rvs(size = 1000) x = np.linspace(-3,3, 100) plt.plot(x, norm.pdf(x), 'r-') plt.hist(z, bins = 25, density = True) plt.savefig('hist.png')