import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 1000)
y = (np.sin((x-2)*np.exp(- x**2)))**2
plt.plot(x, y, label='$sin ^{2}(x-2)e^{-x^2}$')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Exercise 11.1 Ploting a function')
plt.legend()
plt.show()
Result
Exercise 11.2: Data
Solution
import numpy as np
import matplotlib.pyplot as plt
X = np.random.rand(20, 10)
b = np.random.rand(10, 1)
z = np.random.normal(size=(20, 1))
y = np.matmul(X, b) + z
x = np.linspace(0, 9, 10)
bhat = np.array(np.linalg.lstsq(X, y, rcond=-1)[0])
plt.scatter(x, b, c='r', marker='x', label="True coefficients")
plt.scatter(x, bhat, c='b', marker='o', label='Estimated coefficients')
plt.xlabel('index')
plt.ylabel('value')
plt.xlim((0, 9))
plt.legend()
plt.show()
Result
Exercise 11.3: Histogram and density estimation
Solution
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
import seaborn
z = np.random.normal(0, 1, size=(10000))
kernal = scipy.stats.gaussian_kde(z)
seaborn.distplot(z, bins=25, kde=True)
plt.show()
11 MatplotlibExercise 11.1: Plotting a functionPlot the function f(x) = sin^2(x − 2)e^2 over the interval [0, 2]. Add proper axis labels, a title, etc.Solutionimport numpy as npimport...