from sklearn.datasets import load_boston
import numpy as np
from sklearn import tree
import matplotlib.pyplot as plt
import graphviz
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeRegressor
# boston = load_boston()
# regressor = DecisionTreeRegressor(random_state=1)
# # 第一个参数是任意实例化之后的模型:分类回归等等
# score = cross_val_score(
# regressor, boston.data, boston.target, cv=10, scoring="neg_mean_squared_error"
# )
# print(score)
# 准备数据
rng = np.random.RandomState(1) # 生成随机数的种子, 然后是0-1的,在扩大到0-5
x = np.sort(5 * rng.rand(80, 1), axis=0) # 因为决策树的输入必须是2维的,所以80行1列
y = np.sin(x).ravel() # y是2维度的,现在降维到1
y[::5] += 3 * (0.5 - rng.rand(16)) # y的所有行列步长为5取得1个数,总共16个数,然后给他们加上bias(-0.5~0.5)
# 训练模型
regr_1 = DecisionTreeRegressor(criterion='mse', max_depth=2)
regr_2 = DecisionTreeRegressor(criterion='mse', max_depth=5)
regr_1.fit(x, y)
regr_2.fit(x, y)
x_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis] # x现生成0-5步长为0.01,然后升维度2
y_1 = regr_1.predict(x_test)
y_2 = regr_2.predict(x_test)
# 花出土看一看x
plt.figure()
plt.scatter(x, y, s=20, edgecolor="black", c="darkorange", label="data")
plt.plot(x_test, y_1, color="cornflowerblue", label="max_depth=2", linewidth=2)
plt.plot(x_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree")
plt.legend()
plt.show()