当前位置:网站首页>机器学习——特征选择
机器学习——特征选择
2022-07-30 12:29:00 【小羊星球】
机器学习——特征选择
一、读取数据
0、导入一些必需的包
import pandas as pd
import numpy as np
1、加载本地数据
data = pd.read_excel('./L_roudoukou_data_LSTM_CNN.xlsx', index_col=0, nrows=5)
2、检查数据是否包含缺失值
print(np.isnan(df).any()) # L12输出True,说明数据里有空值,用0填充。

3、分配标签和特征
names = ['label', 'L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10', 'L11', 'L12']
dataframe = pd.read_csv("./L_roudoukou_data_LSTM_CNN.csv", names=names) # 读取数据集
dataframe.head()
array = dataframe.values
**注:不要选到标签栏,否则XY就会包含字符串类型,后面无法计算。**
X = array[1:, 1:13] # 选取前1~13列为特征变量,就是要筛选的特征。
Y = array[1:, 0] # 选取label为目标变量,就是标签。
# print(X[0:5, :]) # 打印前5行特征
# print(Y[0:5]) # 打印前5行标签

二、特征选择
1. 过滤方法
1.1. 信息增益(mutual_info_classif互信息)
from sklearn.feature_selection import mutual_info_classif
import matplotlib.pyplot as plt
importances = mutual_info_classif(X, Y)
feat_importances = pd.Series(importances, df.columns[0:len(df.columns)-1])
feat_importances.plot(kind='barh', color='teal')
plt.savefig('./mutual_info_classif.png')
plt.show()
plt.close()

2. 包装方法
2.1 递归特征消除法,
# from sklearn.feature_selection import RFE # 导入RFE库
# from sklearn.linear_model import LogisticRegression # 导入逻辑回归库
# model = LogisticRegression() # 设置算法为逻辑回归
# rfe = RFE(model, 5) # 选择5个最佳特征变量,并进行RFE
# fit = rfe.fit(X, Y) # 进行RFE递归
# print(fit.n_features_) # 打印最优特征变量数
# print(fit.support_) # 打印选择的最优特征变量
# # print(fit.ranking_) # 特征消除排序
这里输出[False False False False False True False True True False True True],说明L6,L8,L9,L11,L12,比较好。
3. 嵌入方法
3.1 基于树模型的特征选择,
from sklearn.ensemble import ExtraTreesClassifier # 导入ExtraTrees
model = ExtraTreesClassifier() # 设置ExtraTrees
model.fit(X, Y)
print(model.feature_importances_) # 得到特征变量的重要性值
输出[0.08752002 0.08456882 0.09494643 0.05386247 0.05954704 0.14586148
0.05104781 0.05556901 0.11572619 0.06029772 0.06241888 0.12863414],从高到低:L6\L12\L9\L3\L1\L2\L11\L10\L5\L8\L4\L7。
三、总结
三种方法比较后,L6最好。
边栏推荐
- Hu-cang integrated e-commerce project (1): project background and structure introduction
- Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
- Breaking the principle and introducing SQL, what does MongoDB want to do???
- 亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人
- saltstack学习3模块
- [PostgreSQL] - Storage structure and cache shared_buffers
- Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
- 爱可可AI前沿推介(7.30)
- 每天学一点Scala之 伴生类和伴生对象
- 什么是私有云?您应该知道的 6 个优势
猜你喜欢
随机推荐
JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
PanGu-Coder: 函数级的代码生成模型
Dolphinscheduler stand-alone transformation
Decoding Redis' most overlooked high CPU and memory usage issues
什么是驱动程序签名,驱动程序如何获取数字签名?
在 Scala 中读取整个文件
Execution order of select, from, join, on where groupby, etc. in MySQL
[BJDCTF2020]Cookie is so stable-1|SSTI injection
[SCTF2019]Flag Shop
我又造了个轮子:GrpcGateway
saltstack学习3模块
IO/multiplexing (select/poll/epoll)
Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
基于卷积神经网络与双向长短时融合的锂离子电池剩余使用寿命预测
JS事件的相关特性以及原理
基于反步积分滑模摩擦补偿的光电伺服转台控制
shell的理解
datax开启hana支持以及dolphinscheduler开启datax任务
智能指针实现猜想
亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人


![[BJDCTF2020]Cookie is so stable-1|SSTI injection](/img/48/34955bbe3460ef09a5b8213c7cc161.png)






![[SCTF2019]Flag Shop](/img/26/20e21ec873f41f2633703216453a44.png)