用 XGBoost 在 Python 中进行特征重要性分析和特征选择

开发 后端
使用诸如梯度增强之类的决策树方法的集成的好处是,它们可以从训练有素的预测模型中自动提供特征重要性的估计。

 

本文转载自微信公众号「Python中文社区」,作者沂水寒城。转载本文请联系Python中文社区公众号。

使用诸如梯度增强之类的决策树方法的集成的好处是,它们可以从训练有素的预测模型中自动提供特征重要性的估计。

在本文中,您将发现如何使用Python中的XGBoost库来估计特征对于预测性建模问题的重要性,阅读这篇文章后,您将知道:

  • 如何使用梯度提升算法计算特征重要性。
  • 如何绘制由XGBoost模型计算的Python中的特征重要性。
  • 如何使用XGBoost计算的特征重要性来执行特征选择。

梯度提升中的特征重要性

使用梯度增强的好处是,在构建增强后的树之后,检索每个属性的重要性得分相对简单。通常,重要性提供了一个分数,该分数指示每个特征在模型中构建增强决策树时的有用性或价值。用于决策树的关键决策使用的属性越多,其相对重要性就越高。

此重要性是针对数据集中的每个属性明确计算得出的,从而可以对属性进行排名并进行相互比较。单个决策树的重要性是通过每个属性拆分点提高性能指标的数量来计算的,并由节点负责的观察次数来加权。性能度量可以是用于选择拆分点的纯度(基尼系数),也可以是其他更特定的误差函数。然后,将特征重要性在模型中所有决策树之间平均。有关如何在增强型决策树中计算特征重要性的更多技术信息,请参见《统计学习的要素:数据挖掘,推理和预测》(第367页)第10.13.1节“预测变量的相对重要性”。另外,请参见Matthew Drury对StackOverflow问题“ Boosting的相对变量重要性”的回答,在此他提供了非常详细和实用的答案。

手动绘制特征重要性

训练有素的XGBoost模型会自动计算出您的预测建模问题中的特征重要性。这些重要性分数可在训练模型的feature_importances_成员变量中获得。例如,可以按如下所示直接打印它们:

  1. print(model.feature_importances_) 

我们可以将这些得分直接绘制在条形图上,以直观表示数据集中每个特征的相对重要性。例如:

 

  1. # plot 
  2. pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_) 
  3. pyplot.show() 

 

我们可以通过在皮马印第安人发病的糖尿病数据集上训练XGBoost模型并根据计算出的特征重要性创建条形图来证明这一点。

下载数据集并将其放置在当前工作目录中。

数据集文件:

https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv

数据集详细信息:

https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.names

 

  1. # plot feature importance manually 
  2. from numpy import loadtxt 
  3. from xgboost import XGBClassifier 
  4. from matplotlib import pyplot 
  5. load data 
  6. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=","
  7. # split data into X and y 
  8. X = dataset[:,0:8] 
  9. y = dataset[:,8] 
  10. # fit model no training data 
  11. model = XGBClassifier() 
  12. model.fit(X, y) 
  13. # feature importance 
  14. print(model.feature_importances_) 
  15. # plot 
  16. pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_) 
  17. pyplot.show() 

 

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

首先运行此示例将输出重要性分数。

 

  1. [ 0.089701    0.17109634  0.08139535  0.04651163  0.10465116  0.2026578 0.1627907   0.14119601] 

我们还获得了相对重要性的条形图。

该图的缺点是要素按其输入索引而不是其重要性排序。我们可以在绘制之前对特征进行排序。

值得庆幸的是,有一个内置的绘图函数可以帮助我们。

使用内置XGBoost特征重要性图XGBoost库提供了一个内置函数,可以按重要性顺序绘制要素。该函数称为plot_importance(),可以按以下方式使用:

  1. # plot feature importance 
  2. plot_importance(model) 
  3. pyplot.show() 

例如,以下是完整的代码清单,其中使用内置的plot_importance()函数绘制了Pima Indians数据集的特征重要性。

  1. # plot feature importance using built-in function 
  2. from numpy import loadtxt 
  3. from xgboost import XGBClassifier 
  4. from xgboost import plot_importance 
  5. from matplotlib import pyplot 
  6. load data 
  7. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=","
  8. # split data into X and y 
  9. X = dataset[:,0:8] 
  10. y = dataset[:,8] 
  11. # fit model no training data 
  12. model = XGBClassifier() 
  13. model.fit(X, y) 
  14. # plot feature importance 
  15. plot_importance(model) 
  16. pyplot.show() 

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

