11 集成学习 - XGBoost案例 - 波士顿房价进行预测

简介:

08 集成学习 - XGBoost概述
09 集成学习 - XGBoost公式推导
10 集成学习 - XGBoost的学习策略

需求: 使用XGBoost相关算法API对波士顿房价进行预测,并最终输出R^2值。

XGBoost相关参数

XGBoost相关参数

参考文献:
https://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.sklearn

数据文件:《boston_housing.data》
'CRIM','ZN', 'INDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT'
0.00632 18.00 2.310 0 0.5380 6.5750 65.20 4.0900 1 296.0 15.30 396.90 4.98 24.00
0.02731 0.00 7.070 0 0.4690 6.4210 78.90 4.9671 2 242.0 17.80 396.90 9.14 21.60
0.02729 0.00 7.070 0 0.4690 7.1850 61.10 4.9671 2 242.0 17.80 392.83 4.03 34.70
0.03237 0.00 2.180 0 0.4580 6.9980 45.80 6.0622 3 222.0 18.70 394.63 2.94 33.40

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd

from sklearn.model_selection  import train_test_split
from sklearn.metrics import mean_squared_error

import xgboost as xgb

mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
def notEmpty(s):
    return s != ''

names = ['CRIM','ZN', 'INDUS','CHAS','NOX','RM',
   'AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT']
path = "datas/boston_housing.data"
## 由于数据文件格式不统一,所以读取的时候
## 先按照一行一个字段属性读取数据,然后再按照每行数据进行处理
fd = pd.read_csv(path, header=None)
data = np.empty((len(fd), 14))
for i, d in enumerate(fd.values):
    d = map(float, filter(notEmpty, d[0].split(' ')))
    data[i] = list(d)

x, y = np.split(data, (13,), axis=1)
y = y.ravel()

print ("样本数据量:%d, 特征个数:%d" % x.shape)
print ("target样本数据量:%d" % y.shape[0])

样本数据量:506, 特征个数:13
target样本数据量:506

# 查看数据信息
X_DF = pd.DataFrame(x)
X_DF.info()
X_DF.describe().T
X_DF.head()


RangeIndex: 506 entries, 0 to 505
Data columns (total 13 columns):
0 506 non-null float64
1 506 non-null float64
2 506 non-null float64
3 506 non-null float64
4 506 non-null float64
5 506 non-null float64
6 506 non-null float64
7 506 non-null float64
8 506 non-null float64
9 506 non-null float64
10 506 non-null float64
11 506 non-null float64
12 506 non-null float64
dtypes: float64(13)
memory usage: 51.5 KB

#数据的分割,
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, random_state=14)
print ("训练数据集样本数目:%d, 测试数据集样本数目:%d" % (x_train.shape[0], x_test.shape[0]))

训练数据集样本数目:404, 测试数据集样本数目:102


XGBoost将数据转换为XGBoost可用的数据类型

dtrain = xgb.DMatrix(x_train, label=y_train)
dtest = xgb.DMatrix(x_test)

XGBoost模型构建

# 1. 参数构建
params = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'reg:linear'}
num_round = 2
# 2. 模型训练
bst = xgb.train(params, dtrain, num_round)
# 3. 模型保存
bst.save_model('xgb.model')

XGBoost模型预测

y_pred = bst.predict(dtest)
print(mean_squared_error(y_pred, y_test))

24.8697379567

# 4. 加载模型
bst2 = xgb.Booster()
bst2.load_model('xgb.model')
# 5 使用加载模型预测
y_pred2 = bst2.predict(dtest)
print(mean_squared_error(y_pred2, y_test))

24.8697379567

画图

plt.figure(figsize=(12,6), facecolor='w')
ln_x_test = range(len(x_test))

plt.plot(ln_x_test, y_test, 'r-', lw=2, label=u'实际值')
plt.plot(ln_x_test, y_pred, 'g-', lw=4, label=u'XGBoost模型')
plt.xlabel(u'数据编码')
plt.ylabel(u'租赁价格')
plt.legend(loc = 'lower right')
plt.grid(True)
plt.title(u'波士顿房屋租赁数据预测')
plt.show()


找出最重要的特征

from xgboost import plot_importance  
from matplotlib import pyplot  
# 找出最重要的特征
plot_importance(bst,importance_type = 'cover')  
pyplot.show()

相关文章
|
1月前
|
机器学习/深度学习 Python
CatBoost高级教程:深度集成与迁移学习
CatBoost高级教程:深度集成与迁移学习【2月更文挑战第17天】
28 1
|
1月前
|
机器学习/深度学习 算法 Python
CatBoost中级教程:集成学习与模型融合
CatBoost中级教程:集成学习与模型融合【2月更文挑战第13天】
40 3
|
2月前
|
机器学习/深度学习 算法 Python
LightGBM高级教程:深度集成与迁移学习
LightGBM高级教程:深度集成与迁移学习【2月更文挑战第6天】
100 4
|
30天前
|
机器学习/深度学习 算法
大模型开发:描述集成学习以及它如何工作。
集成学习通过结合多个模型预测提升性能,减少偏差和方差。分为Bagging和Boosting:Bagging使用数据子集并行训练模型,如随机森林;Boosting则顺序训练,聚焦前一轮错误,如AdaBoost。Stacking利用模型输出训练新模型。多样性是关键,广泛应用于分类、回归等任务,能有效提高泛化能力和防止过拟合。
16 0
|
1天前
|
机器学习/深度学习 缓存 算法
【视频】Boosting集成学习原理与R语言提升回归树BRT预测短鳍鳗分布生态学实例-2
【视频】Boosting集成学习原理与R语言提升回归树BRT预测短鳍鳗分布生态学实例
18 5
|
6天前
|
机器学习/深度学习 算法 前端开发
Scikit-learn进阶:探索集成学习算法
【4月更文挑战第17天】本文介绍了Scikit-learn中的集成学习算法,包括Bagging(如RandomForest)、Boosting(AdaBoost、GradientBoosting)和Stacking。通过结合多个学习器,集成学习能提高模型性能,减少偏差和方差。文中展示了如何使用Scikit-learn实现这些算法,并提供示例代码,帮助读者理解和应用集成学习提升模型预测准确性。
|
7天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
18 0
|
22天前
|
存储 异构计算
System Generator学习——使用 AXI 接口和 IP 集成器(三)
System Generator学习——使用 AXI 接口和 IP 集成器
15 3
|
2月前
|
机器学习/深度学习 Python
探索XGBoost:深度集成与迁移学习
探索XGBoost:深度集成与迁移学习
75 2
|
2月前
|
算法 Python
深入理解XGBoost:集成学习与堆叠模型
深入理解XGBoost:集成学习与堆叠模型
91 1

热门文章

最新文章