基于AdaBoostRegressor训练回归决策树预测房屋价格模型

上次碰到单一决策树预测效果不好的情况,实际预测中,对于不同的特征值,应该要赋予不同的权重,正向激励能够生成指定数量的新决策树,每个数都在上一个数预测的结果上,根据预测值跟实际值的偏差,进行权重调整,达到提高预测期望的结果

import sklearn.model_selection as ms
import sklearn.datasets as sd
import sklearn.tree as st
import sklearn.metrics as sm
import sklearn.ensemble as se

boston = sd.load_boston()

train_x, test_x, train_y, test_y = ms.train_test_split(boston.data, boston.target,  test_size=0.1, random_state=7)  

model = st.DecisionTreeRegressor(max_depth=6)
model = se.AdaBoostRegressor(model,n_estimators=700,random_state=7)

model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)

print('训练集:',sm.r2_score(train_y,pred_train_y))
print('测试集:',sm.r2_score(test_y,pred_test_y))

结果

用到两个模块

import sklearn.tree as st

构建一个树
import sklearn.ensemble as se

构建多个树,不停迭代调参

训练集: 0.9819587536039245
测试集: 0.9209163745104365

你可能感兴趣的:(决策树,回归,机器学习)