Boruta特征选择
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特征选择的更多相关文章
- 特征选择Boruta
A good feature subset is one that: contains features highly correlated with (predictive of) the clas ...
- 挑子学习笔记:特征选择——基于假设检验的Filter方法
转载请标明出处: http://www.cnblogs.com/tiaozistudy/p/hypothesis_testing_based_feature_selection.html Filter ...
- 用信息值进行特征选择(Information Value)
Posted by c cm on January 3, 2014 特征选择(feature selection)或者变量选择(variable selection)是在建模之前的重要一步.数据接口越 ...
- MIL 多示例学习 特征选择
一个主要的跟踪系统包含三个成分:1)外观模型,通过其可以估计目标的似然函数.2)运动模型,预测位置.3)搜索策略,寻找当前帧最有可能为目标的位置.MIL主要的贡献在第一条上. MIL与CT的不同在于后 ...
- 【转】[特征选择] An Introduction to Feature Selection 翻译
中文原文链接:http://www.cnblogs.com/AHappyCat/p/5318042.html 英文原文链接: An Introduction to Feature Selection ...
- 单因素特征选择--Univariate Feature Selection
An example showing univariate feature selection. Noisy (non informative) features are added to the i ...
- 主成分分析(PCA)特征选择算法详解
1. 问题 真实的训练数据总是存在各种各样的问题: 1. 比如拿到一个汽车的样本,里面既有以“千米/每小时”度量的最大速度特征,也有“英里/小时”的最大速度特征,显然这两个特征有一个多余. 2. 拿到 ...
- 干货:结合Scikit-learn介绍几种常用的特征选择方法
原文 http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...
- 【Machine Learning】wekaの特征选择简介
看过这篇博客的都应该明白,特征选择代码实现应该包括3个部分: 搜索算法: 评估函数: 数据: 因此,代码的一般形式为: AttributeSelection attsel = new Attribut ...
- weka特征选择(IG、chi-square)
一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...
随机推荐
- NC19782 Tree
题目链接 题目 题目描述 修修去年种下了一棵树,现在它已经有n个结点了. 修修非常擅长数数,他很快就数出了包含每个点的连通点集的数量. 澜澜也想知道答案,但他不会数数,于是他把问题交给了你. 输入描述 ...
- 【OpenGL ES】光影(光照与阴影)效果
1 前言 Blinn改进的冯氏光照模型 中只展示了光照效果,本文将进一步展示阴影效果. 绘制阴影,需要用到深度纹理,即从光源角度看模型并绘制一张纹理图,纹理图的颜色代表了模型上的点离光源的深度 ...
- col命令
col命令 在很多UNIX说明文件里,都有RLF控制字符,当我们把说明文件的内容输出成纯文本文件时,控制字符会变成乱码,col命令则能有效滤除这些控制字符. 语法 col [options] 参数 - ...
- ElementUI导入Excel文件
功能介绍 最近用ElementUI做管理系统需要把excel数据导入到系统内,我想这是一个很常见的功能点,把它分享出来,希望对大家有所帮助:) 实现效果 实现步骤 1.定义导入组件 <el-up ...
- Fiddler捕获Java发送的HttpURLConnection请求
1.说明 平常使用Fiddler抓包工具查看浏览器的请求和响应信息很方便, 但有时候我们也需要拦截java代码执行的http请求. 以便更好的调试程序.具体方法如下: 2.编写Java代码 // 配置 ...
- 使用webgl(three.js)创建自动化抽象化3D机房,3D机房模块详细介绍(抽象版一)
目前市面上有两种机房 一种是普通机房 一种是由微模块组成的机房,本文主要介绍普通机房的抽象化体现模式. 抽象机房模式:机房展示过程中,我们需要对机房进行建模,当遇到大量机房需要建模时,这无疑是巨大工作 ...
- vmware之NAT模式配置
题外话之前的题外话,本文迁移自别的社区,三年前大学实习时写下本文,过了几年再回过头来看,虽然讲得浅显,作为入门笔记也勉强合格. ---------------------------------- ...
- python装饰器保留原有函数名称和属性functools.wraps()
# python装饰器在实现的时候,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变),为了不影响,python的functools包中提供了一个叫wraps的decorator来消 ...
- git开发流程图解,本地分支合并,并推送远程分支步骤
本地分支合并,并推送远程分支步骤 1.只有当将修改内容commit后 该修改才完全生效,进行merge前需要将两个分支修改的内容都进行commit 2.假设本地两个分支 用于开发的分支:dev 用于同 ...
- python代码,读取一个txt文件,将其中的每一行开头加上一个字母a,每一行的结尾加上一个字母b
with open('name.txt', 'r+') as file: lines = file.readlines() file.seek(0) # 将文件指针移回文件开头 file.trunca ...