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. Ubuntu20.04/22.04 ESP32 命令行开发环境配置

    ESP32 芯片系列 ESP32分三个系列 ESP32-S ESP32-S3: Xtensa 32位 LX7 双核 240 MHz, 384KB ROM, 512KB SRAM, QFN7x7, 56 ...

  2. Javascript中的var变量声明作用域问题

    先看一下这两段代码的执行结果 var name2 = 'What!'; function a() { if (typeof name2 === 'undefined') { console.log(' ...

  3. 【Unity3D】人机交互Input

    1 前言 ​ Input 是 Unity3D 中用于人机交互的工具类,用户可以调用其 GetKey.GetMousePosition.GetMouseButton.GetAxis.GetButton ...

  4. Idea:Fetch failed: fatal: Could not read from remote repository

    今天在idea工具中fetch github仓库报错:Fetch failed: fatal: Could not read from remote repository 查了以下需要调整下setti ...

  5. harbor镜像仓搭建相关问题

    1 环境 自己生成了SSL证书 证书目录与 harbor.cfg 文件中定义的路径需要一致 使用的是offline 包安装 执行 install.sh 脚本后,通过浏览器远程访问成功, 但是在别的机器 ...

  6. Mac M1 在PyCharm中安装(支持GPU)TensorFlow 方法

    本文介绍在Mac M1的PyCharm中安装TensorFlow与创建工程的方法,在2021的MacBook Pro (M1 Max处理器)验证OK. 安装TensorFlow与创建工程是在Minif ...

  7. ubuntu18.04更换下载源

    步骤一 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 步骤二 vim /etc/apt/sources.list 步骤三 # http ...

  8. sqlserver数据库jar包下载

    链接:https://pan.baidu.com/s/1mCx5JpVpmU6uUaqMITxP_Q提取码:4piq 说明:若链接失效,联系会及时补上!

  9. 【LeetCode动态规划#09】完全背包问题实战,其二(零钱兑换和完全平方数--求物品放入个数)

    零钱兑换 力扣题目链接(opens new window) 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能 ...

  10. 【LeetCode二叉树#10】从中序与后序(或者前序)遍历序列构造二叉树(首次构造二叉树)

    从中序与后序遍历序列构造二叉树 力扣题目链接(opens new window) 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorde ...