from sklearn.tree import DecisionTreeRegressor
import numpy as np
import matplotlib.pyplot as plt
Copyright By PowCoder代写 加微信 powcoder
# true function
t1 = np.sqrt(x * (1-x))
t2 = (2.1 * np.pi) / (x + 0.05)
t3 = np.sin(t2)
return t1*t3
def f_sampler(f, n=100, sigma=0.05):
# sample points from function f with Gaussian noise (0,sigma**2)
xvals = np.random.uniform(low=0, high=1, size=n)
yvals = f(xvals) + sigma * np.random.normal(0,1,size=n)
return xvals, yvals
np.random.seed(123)
X, y = f_sampler(f, 160, sigma=0.2)
X = X.reshape(-1,1)
fig = plt.figure(figsize=(7,7))
dt = DecisionTreeRegressor(max_depth=2).fit(X,y)
xx = np.linspace(0,1,1000)
plt.plot(xx, f(xx), alpha=0.5, color=’red’, label=’truth’)
plt.scatter(X,y, marker=’x’, color=’blue’, label=’observed’)
plt.plot(xx, dt.predict(xx.reshape(-1,1)), color=’green’, label=’dt’)
plt.legend()
#plt.savefig(“example.png”, dpi=400)
plt.show()
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com