博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xgboost特征选择
阅读量:4286 次
发布时间:2019-05-27

本文共 2250 字,大约阅读时间需要 7 分钟。

Xgboost在各挖掘比赛中是一个大杀器,往往可以取得比其他各种更好的效果。数据预处理,特征工程,调参对Xgboost的效果有着非常重要的影响。这里介绍一下运用xgboost的特征选择,运用xgboost的特征选择可以筛选出更加有效的特征代入Xgboost模型。

这里采用的数据集来自于,这里的训练集如下所示,有116个离散特征(cat1-cat116),14个连续特征(cont1 -cont14),离散特征用字符串表示,先要对其进行数值化:

[python]   
 
  1.    id cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 cat9   ...        cont6  \  
  2. 0   1    A    B    A    B    A    A    A    A    B   ...     0.718367     
  3. 1   2    A    B    A    A    A    A    A    A    B   ...     0.438917     
  4. 2   5    A    B    A    A    B    A    A    A    B   ...     0.289648     
  5. 3  10    B    B    A    B    A    A    A    A    B   ...     0.440945     
  6. 4  11    A    B    A    B    A    A    A    A    B   ...     0.178193     
  7.   
  8.       cont7    cont8    cont9   cont10    cont11    cont12    cont13  \  
  9. 0  0.335060  0.30260  0.67135  0.83510  0.569745  0.594646  0.822493     
  10. 1  0.436585  0.60087  0.35127  0.43919  0.338312  0.366307  0.611431     
  11. 2  0.315545  0.27320  0.26076  0.32446  0.381398  0.373424  0.195709     
  12. 3  0.391128  0.31796  0.32128  0.44467  0.327915  0.321570  0.605077     
  13. 4  0.247408  0.24564  0.22089  0.21230  0.204687  0.202213  0.246011  

xgboost的特征选择的代码如下:

[python]   
 
  1. import numpy as np  
  2. import pandas as pd  
  3. import xgboost as xgb  
  4. import operator  
  5. import matplotlib.pyplot as plt  
  6.   
  7. def ceate_feature_map(features):  
  8.     outfile = open('xgb.fmap''w')  
  9.     i = 0  
  10.     for feat in features:  
  11.         outfile.write('{0}\t{1}\tq\n'.format(i, feat))  
  12.         i = i + 1  
  13.     outfile.close()  
  14.   
  15.   
  16. if __name__ == '__main__':  
  17.     train = pd.read_csv("../input/train.csv")  
  18.     cat_sel = [n for n in train.columns if n.startswith('cat')]  #类别特征数值化  
  19.     for column in cat_sel:  
  20.         train[column] = pd.factorize(train[column].values , sort=True)[0] + 1  
  21.   
  22.     params = {  
  23.         'min_child_weight'100,  
  24.         'eta'0.02,  
  25.         'colsample_bytree'0.7,  
  26.         'max_depth'12,  
  27.         'subsample'0.7,  
  28.         'alpha'1,  
  29.         'gamma'1,  
  30.         'silent'1,  
  31.         'verbose_eval'True,  
  32.         'seed'12  
  33.     }  
  34.     rounds = 10  
  35.     y = train['loss']  
  36.     X = train.drop(['loss''id'], 1)  
  37.   
  38.     xgtrain = xgb.DMatrix(X, label=y)  
  39.     bst = xgb.train(params, xgtrain, num_boost_round=rounds)  
  40.   
  41.     features = [x for x in train.columns if x not in ['id','loss']]  
  42.     ceate_feature_map(features)  
  43.   
  44.     importance = bst.get_fscore(fmap='xgb.fmap')  
  45.     importance = sorted(importance.items(), key=operator.itemgetter(1))  
  46.   
  47.     df = pd.DataFrame(importance, columns=['feature''fscore'])  
  48.     df['fscore'] = df['fscore'] / df['fscore'].sum()  
  49.     df.to_csv("../input/feat_sel/feat_importance.csv", index=False)  
  50.   
  51.     plt.figure()  
  52.     df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(610))  
  53.     plt.title('XGBoost Feature Importance')  
  54.     plt.xlabel('relative importance')  
  55.     plt.show()  

你可能感兴趣的文章
简单封装Http的Get和Post请求
查看>>
利用Lambda解决蓝桥杯【消除尾一】问题
查看>>
由size_t引发的思考
查看>>
QT水费管理系统 ——纯C++开发
查看>>
PHP调用科大讯飞语音服务
查看>>
mui.ajax使用注意事项
查看>>
repo简介
查看>>
父类指针访问子类私有对象
查看>>
Windows CMD.exe 系统找不到指定的路径
查看>>
SpringBoot电商项目实战 — 商品的SPU/SKU实现
查看>>
SpringBoot电商项目实战 — ElasticSearch接入实现
查看>>
精选的10款Java开源项目,建议收藏
查看>>
Zookeeper+dubbo分布式开发学习(一)
查看>>
JAVA微信扫码支付及微信App支付开发(模式二)完整功能实现
查看>>
Could not parse multipart servlet request; nested exception is java.io.IOException
查看>>
Java面试题汇总---基础版(附答案)
查看>>
Java面试题汇总---升级版(附答案)
查看>>
分布式文件系统之FastDFS文件服务器原理及搭建
查看>>
linux系统中安装mysql详解
查看>>
Java开发者应该养成的良好习惯
查看>>