当先锋百科网

首页 1 2 3 4 5 6 7
from sklearn.tree import DecisionTreeRegressor
import numpy as np
import matplotlib.pyplot as plt

N = 100
x = np.random.rand(N) * 6 - 3

y = np.sin(x) + np.random.rand(N) * 0.05   # 误差
x = x.reshape(-1, 1)

dt_reg = DecisionTreeRegressor(criterion='mse', max_depth=3)
dt_reg.fit(x, y)

x_test = np.linspace(-3, 3, 50).reshape(-1, 1)
y_hat = dt_reg.predict(x_test)

plt.plot(x, y, "y*", label='actual')
plt.plot(x_test, y_hat, 'b-', linewidth=2, label='predict')
plt.grid()
plt.show()

depth = [2, 4, 6, 8, 10]
color = 'rgbmy'
plt.plot(x, y, 'ko', label='actual')
x_test = np.linspace(-3, 3, 50).reshape(-1, 1)
for d, c in zip(depth, color):
	dt_reg = DecisionTreeRegressor(max_depth=d)
	dt_reg.fit(x, y)
	y_hat = dt_reg.predict(x_test)
	plt.plot(x_test, y_hat, '-', color=c, linewidth=2, label='depth=%d' % d)
plt.legend(loc='upper left')
plt.grid(b=True)
plt.show()

在这里插入图片描述