知行编程网知行编程网  2022-04-20 14:00 知行编程网 隐藏边栏 |   抢沙发  27 
文章评分 0 次,平均分 0.0

GitHub标星1.3k!一款功能强大的特征选择工具

编辑 | 机器学习算法与Python实践
特征选择(feature selection)是查找和选择数据集中最有用特征的过程,是机器学习流程中的一大关键步骤。不必要的特征会降低训练速度、降低模型可解释性,并且最重要的是还会降低其在测试集上的泛化表现。
目前存在一些专用型的特征选择方法,我常常要一遍又一遍地将它们应用于机器学习问题,这实在让人心累。所以我用 Python 构建了一个特征选择类并开放在了 GitHub 上。这个 FeatureSelector 包含一些最常用的特征选择方法:
  1. 具有高缺失值百分比的特征
  2. 共线性(高度相关的)特征
  3. 在基于树的模型中重要度为零的特征
  4. 重要度较低的特征
  5. 具有单个唯一值(unique value)的特征
项目地址:
https://github.com/WillKoehrsen/feature-selector
有关使用方法,请参阅Feature Selector的使用指南
https://github.com/WillKoehrsen/feature-selector/blob/master/Feature%20Selector%20Usage.ipynb

1 示例数据集

为了进行演示,我们将使用来自 Kaggle「家庭信用违约风险」机器学习竞赛的一个数据样本。
GitHub标星1.3k!一款功能强大的特征选择工具
这个竞赛是一个监督分类问题,这也是一个非常合适的数据集,因为其中有很多缺失值、大量高度关联的(共线性)特征,还有一些无助于机器学习模型的无关特征。

2 创建实例

要创建一个 FeatureSelector 类的实例,我们需要传入一个结构化数据集,其中观察在行中,特征在列中。我们可以使用一些仅操作特征的方法,但基于重要度的方法也需要训练标签。因为这是一个监督分类任务,所以我们将使用一组特征和一组标签。
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">from feature_selector import FeatureSelector</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># Features are in train and labels are in train_labels</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs = FeatureSelector(data = train, labels = train_labels)</span></section>

3 方法

这个特征选择器有 5 种用于查找待移除特征的方法。我们可以访问任何已被识别出来的特征并通过人工方式将它们移出数据,也可以使用 FeatureSelector 中的 remove 函数。
这里我们将介绍其中每种识别方法,还将展示如何同时运行这 5 种方法。此外,FeatureSelector 还有几个图表绘制功能,因为可视化地检查数据是机器学习的一大关键部分。

4 缺失值

查找和移除特征的第一个方法很简单:查找缺失值比例超过特定阈值的特征。下面的调用能识别缺失值比例超过 60% 的特征(粗体是输出结果)。
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.identify_missing(missing_threshold = 0.6)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">17 features with greater than 0.60 missing values.</span></section>
要查看待移除特征,我们可以读取 FeatureSelector 的 ops 属性,这是一个 Python 特征词典,特征会以列表的形式给出。
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">missing_features = fs.ops['missing']</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">missing_features[:5]</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">['OWN_CAR_AGE',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'YEARS_BUILD_AVG',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'COMMONAREA_AVG',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'FLOORSMIN_AVG',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'LIVINGAPARTMENTS_AVG']</span></section>
最后,我们可以绘制一张所有特制的缺失值分布图:
GitHub标星1.3k!一款功能强大的特征选择工具

5 共线性特征

共线性特征是指彼此之间高度关联的特征。在机器学习领域,高方差和较低的模型可解释性导致在测试集上的泛化能力下降。
identify_collinear 方法能基于指定的相关系数值查找共线性特征。对于每一对相关的特征,它都会标识出其中要移除的一个(因为我们只需要移除其中一个):
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.identify_collinear(correlation_threshold = 0.98)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">21 features with a correlation magnitude greater than 0.98.</span></section>
使用热图可以很好地可视化共线性。下图展示了所有至少有一个相关关系(correlation)超过阈值的特征:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.plot_collinear()</span></section>
GitHub标星1.3k!一款功能强大的特征选择工具
和之前一样,我们可以访问将会被移除的整个相关特征列表,或者在一个 dataframe 中查看高度相关的特征对。
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># list of collinear features to remove</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">collinear_features = fs.ops['collinear']</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># dataframe of collinear features</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.record_collinear.head()</span></section>
GitHub标星1.3k!一款功能强大的特征选择工具
如果我们想全面了解数据集,我们还可以通过将 plot_all = True 传入该调用,绘制出数据中所有相关性的图表:
GitHub标星1.3k!一款功能强大的特征选择工具

