Exercise 11.1: Plotting a function
x = np.linspace(-2, 2, 1000)
y = np.power((np.sin(x - 2)), 2) * np.exp(-x * x)
plt.plot(x, y)
plt.show()
程序运行结果
Exercise 11.2: Data
X = np.random.randint(10, 20, (20, 10))
b = np.random.random(10)
z = np.random.random(20)
y = np.dot(X, b) + z
b1= np.linalg.lstsq(X, y)[0] #最小二乘法生成线性方程
x = list(range(1, 11))
plt.scatter(x, b, c='r', marker='x', label='true coefficients')
plt.scatter(x, b1, c='b', marker='o', label='estimated coefficients')
plt.legend()
plt.show()
Exercise 11.3: Histogram and density estimation
x = np.random.normal(size=1000)
x=sorted(x)
plt.hist(x, bins=25,normed=1)
kernel = stats.gaussian_kde(x)
plt.plot(x, kernel.pdf(x))
plt.show()