题目11.1
代码:
from scipy import linalg
import numpy as np
from matplotlib import pyplot
#11.1
X = np.arange(0, 2.05, 0.05)
Y = np.sin((X - 2) * np.exp(-X ** 2)) ** 2
pyplot.plot(X, Y)
pyplot.title("#11.1")
pyplot.xlabel("x")
pyplot.ylabel("f (x)")
pyplot.axis([0, 2, 0, 1])
pyplot.show()
结果截图:
题目11.2:
代码:
from scipy import linalg
import numpy as np
from matplotlib import pyplot
#11.2
X = np.random.randint(-10, 10, 220).reshape(20, 11)
X[:, 0] = 1
b = np.random.randint(-5, 5, 11).reshape(11, 1)
while 0 in b:
b = np.random.randint(-5, 5, 11).reshape(11, 1)
z = np.random.normal(size = 20).reshape(20, 1)
Y = np.dot(X, b) + z
assume_b = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), Y)
pyplot.plot(b, 'ro', assume_b, 'bx')
pyplot.legend(['True', 'Estimate'])
pyplot.show()
输出截图:
题目11.3:
代码:
from scipy import stats
import numpy as np
from matplotlib import pyplot
#11.3
a = 4
x = sorted(stats.gamma.rvs(a, size=1000))
pyplot.hist(x, bins=25, density=True)
kernel = stats.gaussian_kde(x)
pyplot.plot(x, kernel.pdf(x))
pyplot.show()
输出截图: