sklearn:sklearn.feature_selection的SelectFromModel函数的简介、使用方法之详细攻略(一)

简介: sklearn:sklearn.feature_selection的SelectFromModel函数的简介、使用方法之详细攻略

SelectFromModel函数的简介


       SelectFromModel is a meta-transformer that can be used along with any estimator that has a coef_ or feature_importances_ attribute after fitting. The features are considered unimportant and removed, if the corresponding coef_ or feature_importances_ values are below the provided threshold parameter. Apart from specifying the threshold numerically, there are built-in heuristics for finding a threshold using a string argument. Available heuristics are “mean”, “median” and float multiples of these like “0.1*mean”.

       SelectFromModel是一个元转换器,可以与任何在拟合后具有coef_或feature_importances_属性的estimator 一起使用。如果相应的coef_或feature_importances_值低于提供的阈值参数,则认为这些特性不重要并将其删除。除了以数字方式指定阈值外,还有使用字符串参数查找阈值的内置启发式方法。可用的试探法是“平均数”、“中位数”和这些数的浮点倍数,如“0.1*平均数”。




官网API:https://scikit-learn.org/stable/modules/feature_selection.html#feature-selection-using-selectfrommodel


 """Meta-transformer for selecting features based on importance weights.    .. versionadded:: 0.17

用于根据重要性权重来选择特征的元转换器。

. .加入在0.17版本::

     Parameters

   ----------

   estimator : object

   The base estimator from which the transformer is built.

   This can be both a fitted (if ``prefit`` is set to True)

   or a non-fitted estimator. The estimator must have either a

   ``feature_importances_`` or ``coef_`` attribute after fitting.

 

   threshold : string, float, optional default None

   The threshold value to use for feature selection. Features whose

   importance is greater or equal are kept while the others are

   discarded. If "median" (resp. "mean"), then the ``threshold`` value is

   the median (resp. the mean) of the feature importances. A scaling

   factor (e.g., "1.25*mean") may also be used. If None and if the

   estimator has a parameter penalty set to l1, either explicitly

   or implicitly (e.g, Lasso), the threshold used is 1e-5.

   Otherwise, "mean" is used by default.

 

   prefit : bool, default False

   Whether a prefit model is expected to be passed into the constructor

   directly or not. If True, ``transform`` must be called directly

   and SelectFromModel cannot be used with ``cross_val_score``,

   ``GridSearchCV`` and similar utilities that clone the estimator.

   Otherwise train the model using ``fit`` and then ``transform`` to do

   feature selection.

 

   norm_order : non-zero int, inf, -inf, default 1

   Order of the norm used to filter the vectors of coefficients below

   ``threshold`` in the case where the ``coef_`` attribute of the

   estimator is of dimension 2.

参数

estimator :对象类型,

建立转换的基本estimator 。

这可以是一个拟合(如果' ' prefit ' '被设置为True) 或者非拟合的estimator。在拟合之后,estimator 必须有' ' feature_importances_ ' '或' ' coef_ ' '属性。


threshold :字符串,浮点类型,可选的,默认无

用于特征选择的阈值。重要性大于或等于的特征被保留,其他特征被丢弃。如果“中位数”(分别地。(“均值”),则“阈值”为中位数(resp,特征重要性的平均值)。也可以使用比例因子(例如“1.25*平均值”)。如果没有,并且估计量有一个参数惩罚设置为l1,不管是显式的还是隐式的(例如Lasso),阈值为1e-5。否则,默认使用“mean”。


prefit: bool,默认为False

prefit模型是否应直接传递给构造函数。如果为True,则必须直接调用“transform”,SelectFromModel不能与cross_val_score 、GridSearchCV以及类似的克隆估计器的实用程序一起使用。否则,使用' ' fit ' '和' ' transform ' '训练模型进行特征选择。


norm_order:非零整型,inf, -inf,默认值1

在estimator的' coef_ 属性为2维的情况下,用于过滤' '阈值' '以下系数的向量的范数的顺序。

   Attributes

   ----------

   estimator_ : an estimator

   The base estimator from which the transformer is built.

   This is stored only when a non-fitted estimator is passed to the

   ``SelectFromModel``, i.e when prefit is False.

 

   threshold_ : float

   The threshold value used for feature selection.

   """

属性

estimator_:一个estimator。

建立转换器的基estimator,只有在将非拟合估计量传递给SelectFromModel 时,才会存储它。当prefit 为假时。

