如何用Python计算机器学习中特征的重要程度?

特征重要性评分是一种为输入特征评分的手段,其依据是输入特征在预测目标变量过程中的有用程度。
特征重要性有许多类型和来源,尽管有许多比较常见,比如说统计相关性得分,线性模型的部分系数,基于决策树的特征重要性和经过随机排序得到重要性得分。
特征重要性在预测建模项目中起着重要作用,包括提供对数据、模型的见解,以及如何降维和选择特征,从而提高预测模型的的效率和有效性。
在本教程中,我将会阐述用于python机器学习的特征重要性。完成本教程后,你将会知道:
特征重要性在预测建模中的作用
如何计算和查看来自线性模型和决策树的特征重要性
如何计算和查看随机排序重要性得分
现在让我们开始吧。
教程概述
本教程分为五部分,分别是:
1.特征重要性
2.准备
2.1. 检查scikit-learn版本
2.2. 创建测试数据集
3.特征重要性系数
3.1. 基于线性回归系数的特征重要性
3.2. 基于logistic回归的特征重要性
4.基于决策树的特征重要性
4.1. 基于cart的特征重要性
4.2. 基于随机森林的特征重要性
4.3. 基于xgboost的特征重要性
5.随机排序特征重要性
5.1. 随机排序(回归)中的特征重要性
5.2. 随机排序(分类)中的特征重要性
1.特征重要性
特征重要性是一种为预测模型的输入特征评分的方法,该方法揭示了进行预测时每个特征的相对重要性。
可以为涉及预测数值的问题(称为回归)和涉及预测类别标签的问题(称为分类)计算特征重要性得分。
这些得分非常有用,可用于预测建模问题中的多种情况,例如:
更好地理解数据
更好地理解模型
减少输入特征的数量
特征重要性得分可以帮助了解数据集
相对得分可以突出显示哪些特征可能与目标最相关,反之则突出哪些特征最不相关。这可以由一个领域专家解释,并且可以用作收集更多的或不同的数据的基础。
特征重要性得分可以帮助了解模型
大多数重要性得分是通过数据集拟合出的预测模型计算的。查看重要性得分可以洞悉该特定模型,以及知道在进行预测时哪些特征最重要和哪些最不重要。这是一种模型解释,适用于那些支持它的模型。
特征重要性可用于改进预测模型
可以使用的重要性得分来选择要删除的特征(最低得分)或要保留的特征(最高得分)。这是一种特征选择,可以简化正在建模的问题,加快建模过程(删除特征称为降维),在某些情况下,还可以改善模型的性能。
特征重要性得分可以被输入到包装器模型,如selectfrommodel或selectkbest,以进行特征选择。
有许多方法和模型可以计算特征重要性得分。
也许最简单的方法是计算每个特征和目标变量之间的统计学相关系数。
在本教程中,我们将研究三种比较高级的特征重要性,即:
从模型系数得知的特征重要性。
决策树中的特征重要性。
随机排序检验中的特征重要性。
现在让我们深入了解这三个!
2.准备
在深入学习之前,我们先确认我们的环境并准备一些测试数据集。
检查scikit-learn版本
首先,确认你已安装最新版本的scikit-learn库。这非常重要,因为在本教程中,我们我们研究的一些模型需要最新版的库。
您可以使用以下示例代码来查看已安装的库的版本:
# check scikit-learn version
import sklearn
print(sklearn.__version__)
运行示例代码将会打印出库的版本。在撰写本文时,大概是version 0.22。你需要使用此版本或更高版本的scikit-learn。
0.22.1
生成测试数据集
接下来,让我们生成一些测试数据集,这些数据集可以作为基础来证明和探索特征重要性得分。每个测试问题有五个重要特征和五不重要的特征,看看哪种方法可以根据其重要性找到或区分特征可能会比较有意思。
分类数据集
我们将使用make_classification()函数创建一个用于测试的二进制分类数据集。
数据集将包含1000个实例,且包含10个输入特征,其中五个将会提供信息,其余五个是多余的。
为了确保每次运行代码时都得到相同的实例,我们将使用假随机数种子。下面列出了创建数据集的示例。
# test classification dataset
from sklearn.datasets import make_classification
# define dataset
x, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# summarize the dataset
print(x.shape, y.shape)
运行示例,创建数据集,并确保所需的样本和特征数量。
(1000, 10) (1000,)
回归数据集
我们将使用make_regression()函数创建一个用于测试的回归数据集。
像分类数据集一样,回归数据集将包含1000个实例,且包含10个输入特征,其中五个将会提供信息,其余五个是多余的。
# test regression dataset
from sklearn.datasets import make_regression
# define dataset
x, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
# summarize the dataset
print(x.shape, y.shape)
运行示例,创建数据集,并确保所需的样本和特征数量。
(1000, 10) (1000,)
接下来,我们仔细看一下特征重要性系数。
3.特征重要性系数
线性的机器学习能够拟合出预测是输入值的加权和的模型。
案例包括线性回归,逻辑回归,和正则化的扩展案例,如岭回归和弹性网络。
所有这些算法都是找到一组要在加权求和中使用的系数,以便进行预测。这些系数可以直接用作粗略类型的特征重要性得分。
我们来仔细研究一下分类和回归中的特征重要性系数。我们将在数据集中拟合出一个模型以找到系数,然后计算每个输入特征的重要性得分,最终创建一个条形图来了解特征的相对重要性。
3.1线性回归特征重要性
我们可以在回归数据集中拟合出一个linearregression模型,并检索coeff_属性,该属性包含为每个输入变量(特征)找到的系数。这些系数可以为粗略特征重要性评分提供依据。该模型假设输入变量具有相同的比例或者在拟合模型之前已被按比例缩放。
下面列出了针对特征重要性的线性回归系数的完整示例。
# linear regression feature importance
from sklearn.datasets import make_regression
from sklearn.linear_model import linearregression
from matplotlib import pyplot
# define dataset
x, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
# define the model
model = linearregression()
# fit the model
model.fit(x, y)
# get importance
importance = model.coef_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
得分表明,模型找到了五个重要特征,并用零标记了剩下的特征,实际上,将他们从模型中去除了。
feature: 0, score: 0.00000
feature: 1, score: 12.44483
feature: 2, score: -0.00000
feature: 3, score: -0.00000
feature: 4, score: 93.32225
feature: 5, score: 86.50811
feature: 6, score: 26.74607
feature: 7, score: 3.28535
feature: 8, score: -0.00000
feature: 9, score: 0.00000
然后为特征重要性得分创建条形图。
这种方法也可以用于岭回归和弹性网络模型。
3.2 logistic回归特征重要性
就像线性回归模型一样,我们也可以在回归数据集中拟合出一个logisticregression模型,并检索coeff_属性。这些系数可以为粗略特征重要性评分提供依据。该模型假设输入变量具有相同的比例或者在拟合模型之前已被按比例缩放。
下面列出了针对特征重要性的logistic回归系数的完整示例。
# logistic regression for feature importance
from sklearn.datasets import make_classification
from sklearn.linear_model import logisticregression
from matplotlib import pyplot
# define dataset
x, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# define the model
model = logisticregression()
# fit the model
model.fit(x, y)
# get importance
importance = model.coef_[0]
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
回想一下,这是有关0和1的分类问题。请注意系数既可以为正,也可以为负。正数表示预测类别1的特征,而负数表示预测类别0的特征。
从这些结果,至少从我所知道的结果中,无法清晰的确定出重要和不重要特征。
feature: 0, score: 0.16320
feature: 1, score: -0.64301
feature: 2, score: 0.48497
feature: 3, score: -0.46190
feature: 4, score: 0.18432
feature: 5, score: -0.11978
feature: 6, score: -0.40602
feature: 7, score: 0.03772
feature: 8, score: -0.51785
feature: 9, score: 0.26540
然后为特征重要性得分创建条形图。
现在我们已经看到了将系数用作重要性得分的示例,接下来让我们看向基于决策树的重要性得分的常见示例
4.基于决策树的特征重要性
决策树算法,比如说classification and regression trees(cart)根据gini系数或熵的减少来提供重要性得分。这个方法也可用于随机森林和梯度提升算法。
ok.现在让我们看看相应的运行示例。
4.1基于cart的特征重要性
对于在scikit-learn中实现的特征重要性,我们可以将cart算法用于decisiontreeregressor和decisiontreeclassifier类
拟合后,模型提供feature_importances_属性,可以访问该属性以检索每个输入特征的相对重要性得分。
让我们看一个用于回归和分类的示例。
基于cart(回归)的特征重要性
下面列出了拟合decisiontreeregressor和计算特征重要性得分的完整示例。
# decision tree for feature importance on a regression problem
from sklearn.datasets import make_regression
from sklearn.tree import decisiontreeregressor
from matplotlib import pyplot
# define dataset
x, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
# define the model
model = decisiontreeregressor()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的三个可能对预测很重要。
feature: 0, score: 0.00294
feature: 1, score: 0.00502
feature: 2, score: 0.00318
feature: 3, score: 0.00151
feature: 4, score: 0.51648
feature: 5, score: 0.43814
feature: 6, score: 0.02723
feature: 7, score: 0.00200
feature: 8, score: 0.00244
feature: 9, score: 0.00106
然后为特征重要性得分创建条形图。
基于cart(分类)的特征重要性
下面列出了拟合decisiontreeclassifier和计算特征重要性得分的完整示例
# decision tree for feature importance on a classification problem
from sklearn.datasets import make_classification
from sklearn.tree import decisiontreeclassifier
from matplotlib import pyplot
# define dataset
x, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# define the model
model = decisiontreeclassifier()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的四个可能对预测很重要。
feature: 0, score: 0.01486
feature: 1, score: 0.01029
feature: 2, score: 0.18347
feature: 3, score: 0.30295
feature: 4, score: 0.08124
feature: 5, score: 0.00600
feature: 6, score: 0.19646
feature: 7, score: 0.02908
feature: 8, score: 0.12820
feature: 9, score: 0.04745
然后为特征重要性得分创建条形图。
4.2随机森林中的特征重要性
对于在scikit-learn中实现的特征重要性,我们可以将random forest算法用于decisiontreeregressor和decisiontreeclassifier类。
拟合后,模型提供feature_importances_属性,可以访问该属性以检索每个输入特征的相对重要性得分。
这种方法也可以与装袋和极端随机树(extratree)算法一起使用。
让我们看一个用于回归和分类的示例。
随机森林(回归)中的特征重要性
下面列出了拟合randomforestregressor和计算特征重要性得分的完整示例
# random forest for feature importance on a regression problem
from sklearn.datasets import make_regression
from sklearn.ensemble import randomforestregressor
from matplotlib import pyplot
# define dataset
x, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
# define the model
model = randomforestregressor()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的两个或三个可能对预测很重要。
feature: 0, score: 0.00280
feature: 1, score: 0.00545
feature: 2, score: 0.00294
feature: 3, score: 0.00289
feature: 4, score: 0.52992
feature: 5, score: 0.42046
feature: 6, score: 0.02663
feature: 7, score: 0.00304
feature: 8, score: 0.00304
feature: 9, score: 0.00283
然后为特征重要性得分创建条形图。
随机森林(分类)中的特征重要性
下面列出了拟合randomforestclassifier和计算特征重要性得分的完整示例
# random forest for feature importance on a classification problem
from sklearn.datasets import make_classification
from sklearn.ensemble import randomforestclassifier
from matplotlib import pyplot
# define dataset
x, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# define the model
model = randomforestclassifier()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的两个或三个可能对预测很重要。
feature: 0, score: 0.06523
feature: 1, score: 0.10737
feature: 2, score: 0.15779
feature: 3, score: 0.20422
feature: 4, score: 0.08709
feature: 5, score: 0.09948
feature: 6, score: 0.10009
feature: 7, score: 0.04551
feature: 8, score: 0.08830
feature: 9, score: 0.04493
然后为特征重要性得分创建条形图。
4.3基于xgboost的特征重要性
xgboost是一个库,它提供了随机梯度提升算法的高效实现。可以通过xgbregressor和xgbclassifier类将此算法与scikit-learn一起使用。
拟合后,模型提供feature_importances_属性,可以访问该属性以检索每个输入特征的相对重要性得分。
scikit-learn还通过gradientboostingclassifier和gradientboostingregressor提供了该算法,并且可以使用相同的特征选择方法
首先,安装xgboost库,例如:
sudo pip install xgboost
然后,通过检查版本号来确认该库已正确安装并且可以正常工作。
# check xgboost version
import xgboost
print(xgboost.__version__)
运行该示例,你应该看到以下版本号或者更高版本。
0.90
有关xgboost库的更多信息,请看:
xgboost with python
让我们看一个用于回归和分类问题的示例。
基于xgboost(回归)的特征重要性
下面列出了拟合xgbregressor并且计算特征重要性得分的完整示例
# xgboost for feature importance on a regression problem
from sklearn.datasets import make_regression
from xgboost import xgbregressor
from matplotlib import pyplot
# define dataset
x, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
# define the model
model = xgbregressor()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的两个或三个可能对预测很重要。
feature: 0, score: 0.00060
feature: 1, score: 0.01917
feature: 2, score: 0.00091
feature: 3, score: 0.00118
feature: 4, score: 0.49380
feature: 5, score: 0.42342
feature: 6, score: 0.05057
feature: 7, score: 0.00419
feature: 8, score: 0.00124
feature: 9, score: 0.00491
然后为特征重要性得分创建条形图。
基于xgboost(分类)的特征重要性
下面列出了拟合xgbclassifier并且计算特征重要性得分的完整示例
# xgboost for feature importance on a classification problem
from sklearn.datasets import make_classification
from xgboost import xgbclassifier
from matplotlib import pyplot
# define dataset
x, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# define the model
model = xgbclassifier()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中有七个可能对预测很重要。
feature: 0, score: 0.02464
feature: 1, score: 0.08153
feature: 2, score: 0.12516
feature: 3, score: 0.28400
feature: 4, score: 0.12694
feature: 5, score: 0.10752
feature: 6, score: 0.08624
feature: 7, score: 0.04820
feature: 8, score: 0.09357
feature: 9, score: 0.02220
然后为特征重要性得分创建条形图。
5.基于随机排序的特征重要性
随机排序特征重要性(permutation feature importance)可以计算相对重要性,与所使用的模型无关。
首先,在数据集中拟合出一个模型,比如说一个不支持本地特征重要性评分的模型。然后,尽管对数据集中的特征值进行了干扰,但仍可以使用该模型进行预测。对数据集中的每个特征进行此操作。然后,再将整个流程重新操作3、5、10或更多次。我们得到每个输入特征的平均重要性得分(以及在重复的情况下得分的分布)。
此方法可以用于回归或分类,要求选择性能指标作为重要性得分的基础,例如回归中的均方误差和分类中的准确性。
可以通过permutation_importance()函数(以模型和数据集为参数)和评分函数进行随机排序特性选择。
让我们看下这个特征选择方法,其算法并不支持特征选择,尤其是k近邻算法( k-nearest neighbors)。
5.1随机排序(回归)特征重要性
下面列出了拟合kneighborsregressor并且计算特征重要性得分的完整示例。
# permutation feature importance with knn for regression
from sklearn.datasets import make_regression
from sklearn.neighbors import kneighborsregressor
from sklearn.inspection import permutation_importance
from matplotlib import pyplot
# define dataset
x, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
# define the model
model = kneighborsregressor()
# fit the model
model.fit(x, y)
# perform permutation importance
results = permutation_importance(model, x, y, scoring=‘neg_mean_squared_error’)
# get importance
importance = results.importances_mean
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的两个或三个可能对预测很重要。
feature: 0, score: 175.52007
feature: 1, score: 345.80170
feature: 2, score: 126.60578
feature: 3, score: 95.90081
feature: 4, score: 9666.16446
feature: 5, score: 8036.79033
feature: 6, score: 929.58517
feature: 7, score: 139.67416
feature: 8, score: 132.06246
feature: 9, score: 84.94768
然后为特征重要性得分创建条形图。
5.2随机排序(分类)特征重要性
下面列出了拟合kneighborsclassifier并且计算特征重要性得分的完整示例。
# permutation feature importance with knn for classification
from sklearn.datasets import make_classification
from sklearn.neighbors import kneighborsclassifier
from sklearn.inspection import permutation_importance
from matplotlib import pyplot
# define dataset
x, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)
# define the model
model = kneighborsclassifier()
# fit the model
model.fit(x, y)
# perform permutation importance
results = permutation_importance(model, x, y, scoring=‘accuracy’)
# get importance
importance = results.importances_mean
# summarize feature importance
for i,v in enumerate(importance):
print(‘feature: %0d, score: %.5f’ % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
运行示例,拟合模型,然后输出每个特征的系数值。
结果表明,这十个特征中的两个或三个可能对预测很重要。
feature: 0, score: 0.04760
feature: 1, score: 0.06680
feature: 2, score: 0.05240
feature: 3, score: 0.09300
feature: 4, score: 0.05140
feature: 5, score: 0.05520
feature: 6, score: 0.07920
feature: 7, score: 0.05560
feature: 8, score: 0.05620
feature: 9, score: 0.03080
然后为特征重要性得分创建条形图。
总结
在本教程中,您知道了在python机器学习中的特征重要性得分。
具体来说,您了解到:
特征重要性在预测建模问题中的作用
如何从线性模型和决策树中计算和查看特征重要性
如何计算和查看随机排序特征重要性得分

华为折叠手机Mate X2深度评测
fireflyAIO-3288J主板IR使用介绍
繁星之夜更多精彩即将呈现!OPPO Reno5系列今日发布
传输线损耗:模型和方程式
求一种基于RJM8L151S的智能提醒药盒解决方案
如何用Python计算机器学习中特征的重要程度?
应用在数码相机触摸屏中的触摸芯片
光缆是干什么用的?选购光缆需要注意哪些?
吉利集团的股东有哪些_吉利汽车的基本架构
虹科新闻 | 虹科电子与 Mend 正式建立合作伙伴关系
SPWM调制方法对比分析
Arm发布全新智能视觉参考设计 满足中国市场视觉应用设备的强劲增长需求
USB3.0:VL817Q7-C0的LAYOUT指南(一)
空调扇怎么用
BGA封装类别
TikTok近期在欧洲招兵买马扩充团队
硬件大牛分析:成本、低功耗、可靠性、系统效率
10月我国集成电路制造设备进口额同比增长6.5%
iOS10.3.3正式版更新推送,iOS10.2越狱、iOS10.3越狱还有吗?iOS10.3.3升级教程、iOS10.3描述文件
小巧玲珑的iPhone SE第二代大曝光!