import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
N = 100
x = np.random.rand(N) * 6 - 3
y = np.sin(x) + np.random.rand(N) * 0.05
# print(y)
#将x转化成一列
x = x.reshape(-1, 1)
# print(x)
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.legend(loc='upper left')
plt.grid()
# plt.savefig("./temp_decision_tree_regressor")
plt.show() #必须先保存,再展示
#比较不同深度的决策树
depth = [2, 4, 6, 8, 10]
color='rgbmy'
dt_reg = DecisionTreeRegressor(criterion="mse") #默认criterion="mse",
plt.plot(x, y, 'ko', label="actual")
x_test = np.linspace(-3, 3, 50).reshape(-1, 1)
y_test = np.sin(x_test) + np.random.rand(N) * 0.05
print(y_test)
for d, c in zip(depth, color):
dt_reg.set_params(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)
acc_score = accuracy_score(y_test, y_hat)
print("depth:",d)
print('acc score:',acc_score)
plt.legend(loc='upper left')
plt.grid(b=True)
plt.savefig("./temp_compare_decision_tree_regressor")
plt.show()
老师,请问, 这里想测试max_depth为多少的时候会过拟合,加了这三行代码,报错了,不知道哪里不对

