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 ...
随机推荐
- NC20154 [JSOI2007]建筑抢修
题目链接 题目 题目描述 小刚在玩JSOI提供的一个称之为"建筑抢修"的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严 ...
- NC235247 Sramoc问题
题目链接 题目 题目描述 \(Sramoc(K ,M)\) 表示用数字 \(0,1,2,3,4,...,k-1\) 组成的自然数中能被M整除的最小数.给定 \(K,M\) \(2\leq K\leq ...
- NC16655 [NOIP2005]过河
题目链接 题目 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可 ...
- 每月免费调用1000次API调用:实现PDF转档、页面编辑、OCR
每月1000次免费PDF API调用: 使用ComPDFKit API充分发挥您PDF转换的全部潜力 您是否在寻找无需前期投资即可提升软件集成能力的途径?再也不用找了!我们先进的API为您的项目提供所 ...
- Python异步编程原理篇之IO多路复用模块selector
selector 简介 selector 是一个实现了IO复用模型的python包,实现了IO多路复用模型的 select.poll 和 epoll 等函数. 它允许程序同时监听多个文件描述符(例如套 ...
- 【Unity3D】边缘检测特效
1 边缘检测原理 边缘检测的原理是:检测每个像素周围的像素亮度差,如果亮度差异较大,就将该像素识别为边缘,并进行边缘着色. 本文完整资源见→Unity3D边缘检测特效. 使用过卷积神经网络 ...
- 【Unity3D】MonoBehaviour的生命周期
1 前言 Unity3D 中可以给每个游戏对象添加脚本,这些脚本必须继承 MonoBehaviour,用户可以根据需要重写 MonoBehaviour 的部分生命周期函数,这些生命周期函数由系统自 ...
- Spring源码之-AOP
目录 一.大话AOP 1.AOP的概念 2.必要的准备工作 什么是代理模式? 3.大话AOP 那么AOP 具体是什么呢? 实现AOP的方式 二.动态AOP自定义标签 1.JDK动态代理 2.CGLIB ...
- mysql中如何批量生成百万级数据
# 准备 #1. 准备表 create table s1( id int, name varchar(20), gender char(6), email varchar(50), first_nam ...
- CUDA、CUDNN 安装
安装 CUDA.CUDNN 1. CUDA CUDA 是 NVIDIA 发明的一种并行计算平台和编程模型.它通过利用图形处理器 (GPU) 的处理能力,可大幅提升计算性能. 官方地址 https:// ...