运行该示例将为我们提供更有用的条形图。

您可以看到,要素是根据它们在F0至F7的输入数组(X)中的索引自动命名的。手动将这些索引映射到问题描述中的名称,可以看到该图显示F5(体重指数)具有最高的重要性,而F3(皮肤褶皱厚度)具有最低的重要性。

XGBoost特征重要性评分的特征选择

特征重要性评分可用于scikit-learn中的特征选择。这是通过使用SelectFromModel类完成的,该类采用一个模型,并且可以将数据集转换为具有选定要素的子集。此类可以采用预训练的模型,例如在整个训练数据集上进行训练的模型。然后,它可以使用阈值来确定要选择的特征。当您在SelectFromModel实例上调用transform()方法以一致地选择训练数据集和测试数据集上的相同要素时,将使用此阈值。

在下面的示例中,我们首先训练,然后分别在整个训练数据集和测试数据集上评估XGBoost模型。使用从训练数据集计算出的特征重要性,然后将模型包装在SelectFromModel实例中。我们使用它来选择训练数据集上的特征,从选定的特征子集中训练模型,然后在测试集上评估模型,并遵循相同的特征选择方案。

例如:

  1. select features using threshold 
  2. selection = SelectFromModel(model, threshold=thresh, prefit=True
  3. select_X_train = selection.transform(X_train) 
  4. # train model 
  5. selection_model = XGBClassifier() 
  6. selection_model.fit(select_X_train, y_train) 
  7. # eval model 
  8. select_X_test = selection.transform(X_test) 
  9. y_pred = selection_model.predict(select_X_test) 

出于兴趣,我们可以测试多个阈值,以根据特征重要性选择特征。具体来说,每个输入变量的特征重要性,从本质上讲,使我们能够按重要性测试每个特征子集,从所有特征开始,到具有最重要特征的子集结束。

下面提供了完整的代码清单:

  1. # use feature importance for feature selection 
  2. from numpy import loadtxt 
  3. from numpy import sort 
  4. from xgboost import XGBClassifier 
  5. from sklearn.model_selection import train_test_split 
  6. from sklearn.metrics import accuracy_score 
  7. from sklearn.feature_selection import SelectFromModel 
  8. load data 
  9. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=","
  10. # split data into X and y 
  11. X = dataset[:,0:8] 
  12. Y = dataset[:,8] 
  13. # split data into train and test sets 
  14. X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=7) 
  15. # fit model on all training data 
  16. model = XGBClassifier() 
  17. model.fit(X_train, y_train) 
  18. # make predictions for test data and evaluate 
  19. y_pred = model.predict(X_test) 
  20. predictions = [round(value) for value in y_pred] 
  21. accuracy = accuracy_score(y_test, predictions) 
  22. print("Accuracy: %.2f%%" % (accuracy * 100.0)) 
  23. # Fit model using each importance as a threshold 
  24. thresholds = sort(model.feature_importances_) 
  25. for thresh in thresholds: 
  26.  # select features using threshold 
  27.  selection = SelectFromModel(model, threshold=thresh, prefit=True
  28.  select_X_train = selection.transform(X_train) 
  29.  # train model 
  30.  selection_model = XGBClassifier() 
  31.  selection_model.fit(select_X_train, y_train) 
  32.  # eval model 
  33.  select_X_test = selection.transform(X_test) 
  34.  y_pred = selection_model.predict(select_X_test) 
  35.  predictions = [round(value) for value in y_pred] 
  36.  accuracy = accuracy_score(y_test, predictions) 
  37.  print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" % (thresh, select_X_train.shape[1], accuracy*100.0)) 

请注意,如果您使用的是XGBoost 1.0.2(可能还有其他版本),则XGBClassifier类中存在一个错误,该错误会导致错误:

  1. KeyError: 'weight' 

这可以通过使用自定义XGBClassifier类来解决,该类为coef_属性返回None。下面列出了完整的示例。

  1. # use feature importance for feature selection, with fix for xgboost 1.0.2 
  2. from numpy import loadtxt 
  3. from numpy import sort 
  4. from xgboost import XGBClassifier 
  5. from sklearn.model_selection import train_test_split 
  6. from sklearn.metrics import accuracy_score 
  7. from sklearn.feature_selection import SelectFromModel 
  8.   
  9. # define custom class to fix bug in xgboost 1.0.2 
  10. class MyXGBClassifier(XGBClassifier): 
  11.  @property 
  12.  def coef_(self): 
  13.   return None 
  14.   
  15. load data 
  16. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=","
  17. # split data into X and y 
  18. X = dataset[:,0:8] 
  19. Y = dataset[:,8] 
  20. # split data into train and test sets 
  21. X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=7) 
  22. # fit model on all training data 
  23. model = MyXGBClassifier() 
  24. model.fit(X_train, y_train) 
  25. # make predictions for test data and evaluate 
  26. predictions = model.predict(X_test) 
  27. accuracy = accuracy_score(y_test, predictions) 
  28. print("Accuracy: %.2f%%" % (accuracy * 100.0)) 
  29. # Fit model using each importance as a threshold 
  30. thresholds = sort(model.feature_importances_) 
  31. for thresh in thresholds: 
  32.  # select features using threshold 
  33.  selection = SelectFromModel(model, threshold=thresh, prefit=True
  34.  select_X_train = selection.transform(X_train) 
  35.  # train model 
  36.  selection_model = XGBClassifier() 
  37.  selection_model.fit(select_X_train, y_train) 
  38.  # eval model 
  39.  select_X_test = selection.transform(X_test) 
  40.  predictions = selection_model.predict(select_X_test) 
  41.  accuracy = accuracy_score(y_test, predictions) 
  42.  print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" % (thresh, select_X_train.shape[1], accuracy*100.0)) 

注意:由于算法或评估程序的随机性,或者数值精度的差异,您的结果可能会有所不同。考虑运行该示例几次并比较平均结果。

运行此示例将打印以下输出。

  1. Accuracy: 77.95% 
  2. Thresh=0.071, n=8, Accuracy: 77.95% 
  3. Thresh=0.073, n=7, Accuracy: 76.38% 
  4. Thresh=0.084, n=6, Accuracy: 77.56% 
  5. Thresh=0.090, n=5, Accuracy: 76.38% 
  6. Thresh=0.128, n=4, Accuracy: 76.38% 
  7. Thresh=0.160, n=3, Accuracy: 74.80% 
  8. Thresh=0.186, n=2, Accuracy: 71.65% 
  9. Thresh=0.208, n=1, Accuracy: 63.78% 

我们可以看到,模型的性能通常随所选特征的数量而降低。

在此问题上,需要权衡测试集精度的特征,我们可以决定采用较不复杂的模型(较少的属性,例如n = 4),并接受估计精度的适度降低,从77.95%降至76.38%。

这可能是对这么小的数据集的洗礼,但是对于更大的数据集并使用交叉验证作为模型评估方案可能是更有用的策略。

 

责任编辑:武晓燕 来源: Python中文社区
相关推荐

2023-09-18 15:54:56

Python机器学习

2009-12-25 15:00:48

WPF软件

2017-09-19 22:36:39

XGBoostLR 算法

2009-08-05 15:26:23

需求分析

2023-02-26 18:46:35

机器学习数据集算法

2009-11-25 17:36:38

PHP函数includ

2018-11-06 09:31:34

物联网分析AoT物联网

2021-04-09 10:02:29

机器学习人工智能计算机

2021-06-05 08:04:26

机器学习CARTOptimal

2020-04-27 21:44:39

物联网开发物联网IOT

2021-12-22 23:12:19

物联网隐私安全

2020-08-26 10:42:15

IIoT智能工厂工业物联网

2019-09-27 09:56:31

软件技术硬件

2023-09-08 07:01:08

机器学习监控ML

2023-09-04 09:00:00

机器学习算法

2021-11-22 11:40:05

人工智能物联网商业

2021-12-20 11:02:13

勒索软件攻击网络安全

2022-08-26 15:54:34

云原生Kubernetes物联网

2023-05-24 16:08:45

深度学习工业4.0

2023-02-10 10:58:41

智慧城市数据隐私
点赞
收藏

51CTO技术栈公众号