Python方差特征过滤的实例分析
作者:小妮浅浅 时间:2021-08-11 01:12:56
说明
1、通过特征本身的方差来筛选特征。特征的方差越小,特征的变化越不明显。
2、变化越不明显的特征对我们区分标签没有太大作用,因此应该消除这些特征。
实例
def variance_demo():
"""
过滤低方差特征
:return:
"""
# 1. 获取数据
data = pd.read_csv('factor_returns.csv')
data = data.iloc[:, 1:-2]
print('data:\n', data)
# 2. 实例化一个转换器类
transfer = VarianceThreshold(threshold=10)
# 3. 调用fit_transform()
data_new = transfer.fit_transform(data)
print('data_new:\n', data_new, data_new.shape)
return None
知识点扩充:
方差过滤法
VarianceThreshold 是特征选择的一个简单基本方法,其原理在于–底方差的特征的预测效果往往不好。而VarianceThreshold会移除所有那些方差不满足一些阈值的特征。默认情况下,它将会移除所有的零方差特征,即那些在所有的样本上的取值均不变的特征。
sklearn中的VarianceThreshold类中重要参数 threshold(方差的阈值),表示删除所有方差小于threshold的特征 #不填默认为0——删除所有记录相同的特征。
import pandas as pd
import numpy as np
np.random.seed(1) #设置随机种子,实现每次生成的随机数矩阵都一样
a= np.random.randint(0, 200,10)
b= np.random.randint(0, 200,10)
c= np.random.randint(0, 200,10)
d= [9,9,9,9,9,9,9,9,9,9]
data=pd.DataFrame({"A" : a,"B" : b,"C" : c,"D" : d})
data
from sklearn.feature_selection import VarianceThreshold
sel_model = VarianceThreshold(threshold = 0)
#删除不合格特征之后的新矩阵
sel_model.fit_transform(data)
来源:https://www.py.cn/jishu/jichu/32599.html
标签:Python,方差特征,过滤
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python数字图像处理代数之加减乘运算
2023-03-20 07:51:26
![](https://img.aspxhome.com/file/2023/7/82667_0s.png)
Python接口自动化之接口依赖
2021-09-03 15:53:09
![](https://img.aspxhome.com/file/2023/3/91893_0s.png)
python使用正则表达式(Regular Expression)方法超详细
2022-09-07 14:30:53
![](https://img.aspxhome.com/file/2023/4/109934_0s.png)
简单的asp采集代码教程
2011-04-18 10:39:00
用python实现海龟赛跑小游戏
2023-03-14 22:59:39
![](https://img.aspxhome.com/file/2023/2/112152_0s.png)
Python实现的爬取百度贴吧图片功能完整示例
2021-06-30 19:22:13
不同浏览器空格的宽度
2007-08-22 08:29:00
![](https://img.aspxhome.com/file/UploadPic/20078/22/200782283622920s.gif)
Python采集王者皮肤图片实战示例
2021-08-02 12:09:23
![](https://img.aspxhome.com/file/2023/7/106967_0s.jpg)
python内置模块collections知识点总结
2023-10-27 19:37:08
Pandas聚合运算和分组运算的实现示例
2023-11-10 03:49:14
Python使用爬虫猜密码
2023-03-21 12:38:39
![](https://img.aspxhome.com/file/2023/6/67766_0s.png)
Python中类的初始化特殊方法
2021-05-26 14:50:01
教你两步解决conda安装pytorch时下载速度慢or超时的问题
2022-04-12 17:19:59
![](https://img.aspxhome.com/file/2023/5/110435_0s.png)
python和c语言哪个更适合初学者
2022-06-22 08:23:29
python 删除非空文件夹的实例
2022-11-07 01:25:23
Python数据结构与算法之常见的分配排序法示例【桶排序与基数排序】
2022-06-12 22:00:40
python之pil的使用详解
2023-01-19 07:21:19
![](https://img.aspxhome.com/file/2023/8/91988_0s.jpg)
Javascript:window对象出身何处
2007-08-28 15:16:00
tensorflow入门之训练简单的神经网络方法
2021-07-16 14:21:51
python冒泡排序简单实现方法
2022-09-27 12:28:57