Boruta特征选择

官方github地址:https://github.com/scikit-learn-contrib/boruta_py?tab=readme-ov-file

论文地址:https://www.jstatsoft.org/article/view/v036i11

官方代码:

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from boruta import BorutaPy # load X and y
# NOTE BorutaPy accepts numpy arrays only, hence the .values attribute
X = pd.read_csv('examples/test_X.csv', index_col=0).values
y = pd.read_csv('examples/test_y.csv', header=None, index_col=0).values
y = y.ravel() # define random forest classifier, with utilising all cores and
# sampling in proportion to y labels
rf = RandomForestClassifier(n_jobs=-1, class_weight='balanced', max_depth=5) # define Boruta feature selection method
feat_selector = BorutaPy(rf, n_estimators='auto', verbose=2, random_state=1) # find all relevant features - 5 features should be selected
feat_selector.fit(X, y) # check selected features - first 5 features are selected
feat_selector.support_ # check ranking of features
feat_selector.ranking_ # call transform() on X to filter it down to selected features
X_filtered = feat_selector.transform(X)

在本地运行时出现了问题:AttributeError: module 'numpy' has no attribute 'int'. np.int was a deprecated alias for the builtin int.就是numpy的1.20版本以后的都不在支持np.int,我尝试了降低numpy版本,但是报错wheel出问题了。看了github上的issues很多人都遇到了同样的问题,解决办法就是在调用boruta = BorutaPy(estimator=rf)前加三行代码:

np.int = np.int32
np.float = np.float64
np.bool = np.bool_ boruta = BorutaPy(estimator=rf)
boruta.fit(x, y)

下面是我修改后以及适配我的需求的代码:

import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from boruta import BorutaPy
import numpy as np file_names_to_add = ['xxx', 'xxxx']
file_path2 = '../xxxx' for file_name in file_names_to_add:
input_file_path = f"{file_path2}{file_name}.xlsx"
print(input_file_path) sheet_name_nor = 'xxx' y_tos = ['xxx', '...'] for y_to in y_tos:
sheet_name_uni = y_to
print(sheet_name_uni) df = pd.read_excel(input_file_path, sheet_name=sheet_name_nor) cols_to_pre = ['xxxxxxx', 'xxxxxx','...'] missing_cols = [col for col in cols_to_pre if col not in df.columns]
if missing_cols:
print(f"{missing_cols} not found in the, skipping.")
cols_to_pre = [col for col in cols_to_pre if col in df.columns] # load X and y
# NOTE BorutaPy accepts numpy arrays only, hence the .values attribute
X = df[cols_to_pre].values
y = df[y_to].values np.int = np.int32
np.float = np.float64
np.bool = np.bool_ # define random forest classifier, with utilising all cores and
# sampling in proportion to y labels
rf = RandomForestClassifier(n_jobs=-1, class_weight='balanced', max_depth=5) # define Boruta feature selection method
feat_selector = BorutaPy(rf, n_estimators='auto', verbose=2, random_state=1) # find all relevant features - 5 features should be selected
feat_selector.fit(X, y) # # check selected features - first 5 features are selected
# feat_selector.support_ # # check ranking of features
# feat_selector.ranking_ # call transform() on X to filter it down to selected features
# X_filtered = feat_selector.transform(X)
selected_features = [cols_to_pre[i] for i, support in enumerate(feat_selector.support_) if support] print('Selected features: ', selected_features)
print('Feature ranking: ', feat_selector.ranking_)

因为'feat_selector.support_' 放回的是一个布尔数组,当我们想打印出选出来的特征时直接打印不行,需要通过使用布尔索引来解决这个问题。

selected_features = [cols_to_pre[i] for i, support in enumerate(feat_selector.support_) if support]

上段代码遍历 cols_to_pre 列表,并且只选择 feat_selector.support_ 中为 True 的列。

