栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

xgb的简单使用(特征选择,重要性图像绘制,分类,预测)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

xgb的简单使用(特征选择,重要性图像绘制,分类,预测)

在kaggle等竞赛中时常可以看到xgb的身影。2016年,陈天奇在论文《 XGBoost:A Scalable Tree Boosting System》中正式提出该算法。XGBoost的基本思想和GBDT相同,但是做了一些优化,比如二阶导数使损失函数更精准;正则项避免树过拟合;Block存储可以并行计算等。XGBoost具有高效、灵活和轻便的特点,在数据挖掘、推荐系统等领域得到广泛的应用。在此简单总结一下常用代码。
假设已经下载好了xgb并准备好了train_x, train_y 与 test_x, test_y

分类
import xgboost as xgb
from xgboost import XGBClassifier
from matplotlib import pyplot as plt

model = XGBClassifier()
model.fit(train_x, train_y )

# feature importance
print(model.feature_importances_)

'''
plot_importance 与 feature_importances 可能会出现不一致
这是因为model.feature_importances_的重要性排名默认使用gain,而xgb.plot_importance默认使用weight。
改一下就一样了
plot_importance(model,importance_type='gain')
'''

# plot feature importance
plot_importance(model)
plt.show()

# 预测
y_pred = model.predict(test_x)
预测
import xgboost as xgb
from xgboost import plot_importance

model = xgb.XGBRegressor(max_depth=6, # 可以调节这些参数来改进模型效果
			learning_rate=0.12, 
			n_estimators=90, 
			min_child_weight=6, 
			objective="reg:gamma")
model.fit(x_train, y_train)
特征重要性图像尺寸调整
import xgboost as xgb
from xgboost import plot_importance
from matplotlib import pyplot as plt

fig,ax = plt.subplots(figsize=(10,6))# 调节图像尺寸
plot_importance(model,
                height=0.6,# 调节线宽
                ax = ax,
                max_num_features=64)#调节显示数目
plt.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号