机器学习11-不平衡数据之采样
文章目录
- 1.过采样(上采样)
- 1)随机过采样
- 2)SMOTE方法
- 3).Border-line SMOTE
- 4).ADASYN-自适应合成采样
- 2.下采样-降采样
- 1)原型生成
- 2)原型选择
- 3)NearMiss
- 3.过采样与下采样结合
- 1)SMOTEENN
- 2)SMOTETomek
1.过采样(上采样)
1)随机过采样
# 统计数据
from collections import Counterimport matplotlib.pyplot as plt
# 过采样
from imblearn.over_sampling import RandomOverSampler
# 构造数据
from sklearn.datasets import make_classificationX, y = make_classification(n_samples=50, n_features=2, n_informative=2,n_redundant=0, n_repeated=0, n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],class_sep=0.8, random_state=0)print(Counter(y)) # Counter({2: 47, 1: 2, 0: 1})
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()ros = RandomOverSampler(random_state=0)
# 过采样
X_resampled, y_resampled = ros.fit_resample(X, y)plt.scatter(X_resampled[:, 0], X_resampled[:, 1], c=y_resampled)
print(Counter(y_resampled)) # Counter({2: 47, 1: 47, 0: 47})
2)SMOTE方法
SMOTE会随机选取少数类样本用以合成新样本,而不考虑周边样本的情况,这样容易带来两个问题:如果选取的少数类样本周围也都是少数类样本,则新合成的样本不会提供太多有用信息。这就像支持向量机中远离margin的点对决策边界影响不大。
如果选取的少数类样本周围都是多数类样本,这类的样本可能是噪音,则新合成的样本会与周围的多数类样本产生大部分重叠,致使分类困难。
# SMOTE算法的基本思想是对少数类样本进行分析并根据少数类样本人工合成新样本添加到数据集中
from collections import Counter
import matplotlib.pyplot as plt
# 过采样
from imblearn.over_sampling import RandomOverSampler,SMOTE
# 构造数据
from sklearn.datasets import make_classificationX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0, n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)plt.scatter(X[:,0],X[:,1],c=y)
plt.show()
print(Counter(y)) # Counter({2: 930, 1: 57, 0: 13})
X_resampled_smote,y_resampled_smote = SMOTE().fit_sample(X,y)
print(Counter(y_resampled_smote)) # Counter({2: 930, 1: 930, 0: 930})
plt.scatter(X_resampled_smote[:,0],X_resampled_smote[:,1],c=y_resampled_smote)
plt.show()
3).Border-line SMOTE
这个算法会先将所有的少数类样本分成三类,如下图所示:
"noise" : 所有的k近邻个样本都属于多数类
"danger" : 超过一半的k近邻样本属于多数类
"safe": 超过一半的k近邻样本属于少数类Border-line SMOTE算法只会从处于”danger“状态的样本中随机选择,然后用SMOTE算法产生新的样本。
处于”danger“状态的样本代表靠近”边界“附近的少数类样本,而处于边界附近的样本往往更容易被误分类。
因而 Border-line SMOTE 只对那些靠近”边界“的少数类样本进行人工合成样本,而 SMOTE 则对所有少
数类样本一视同仁。Border-line SMOTE 分为两种: Borderline-1 SMOTE 和 Borderline-2 SMOTE。Borderline-1 SMOTE 在合成样本时所选的近邻是一个少数类样本,而 Borderline-2 SMOTE 中所选的k近邻中的是任意一个样本。
https://blog.csdn.net/weixin_42707617/article/details/103651183
库的改变
from collections import Counter
import matplotlib.pyplot as plt
# 过采样
from imblearn.over_sampling import BorderlineSMOTE
# 构造数据
from sklearn.datasets import make_classificationX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0, n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)plt.scatter(X[:,0],X[:,1],c=y)
plt.show()
print(Counter(y)) # Counter({2: 930, 1: 57, 0: 13})X_resampled_smote, y_resampled_smote = BorderlineSMOTE(kind="borderline-1").fit_sample(X, y)
plt.scatter(X_resampled_smote[:,0],X_resampled_smote[:,1],c=y_resampled_smote)X_resampled_smote, y_resampled_smote = BorderlineSMOTE(kind="borderline-2").fit_sample(X, y)
plt.scatter(X_resampled_smote[:,0],X_resampled_smote[:,1],c=y_resampled_smote)
plt.show()
4).ADASYN-自适应合成采样
这种改进方法的主要思想是根据数据分布情况为不同的少数类样本生成不同数量的新样本。首先根据最终的
平衡程度设定总共需要生成的新少数类样本数量 ,然后为每个少数类样本x计算分布比例。
from collections import Counter
import matplotlib.pyplot as plt
# 过采样
from imblearn.over_sampling import ADASYN
# 构造数据
from sklearn.datasets import make_classificationX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0, n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)plt.scatter(X[:,0],X[:,1],c=y)
plt.show()
print(Counter(y)) # Counter({2: 930, 1: 57, 0: 13})X_resampled_adasyn, y_resampled_adasyn = ADASYN().fit_sample(X, y)
print(Counter(y_resampled_adasyn)) # Counter({2: 930, 0: 928, 1: 923})
plt.scatter(X_resampled_adasyn[:,0],X_resampled_adasyn[:,1],c=y_resampled_adasyn)
plt.show()
2.下采样-降采样
1)原型生成
给定数据集S, 原型生成算法将生成一个子集S’, 其中|S’| < |S|, 但是子集并非来自于原始数据集.
意思就是说: 原型生成方法将减少数据集的样本数量, 剩下的样本是由原始数据集生成的, 而不是直接
来源于原始数据集
# 原型生成
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.datasets import make_classification
# 原型生成
from imblearn.under_sampling import ClusterCentroidsX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0, n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)cc = ClusterCentroids(random_state=0)
X_resampled, y_resampled = cc.fit_sample(X, y)print(Counter(y_resampled))
plt.scatter(X_resampled[:,0],X_resampled[:,1],c=y_resampled)
plt.show()
2)原型选择
原型选择算法是直接从原始数据集中进行抽取
# 原型选择
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.under_sampling import RandomUnderSamplerX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0,n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)rus = RandomUnderSampler(random_state=0)
X_resampled,y_resampled = rus.fit_resample(X,y)
print(Counter(y_resampled)) # Counter({0: 13, 1: 13, 2: 13})
plt.scatter(X_resampled[:,0],X_resampled[:,1],c= y_resampled)
plt.show()
3)NearMiss
NearMiss本质上是一种原型选择(prototype selection)方法,即从多数类样本中选取最具代表性的样本
用于训练,主要是为了缓解随机欠采样中的信息丢失问题。
NearMiss采用一些启发式的规则来选择样本,根据规则的不同可分为3类:NearMiss-1:选择到最近的K个少数类样本平均距离最近的多数类样本
NearMiss-2:选择到最远的K个少数类样本平均距离最近的多数类样本
NearMiss-3:对于每个少数类样本选择K个最近的多数类样本,目的是保证每个少数类样本都被多数类样本包围
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.under_sampling import NearMissX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0,n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)# 不同的NearMiss类别通过version参数来设置
nml = NearMiss(version=1)
X_resampled,y_resampled = nml.fit_resample(X,y)
print(Counter(y_resampled)) # Counter({0: 13, 1: 13, 2: 13})
plt.scatter(X_resampled[:,0],X_resampled[:,1],c= y_resampled)
plt.show()
version=1
version = 2
version=3
3.过采样与下采样结合
1)SMOTEENN
# 先过采样后清洗
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.combine import SMOTEENNX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0,n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)smote_enn = SMOTEENN(random_state=0)
X_resampled,y_resampled = smote_enn.fit_resample(X,y)
print(Counter(y_resampled)) # Counter({1: 783, 0: 763, 2: 627})
plt.scatter(X_resampled[:,0],X_resampled[:,1],c= y_resampled)
plt.show()
2)SMOTETomek
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.combine import SMOTETomekX, y = make_classification(n_samples=1000, n_features=2,n_informative=2, n_redundant=0, n_repeated=0,n_classes=3,n_clusters_per_class=1,weights=[0.01, 0.05, 0.94],random_state=500)smote_tomek = SMOTETomek(random_state=0)X_resampled,y_resampled = smote_tomek.fit_sample(X,y)
print(Counter(y_resampled)) # Counter({0: 890, 1: 885, 2: 863})
plt.scatter(X_resampled[:,0],X_resampled[:,1],c= y_resampled)
plt.show()
过采样:SMOTE及改进版本
下采样:原型选择
组合:推荐使用SMOTE+ENN
机器学习11-不平衡数据之采样相关推荐
- 机器学习 处理不平衡数据_在机器学习中处理不平衡数据
机器学习 处理不平衡数据 As an ML engineer or data scientist, sometimes you inevitably find yourself in a situat ...
- 数据处理之不平衡数据过采样与下采样
https://blog.csdn.net/mengjiexu_cn/article/details/97008269
- 机器学习 对不平衡数据的四种处理方法
https://blog.csdn.net/qq_40875849/article/details/85013973
- 不平衡数据采样_过度采样不平衡数据的5种打击技术
不平衡数据采样 Imbalance data is a case where the classification dataset class has a skewed proportion. For ...
- 机器学习中的数据不平衡问题----通过随机采样比例大的类别使得训练集中大类的个数与小类相当,或者模型中加入惩罚项...
机器学习中的数据不平衡问题 摘自:http://wap.sciencenet.cn/blogview.aspx?id=377102 最近碰到一个问题,其中的阳性数据比阴性数据少很多,这样的数据集在进行 ...
- 【机器学习基础】如何在Python中处理不平衡数据
特征锦囊:如何在Python中处理不平衡数据 ???? Index 1.到底什么是不平衡数据 2.处理不平衡数据的理论方法 3.Python里有什么包可以处理不平衡样本 4.Python中具体如何处理 ...
- lightgbm 数据不平衡_不平衡数据下的机器学习(下)
本文从不平衡学习的基础概念和问题定义出发,介绍了几类常见的不平衡学习算法和部分研究成果.总体来说,不平衡学习是一个很广阔的研究领域,但受笔者能力和篇幅的限制,本文仅对其中部分内容做了简单概述,有兴趣深 ...
- 如何解决机器学习中的数据不平衡问题?
在机器学习任务中,我们经常会遇到这种困扰:数据不平衡问题. 数据不平衡问题主要存在于有监督机器学习任务中.当遇到不平衡数据时,以总体分类准确率为学习目标的传统分类算法会过多地关注多数类,从而使得少数类 ...
- linux中python如何调用matlab的数据_特征锦囊:如何在Python中处理不平衡数据
今日锦囊 特征锦囊:如何在Python中处理不平衡数据 ? Index 1.到底什么是不平衡数据 2.处理不平衡数据的理论方法 3.Python里有什么包可以处理不平衡样本 4.Python中具体如何 ...
- 【机器学习】交通数据的时间序列分析和预测实战
今天又给大家带来一篇实战案例,本案例旨在运用之前学习的时间序列分析和预测基础理论知识,用一个实际案例数据演示这些方法是如何被应用的. 本文较长,建议收藏!由于篇幅限制,文内精简了部分代码,但不影响阅读 ...
最新文章
- 项目性能优化(页面静态化2)
- xp里删除不想要的删不掉的帐户
- MPO文件类型解码(四)3D图像整体结构
- nagios监控单网卡双IP
- 关于hive数仓这个概念的一些理解+查看hive底层引擎是否是tez
- kafka 脚本发送_Kafka笔记归纳(第五部分:一致性保证,消息重复消费场景及解决方式)...
- 小米MIX 4支持UWB技术:实现设备间无感定向传输和操控
- 蓝桥杯 ADV-188 算法提高 排列数
- laravel5.1 基于redis实现任务队列
- Docker的安装和操作(虚拟机+linux系统)
- 【小知识】二分类问题,应该选择sigmoid还是softmax?
- C# Access数据库使用
- 汇编(五):第一个汇编程序
- php页面中播放flv视频,页面播放flv格式视频[原创]
- 计算机单元测验2符号化,地理信息系统概论
- mysql全称量词_数据库整理(三) SQL基础
- Hairpin流量, 发卡流量怎么理解
- [PTA] 7-11 计算平均分
- Redis主从复制(薪火相传模式 演示示例)——图解版
- html四舍五入函数,Javascript四舍五入(Math.round()与Math.pow())