threshold_ :浮点类型

用于特征选择的阈值。


1、使用SelectFromModel和LassoCV进行特征选择

# Author: Manoj Kumar <mks542@nyu.edu>

# License: BSD 3 clause

print(__doc__)

import matplotlib.pyplot as plt

import numpy as np

from sklearn.datasets import load_boston

from sklearn.feature_selection import SelectFromModel

from sklearn.linear_model import LassoCV

# Load the boston dataset.

X, y = load_boston(return_X_y=True)

# We use the base estimator LassoCV since the L1 norm promotes sparsity of features.

clf = LassoCV()

# Set a minimum threshold of 0.25

sfm = SelectFromModel(clf, threshold=0.25)

sfm.fit(X, y)

n_features = sfm.transform(X).shape[1]

# Reset the threshold till the number of features equals two.

# Note that the attribute can be set directly instead of repeatedly

# fitting the metatransformer.

while n_features > 2:

   sfm.threshold += 0.1

   X_transform = sfm.transform(X)

   n_features = X_transform.shape[1]

# Plot the selected two features from X.

plt.title(

   "Features selected from Boston using SelectFromModel with "

   "threshold %0.3f." % sfm.threshold)

feature1 = X_transform[:, 0]

feature2 = X_transform[:, 1]

plt.plot(feature1, feature2, 'r.')

plt.xlabel("Feature number 1")

plt.ylabel("Feature number 2")

plt.ylim([np.min(feature2), np.max(feature2)])

plt.show()


 


相关文章
|
关系型数据库 PostgreSQL 索引
PostgreSQL 11 新特性解读:分区表支持创建主键、外键、索引
PostgreSQL 10 版本虽然支持创建范围分区表和列表分区表,但创建过程依然比较繁琐,需要手工定义子表索引、主键,详见 PostgreSQL10:重量级新特性-支持分区表,PostgreSQL 11 版本得到增强,在父表上创建索引、主键、外键后,子表上将自动创建,本文演示这三种场景。
7767 0
|
Python
Numpy学习笔记(一):array()、range()、arange()用法
这篇文章是关于NumPy库中array()、range()和arange()函数的用法和区别的介绍。
960 6
Numpy学习笔记(一):array()、range()、arange()用法
|
存储 搜索推荐 API
Electron-store本地存储功能
【10月更文挑战第18天】Electron-store 无疑为我们的 Electron 应用开发提供了强大的支持。它的本地存储功能不仅方便实用,而且性能优异,为我们打造高质量的应用提供了坚实的基础。
Vue3日期选择器(DatePicker)
该组件基于 **@vuepic/vue-datepicker@9.0.1** 进行二次封装,简化了日常使用。除范围和年选择器外,其他日期选择均返回格式化的字符串。提供了多种自定义设置,如日期选择器宽度、模式、格式等,并支持时间选择和“今天”按钮展示。详细配置及更多功能请参考[官方文档](https://vue3datepicker.com/installation/)。组件已集成所有原生属性,并支持主题颜色自定义。 示例代码展示了如何创建和使用日期选择器组件,包括基本使用、禁用日期、日期时间选择器、范围选择器等多种场景。更多功能和样式可通过官方文档了解。
2990 2
Vue3日期选择器(DatePicker)
|
存储 缓存 API
Vulkan 围炉夜话4
Vulkan 围炉夜话
302 5
|
Linux Docker Python
如何将本地的python项目部署到linux服务器中
如何将本地的python项目部署到linux服务器中
|
JavaScript 前端开发
“layui助力博客管理升级!用增删改查功能打造优质博客体验“
“layui助力博客管理升级!用增删改查功能打造优质博客体验“
162 0
|
机器学习/深度学习 PyTorch 算法框架/工具
使用PyTorch构建GAN生成对抗网络源码(详细步骤讲解+注释版)02 人脸识别 下
使用PyTorch构建GAN生成对抗网络源码(详细步骤讲解+注释版)02 人脸识别 下
|
Web App开发 JavaScript 数据安全/隐私保护
合成大西瓜开发源码,手把手教你运行和部署大西瓜游戏项目
合成大西瓜开发源码,手把手教你运行和部署大西瓜游戏项目
合成大西瓜开发源码,手把手教你运行和部署大西瓜游戏项目
|
Java
Java面向对象程序设计综合练习2(编程题)(上)
Java面向对象程序设计综合练习2(编程题)(上)
654 0