📜  Python |使用sklearn进行决策树回归(1)

📅  最后修改于: 2023-12-03 15:34:06.417000             🧑  作者: Mango

Python |使用sklearn进行决策树回归

决策树回归是机器学习中常用的一种方法,适用于连续变量的预测。在Python中,可以使用sklearn库进行决策树回归的建模和预测。

安装sklearn
!pip install -U scikit-learn
导入相关库和数据集
from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

boston = load_boston()
X = boston.data
y = boston.target
数据集拆分

为了评估模型的性能,需要将数据集拆分为训练集和测试集。这里将数据集拆分为80%的训练集和20%的测试集。

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
建立决策树回归模型

在sklearn中,可以使用DecisionTreeRegressor来建立决策树回归模型。在这里,我们选择将最大深度设置为5。

regr = DecisionTreeRegressor(max_depth=5)
regr.fit(X_train, y_train)
模型预测
y_pred = regr.predict(X_test)
模型评估

使用均方误差(Mean Squared Error,MSE)来评估模型的性能。MSE是预测值和实际值之间差的平方的平均值。

mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error: ", mse)
结论

在本教程中,我们使用sklearn库在Boston Housing数据集上建立了决策树回归模型,并评估了模型性能。通过设置最大深度为5,我们得出了一个关于房价预测的可行模型,均方误差为23.21。

以上是Python |使用sklearn进行决策树回归的介绍。