数据分析实战之AdaBoost(对房价进行预测)

本文数据源来自于sklearn中自带的波士顿房价数据集。

1、加载数据

from sklearn.datasets import load_boston
data = load_boston()

2、将数据分割成训练集和测试集

from sklearn.model_selection import train_test_split  #准备训练集和测试集
train_x,test_x,train_y,test_y = train_test_split(data.data,data.target,test_size = 0.3,random_state = 33)

3、创建AdaBoost回归模型,得到房价预测结果

from sklearn.ensemble import AdaBoostRegressor
from sklearn.metrics import mean_squared_error #导入均方误差
regressor = AdaBoostRegressor()
regressor.fit(train_x,train_y)
pred_y = regressor.predict(test_x)
print('房价预测结果',pred_y)

数据分析实战之AdaBoost(对房价进行预测)_第1张图片

4、将预测结果与实际结果对比

mse = mean_squared_error(test_y,pred_y)
print('均方误差 = ',round(mse,2))   #返回浮点数的四舍五入值


均方误差 =  16.15

5、使用决策树回归和KNN回归分析数据集,对比结果

from sklearn.tree import DecisionTreeRegressor
dec_regressor = DecisionTreeRegressor()
dec_regressor.fit(train_x,train_y)
pred_dec_y = dec_regressor.predict(test_x)
mse_dec = mean_squared_error(test_y,pred_dec_y)
print('决策树均方误差=',round(mse_dec,2))


决策树均方误差= 24.13
from sklearn.neighbors import KNeighborsRegressor
knn_regressor = KNeighborsRegressor()
knn_regressor.fit(train_x,train_y)
pred_knn_y = knn_regressor.predict(test_x)
mse_knn = mean_squared_error(test_y,pred_knn_y)
print('KNN均方误差=',round(mse_knn,2))


KNN均方误差= 27.27

可以看到AdaBoost的均方误差更小,结果更优。

你可能感兴趣的:(数据分析实战)