别再只把CART当分类树了:手把手教你用Python实现回归树预测房价(附完整代码)

张开发
2026/4/21 12:06:38 15 分钟阅读

分享文章

别再只把CART当分类树了:手把手教你用Python实现回归树预测房价(附完整代码)
别再只把CART当分类树了手把手教你用Python实现回归树预测房价附完整代码房价预测一直是数据分析领域的经典问题。传统的线性回归模型在面对非线性关系时往往力不从心而决策树算法却能很好地捕捉特征间的复杂交互。本文将带你深入CART回归树的实战应用从原理到代码实现一步步构建房价预测模型。1. 为什么选择CART回归树决策树算法中CARTClassification and Regression Trees是少数同时支持分类和回归任务的算法。与ID3、C4.5不同CART采用二叉树结构计算效率更高。在回归任务中它通过递归二分数据空间最终用叶节点的平均值作为预测输出。回归树的三大优势自动处理非线性关系无需人工特征工程对异常值和缺失值不敏感输出结果可解释性强适合业务分析from sklearn.tree import DecisionTreeRegressor # 基础模型构建只需两行代码 regressor DecisionTreeRegressor(max_depth3) regressor.fit(X_train, y_train)2. 数据准备与特征工程我们使用波士顿房价数据集演示该数据集包含13个特征变量和房屋中位数价格标签。首先进行数据探索import pandas as pd from sklearn.datasets import load_boston boston load_boston() df pd.DataFrame(boston.data, columnsboston.feature_names) df[PRICE] boston.target关键特征分析特征名描述与房价相关性RM平均房间数0.7LSTAT低收入人群比例-0.74PTRATIO师生比-0.51注意决策树虽然不需要标准化处理但高度相关的特征会影响特征重要性评估3. 回归树建模全流程3.1 基础模型构建from sklearn.model_selection import train_test_split X df.drop(PRICE, axis1) y df[PRICE] X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2) model DecisionTreeRegressor( criterionmse, # 使用均方误差作为分裂标准 max_depth5, min_samples_split10 ) model.fit(X_train, y_train)3.2 模型评估指标不同于分类任务回归问题需采用不同的评估标准from sklearn.metrics import mean_squared_error, r2_score y_pred model.predict(X_test) print(fMSE: {mean_squared_error(y_test, y_pred):.2f}) print(fR²: {r2_score(y_test, y_pred):.2f})评估结果对比模型MSER²线性回归24.30.72回归树(max_depth3)18.70.78回归树(max_depth5)15.20.824. 关键参数调优实战决策树容易过拟合需要通过参数控制复杂度4.1 主要调参参数params { max_depth: [3, 5, 7, None], min_samples_split: [2, 5, 10], min_samples_leaf: [1, 2, 4] }4.2 网格搜索实现from sklearn.model_selection import GridSearchCV grid_search GridSearchCV( estimatorDecisionTreeRegressor(), param_gridparams, cv5, scoringneg_mean_squared_error ) grid_search.fit(X_train, y_train)最优参数组合print(grid_search.best_params_) # 输出示例{max_depth: 5, min_samples_leaf: 2, min_samples_split: 10}5. 模型解释与业务应用5.1 特征重要性分析import matplotlib.pyplot as plt features X.columns importances model.feature_importances_ plt.barh(features, importances) plt.xlabel(Feature Importance) plt.show()5.2 决策路径解读通过tree模块可以查看具体决策规则from sklearn.tree import export_text tree_rules export_text(model, feature_nameslist(X.columns)) print(tree_rules[:500]) # 打印前500个字符典型决策路径示例如果LSTAT ≤ 14.8且RM ≤ 7.04 → 预测价格$23.5k否则RM 7.04 → 预测价格$45.2k6. 进阶技巧与注意事项6.1 处理过拟合问题使用ccp_alpha参数进行代价复杂度剪枝设置max_leaf_nodes限制叶节点数量通过早停策略防止过度生长6.2 类别型特征处理虽然波士顿房价数据集都是数值特征但实际项目中常遇到类别变量# 使用OrdinalEncoder处理有序类别 from sklearn.preprocessing import OrdinalEncoder encoder OrdinalEncoder(categories[[低, 中, 高]]) X_train[装修等级] encoder.fit_transform(X_train[[装修等级]])7. 完整项目代码示例# 波士顿房价预测完整流程 import pandas as pd from sklearn.datasets import load_boston from sklearn.tree import DecisionTreeRegressor, export_text from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.metrics import mean_squared_error, r2_score import matplotlib.pyplot as plt # 数据加载 boston load_boston() df pd.DataFrame(boston.data, columnsboston.feature_names) df[PRICE] boston.target # 特征工程 X df.drop(PRICE, axis1) y df[PRICE] X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42) # 模型训练 model DecisionTreeRegressor( max_depth5, min_samples_split10, min_samples_leaf2, random_state42 ) model.fit(X_train, y_train) # 模型评估 y_pred model.predict(X_test) print(fMSE: {mean_squared_error(y_test, y_pred):.2f}) print(fR²: {r2_score(y_test, y_pred):.2f}) # 特征重要性可视化 plt.figure(figsize(10,6)) pd.Series(model.feature_importances_, indexX.columns).sort_values().plot.barh() plt.title(Feature Importance) plt.show()在实际项目中我发现将回归树的max_depth控制在5-7层之间既能保持较好的预测性能又不会让模型过于复杂。对于需要更高精度的场景可以尝试集成学习方法如随机森林或梯度提升树它们以回归树为基学习器能显著提升预测效果。

更多文章