Xgboost筛选特征重要性

基本思想

根据结构分数的增益情况计算出来选择哪个特征的哪个分割点,某个特征的重要性,就是它在所有树中出现的次数之和。

使用代码

import pandas as pd
import xgboost as xgb
import operator
from matplotlib import pylab as plt

def ceate_feature_map(features):
    outfile = open('xgb.fmap', 'w')
    i = 0
    for feat in features:
        outfile.write('{0}\t{1}\tq\n'.format(i, feat))
        i = i + 1

    outfile.close()

def get_data():
    train = pd.read_csv("../input/train.csv")

    features = list(train.columns[2:])

    y_train = train.Hazard

    for feat in train.select_dtypes(include=['object']).columns:
        m = train.groupby([feat])['Hazard'].mean()
        train[feat].replace(m,inplace=True)

    x_train = train[features]

    return features, x_train, y_train

def get_data2():
    from sklearn.datasets import load_iris
    #获取数据
    iris = load_iris()
    x_train=pd.DataFrame(iris.data)
    features=["sepal_length","sepal_width","petal_length","petal_width"]
    x_train.columns=features
    y_train=pd.DataFrame(iris.target)
    return features, x_train, y_train

#features, x_train, y_train = get_data()
features, x_train, y_train = get_data2()
ceate_feature_map(features)

xgb_params = {"objective": "reg:linear", "eta": 0.01, "max_depth": 8, "seed": 42, "silent": 1}
num_rounds = 1000

dtrain = xgb.DMatrix(x_train, label=y_train)
xgb_model = xgb.train(xgb_params, dtrain, num_rounds)

importance = xgb_model.get_fscore(fmap='xgb.fmap')
importance = sorted(importance.items(), key=operator.itemgetter(1))

df = pd.DataFrame(importance, columns=['feature', 'fscore'])
df['fscore'] = df['fscore'] / df['fscore'].sum()

plt.figure()
df.plot()
df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(16, 10))
plt.title('XGBoost Feature Importance')
plt.xlabel('relative importance')
plt.gcf().savefig('feature_importance_xgb.png')
import pandas as pd
import matplotlib.pylab as plt
feat_imp = pd.Series(clf.booster().get_fscore()).sort_values(ascending=False)
#新版需要转换成dict or list 
#feat_imp = pd.Series(dict(clf.get_booster().get_fscore())).sort_values(ascending=False)
#plt.bar(feat_imp.index, feat_imp)  

feat_imp.plot(kind='bar', title='Feature Importances')
plt.ylabel('Feature Importance Score')
plt.show()
### 使用XGBoost进行特征重要性排序 为了理解如何利用XGBoost执行特征重要性排序,先要明白其背后的机制。XGBoost通过统计每个特征被用于分裂节点的次数来衡量该特征重要性;对于每次分裂,还会考虑加权后的盖诺系数增益[^1]。 当训练完成之后,可以通过访问`feature_importances_`属性获取这些数值。此属性返回的是一个数组,其中包含了各个输入变量相对于整体模型性能的重要程度得分: ```python import xgboost as xgb model_XGB = xgb.XGBClassifier().fit(X_train, y_train) print(model_XGB.feature_importances_) ``` 除了直接查看数值外,还可以借助可视化工具更直观地展示这种关系。例如,使用matplotlib库中的pyplot模块配合XGBoost自带的功能画出柱状图表示各因素的影响力度: ```python from matplotlib import pyplot from xgboost import plot_importance # 绘制特征重要性图表 plot_importance(model_XGB) pyplot.show() ``` ### 基于重要性筛选特征的方法 一旦得到了上述提到的特征评分列表,就可以进一步实施降维处理——即只保留那些被认为最有价值的信息源而舍弃其余部分。一种简单的方式就是设定阈值,仅选取分数高于特定水平的要素作为最终输入给分类器的数据集组成部分[^3]。 另一种更为精细的做法则是采用递归消除法(recursive feature elimination,RFE),它会反复构建模型并选出最佳(或最差)的一个特征直到达到预定数量为止。这种方法能够更好地保持原始分布特性的同时实现有效的维度缩减: ```python from sklearn.feature_selection import SelectFromModel threshold = 0.05 # 设定阈值 selection = SelectFromModel(model_XGB, threshold=threshold, prefit=True) # 应用选择标准过滤原数据矩阵 selected_features = selection.transform(X_test) ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值