6 零重要度特征

前面两种方法可被应用于任何结构化的数据集并且结果是确定的——对于一个给定的阈值,每次结果都一样。接下来的方法是专为监督式机器学习问题设计的,其中我们有训练模型的标签并且是非确定性的。identify_zero_importance 函数能根据梯度提升机(GBM)学习模型查找重要度为零的特征。
我们可以使用基于树的机器学习模型(比如 boosting ensemble)求取特征重要度。这个重要度的绝对值没有相对值重要,我们可以将相对值用于确定对一个任务而言最相关的特征。我们还可以通过移除零重要度特征来在特征选择中使用特征重要度。在基于树的模型中,零重要度的特征不会被用于分割任何节点,所以我们可以移除它们而不影响模型表现。
FeatureSelector 能使用来自 LightGBM 库的梯度提升机来得到特征重要度。为了降低方差,所得到的特征重要度是在 GBM 的 10 轮训练上的平均。另外,该模型还使用早停(early stopping)进行训练(也可关闭该选项),以防止在训练数据上过拟合。
下面的代码调用了该方法并提取出了零重要度特征:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># Pass in the appropriate parameters</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.identify_zero_importance(task = 'classification',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> eval_metric = 'auc',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> n_iterations = 10,</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"> early_stopping = True)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># list of zero importance features</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">zero_importance_features = fs.ops['zero_importance']</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">63 features with zero importance after one-hot encoding.</span></section>
我们传入的参数解释如下:
task:根据我们的问题,要么是「classification」,要么是「regression」
eval_metric:用于早停的度量(如果早停禁用了,就不必使用)
n_iterations:训练轮数,最后结果取多轮的平均
early_stopping:是否为训练模型使用早停
这时候我们可以使用 plot_feature_importances 绘制两个图表:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># plot the feature importances</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.plot_feature_importances(threshold = 0.99, plot_n = 12)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">124 features required for 0.99 of cumulative importance</span></section>
GitHub标星1.3k!一款功能强大的特征选择工具
左图给出了 plot_n 最重要的特征(重要度进行了归一化,总和为 1)。右图是对应特征数量的累积重要度。蓝色竖线标出了累积重要度为 99% 的阈值。
对于基于重要度的方法,有两点需要记住:
训练梯度提升机是随机的,这意味着模型每次运行后,特征重要度都会改变。
这应该不会有太大的影响(最重要的特征不会突然就变成最不重要的),但这会改变某些特征的排序,也会影响识别出的零重要度特征的数量。如果特征重要度每次都改变,请不要感到惊讶!
要训练机器学习模型,特征首先要经过 one-hot 编码。这意味着某些被识别为零重要度的特征可能是在建模过程中加入的 one-hot 编码特征。
当我们到达特征移除阶段时,还有一个选项可移除任何被添加进来的 one-hot 编码的特征。但是,如果我们要在特征选择之后做机器学习,我们还是必须要 one-hot 编码这些特征。

7 低重要度特征

接下来的方法基于零重要度函数,使用来自模型的特征重要度来进一步选择。identify_low_importance 函数能找到重要度最低的特征,这些特征无助于指定的总重要性。
比如,下面的调用能找到最不重要的特征,即使没有这些特征也能达到 99% 的重要度。
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.identify_low_importance(cumulative_importance = 0.99)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">123 features required for cumulative importance of 0.99 after one hot encoding.</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">116 features do not contribute to cumulative importance of 0.99.</span></section>
根据前面的累积重要度图和这一信息,梯度提升机认为很多特征都与学习无关。重申一下,每次训练运行后该方法的结果都不一样。
我们也可以在一个 dataframe 中查看所有特征重要度:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.feature_importances.head(10)</span></section>
GitHub标星1.3k!一款功能强大的特征选择工具
low_importance 方法借鉴了主成分分析(PCA)中的一种方法,其中仅保留维持一定方差比例(比如 95%)所需的主成分是很常见的做法。要纳入考虑的总重要度百分比基于同一思想。
只有当我们要用基于树的模型来做预测时,基于特征重要度的方法才真正有用。除了结果随机之外,基于重要度的方法还是一种黑箱方法,也就是说我们并不真正清楚模型认为某些特征无关的原因。如果使用这些方法,多次运行它们看到结果的改变情况,也许可以创建具有不同参数的多个数据集来进行测试!

8 单个唯一值特征