Boruta特征选择的更多相关文章

  1. 特征选择Boruta

    A good feature subset is one that: contains features highly correlated with (predictive of) the clas ...

  2. 挑子学习笔记:特征选择——基于假设检验的Filter方法

    转载请标明出处: http://www.cnblogs.com/tiaozistudy/p/hypothesis_testing_based_feature_selection.html Filter ...

  3. 用信息值进行特征选择(Information Value)

    Posted by c cm on January 3, 2014 特征选择(feature selection)或者变量选择(variable selection)是在建模之前的重要一步.数据接口越 ...

  4. MIL 多示例学习 特征选择

    一个主要的跟踪系统包含三个成分:1)外观模型,通过其可以估计目标的似然函数.2)运动模型,预测位置.3)搜索策略,寻找当前帧最有可能为目标的位置.MIL主要的贡献在第一条上. MIL与CT的不同在于后 ...

  5. 【转】[特征选择] An Introduction to Feature Selection 翻译

    中文原文链接:http://www.cnblogs.com/AHappyCat/p/5318042.html 英文原文链接: An Introduction to Feature Selection ...

  6. 单因素特征选择--Univariate Feature Selection

    An example showing univariate feature selection. Noisy (non informative) features are added to the i ...

  7. 主成分分析(PCA)特征选择算法详解

    1. 问题 真实的训练数据总是存在各种各样的问题: 1. 比如拿到一个汽车的样本,里面既有以“千米/每小时”度量的最大速度特征,也有“英里/小时”的最大速度特征,显然这两个特征有一个多余. 2. 拿到 ...

  8. 干货:结合Scikit-learn介绍几种常用的特征选择方法

    原文  http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...

  9. 【Machine Learning】wekaの特征选择简介

    看过这篇博客的都应该明白,特征选择代码实现应该包括3个部分: 搜索算法: 评估函数: 数据: 因此,代码的一般形式为: AttributeSelection attsel = new Attribut ...

  10. weka特征选择(IG、chi-square)

    一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...

随机推荐

  1. 扯淡的DevOps,我们开发根本不想做运维!

    引言 最初考虑引用" DevOps 已死,平台工程才是未来"作为标题,但这样的表达可能太过于绝对.最终,决定用了"扯淡的"这个词来描述 DevOps,但这并不是 ...

  2. STM32的时钟控制RCC和外设定时器

    STM32的RCC(Reset and Clock Control)时钟控制 stm32f103c8的时钟是72MHz, stm32f401ccu6的时钟是80M, 开发板板载两个晶振, 一个高速一个 ...

  3. 【Unity3D】UGUI之Dropdown

    1 Dropdown属性面板 ​ 在 Hierarchy 窗口右键,选择 UI 列表里的 Dwondown (下拉列表)控件,即可创建 Dwondown 控件,选中创建的 Dwondown 控件,按键 ...

  4. CSS隐藏元素的方法

    CSS隐藏元素的方法 使用CSS隐藏元素的主要方式有diaplay: none;.opacity: 0;.visibility: hidden;.position: absolute; overflo ...

  5. 如何设置mysql root密码

    如何设置mysql root密码: 1.首次安装 root初始密码为空,注意就是没有密码,直接敲回车即可进入 D:\wamp\bin\mysql\mysql5.6.17\bin> mysql - ...

  6. Spring Boot整合JWT实现接口访问认证

    最近项目组需要对外开发相关API接口,需要对外系统进行授权认证.实现流程是先给第三方系统分配appId和appSecret,第三方系统调用我getToken接口获取token,然后将token填入Au ...

  7. Java集合框架学习(七) Vector详解

    Vector介绍 Vector 实现了List接口.和ArrayList一样也维护元素的插入顺序. 但它一般只用在多线程环境,因为它是线程同步的. 还有就是它对元素的增删改查效率低下. 类定义 pub ...

  8. abc模块的用法

    首先需要了解的是一个基类(父类),abc.ABCMeta.这个是用于实现抽象类的一个基础类 抽象方法的使用,在相应的方法之前一行加上@abstractmethod之后,从新的一行开始定义相应的方法.实 ...

  9. 解析Spring中的循环依赖问题:初探三级缓存

    什么是循环依赖? 这个情况很简单,即A对象依赖B对象,同时B对象也依赖A对象,让我们来简单看一下. // A依赖了B class A{ public B b; } // B依赖了A class B{ ...

  10. 关于无法查看hadoop的防火墙状态解决方法

    可以参考这两位博主写的 https://www.055401.com/computer/376.html https://blog.csdn.net/weixin_52596632/article/d ...