class sklearn.tree.
DecisionTreeRegressor
(*, criterion='mse', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, ccp_alpha=0.0)
1 重要参数,属性及接口
from sklearn.datasets import load_boston from sklearn.model_selection import cross_val_score from sklearn.tree import DecisionTreeRegressor boston = load_boston() regressor = DecisionTreeRegressor(random_state=0) cross_val_score(regressor, boston.data, boston.target, cv=10, scoring = "neg_mean_squared_error") #交叉验证cross_val_score的用法
2 实例:一维回归的图像绘制
import numpy as np from sklearn.tree import DecisionTreeRegressor import matplotlib.pyplot as plt
rng = np.random.RandomState(1) X = np.sort(5 * rng.rand(80,1), axis=0) y = np.sin(X).ravel() y[::5] += 3 * (0.5 - rng.rand(16)) #np.random.rand(数组结构),生成随机数组的函数 #了解降维函数ravel()的用法 np.random.random((2,1)) np.random.random((2,1)).ravel() np.random.random((2,1)).ravel().shape
regr_1 = DecisionTreeRegressor(max_depth=2) regr_2 = DecisionTreeRegressor(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] y_1 = regr_1.predict(X_test) y_2 = regr_2.predict(X_test) #np.arrange(开始点,结束点,步长) 生成有序数组的函数 #了解增维切片np.newaxis的用法 l = np.array([1,2,3,4]) ll.shape l[:,np.newaxis] l[:,np.newaxis].shape l[np.newaxis,:].shape
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 Regression") plt.legend() plt.show()