最后一个方法相当基础:找出任何有单个唯一值的列。仅有单个唯一值的特征不能用于机器学习,因为这个特征的方差为 0。举个例子,如果一个特征仅有一个值,那么基于树的模型就永远不能进行区分(因为没有可做区分的依据)。
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.identify_single_unique()</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">4 features with a single unique value.</span></section>
我们可以绘制每个类别唯一值数量的直方图:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.plot_unique()</span></section>
GitHub标星1.3k!一款功能强大的特征选择工具

9 移除特征

在确定了待移除特征之后,我们有两种移除它们的选择。所有要移除的特征都存储在 FeatureSelector 的 ops 词典中,我们可以使用这个列表来手动移除它们,当然也可使用内置的 remove 函数。
对于这一方法,我们需传入要用于移除特征的 methods。如果我们想使用所实现的所有方法,我们只需使用 methods = 'all'
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"># Remove the features from all methods (returns a df)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">train_removed = fs.remove(methods = 'all')</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">['missing', 'single_unique', 'collinear', 'zero_importance', 'low_importance'] methods have been runRemoved 140 features.</span></section>
这个方法会返回一个包含被移除特征的 dataframe。另外,要移除在机器学习过程中创建的 one-hot 编码的特征:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">train_removed_all = fs.remove(methods = 'all', keep_one_hot=False)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">Removed 187 features including one-hot features.</span></section>
在执行操作之前检查将被移除的特征可能是个好想法!原来的数据集会被存储在 FeatureSelector 的 data 属性中用作备份!

10 一次运行所有方法

除了单独使用各个方法之外,我们也可通过 identify_all 一次性使用所有方法。我们需要使用一个词典来设定其中每个方法的参数:
<section style="margin: auto auto 20px;max-width: 100%;vertical-align: middle;display: block;background-image: initial;background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;height: auto;overflow-x: auto;color: rgb(0, 0, 0);padding: 5px !important;box-sizing: border-box !important;overflow-wrap: break-word !important;line-height: 1.5 !important;font-family: "Courier New", sans-serif !important;font-size: 12px !important;border-width: 1px !important;border-style: solid !important;border-color: rgb(204, 204, 204) !important;border-radius: 3px !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">fs.identify_all(selection_params = {'missing_threshold': 0.6,</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'correlation_threshold': 0.98,</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'task': 'classification',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'eval_metric': 'auc',</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> <span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">'cumulative_importance': 0.99})</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">151 total features out of 255 identified for removal after one-hot encoding.</span></section>

总结

这个特征选择器类实现了训练机器学习模型之前几种用于移除特征的常见操作。其提供了可用于识别待移除特征的函数以及可视化函数。这些方法可以单独使用,也可以一次全部应用以实现高效的工作流程。
其中 missing、collinear 和 single_unique 方法是确定性的,而基于特征重要度的方法会随每次运行而变化。与机器学习领域很相似,特征选择很大程度上是实证式的,需要测试多种组合才能找到最优解。最好的做法是在流程中尝试多种配置,并且 FeatureSelector 提供了一种用于快速评估特征选择参数的方法。

原文链接:

https://www.cnblogs.com/whig/p/9285736.html


<section style="margin-right: 8px;margin-left: 8px;white-space: normal;max-width: 100%;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;widows: 1;background-color: rgb(255, 255, 255);line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="font-size: 16px;letter-spacing: 0.544px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></span></section><section style="white-space: normal;max-width: 100%;box-sizing: border-box;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;widows: 1;background-color: rgb(255, 255, 255);color: rgb(255, 97, 149);overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;box-sizing: border-box;opacity: 0.8;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box;letter-spacing: 0.544px;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;box-sizing: border-box;opacity: 0.8;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="margin-right: 8px;margin-bottom: 15px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-family: sans-serif;font-size: 12px;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></p><section style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-family: sans-serif;font-size: 12px;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;">微软 VS Code 已原生支持 Jupyter 笔记本,再也不用打开网页调试运行了<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section><section style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-family: sans-serif;font-size: 12px;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;">作为 IT 行业的过来人,你有什么话想对后辈说的?<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section><section style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-family: sans-serif;font-size: 12px;letter-spacing: 0.5px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">程序员真的是太太太太太太太太难了!<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section><section style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-family: sans-serif;font-size: 12px;letter-spacing: 0.5px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">深度学习必懂的13种概率分布<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section><section style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-family: sans-serif;font-size: 12px;letter-spacing: 0.5px;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;">【微软】AI-神经网络基本原理简明教程</section></section></section></section></section></section></section></section></section><section style="white-space: normal;max-width: 100%;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;widows: 1;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"></section>

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享