文章目录

  • 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-不平衡数据之采样相关推荐

  1. 机器学习 处理不平衡数据_在机器学习中处理不平衡数据

    机器学习 处理不平衡数据 As an ML engineer or data scientist, sometimes you inevitably find yourself in a situat ...

  2. 数据处理之不平衡数据过采样与下采样

    https://blog.csdn.net/mengjiexu_cn/article/details/97008269

  3. 机器学习 对不平衡数据的四种处理方法

    https://blog.csdn.net/qq_40875849/article/details/85013973

  4. 不平衡数据采样_过度采样不平衡数据的5种打击技术

    不平衡数据采样 Imbalance data is a case where the classification dataset class has a skewed proportion. For ...

  5. 机器学习中的数据不平衡问题----通过随机采样比例大的类别使得训练集中大类的个数与小类相当,或者模型中加入惩罚项...

    机器学习中的数据不平衡问题 摘自:http://wap.sciencenet.cn/blogview.aspx?id=377102 最近碰到一个问题,其中的阳性数据比阴性数据少很多,这样的数据集在进行 ...

  6. 【机器学习基础】如何在Python中处理不平衡数据

    特征锦囊:如何在Python中处理不平衡数据 ???? Index 1.到底什么是不平衡数据 2.处理不平衡数据的理论方法 3.Python里有什么包可以处理不平衡样本 4.Python中具体如何处理 ...

  7. lightgbm 数据不平衡_不平衡数据下的机器学习(下)

    本文从不平衡学习的基础概念和问题定义出发,介绍了几类常见的不平衡学习算法和部分研究成果.总体来说,不平衡学习是一个很广阔的研究领域,但受笔者能力和篇幅的限制,本文仅对其中部分内容做了简单概述,有兴趣深 ...

  8. 如何解决机器学习中的数据不平衡问题?

    在机器学习任务中,我们经常会遇到这种困扰:数据不平衡问题. 数据不平衡问题主要存在于有监督机器学习任务中.当遇到不平衡数据时,以总体分类准确率为学习目标的传统分类算法会过多地关注多数类,从而使得少数类 ...

  9. linux中python如何调用matlab的数据_特征锦囊:如何在Python中处理不平衡数据

    今日锦囊 特征锦囊:如何在Python中处理不平衡数据 ? Index 1.到底什么是不平衡数据 2.处理不平衡数据的理论方法 3.Python里有什么包可以处理不平衡样本 4.Python中具体如何 ...

  10. ​【机器学习】交通数据的时间序列分析和预测实战

    今天又给大家带来一篇实战案例,本案例旨在运用之前学习的时间序列分析和预测基础理论知识,用一个实际案例数据演示这些方法是如何被应用的. 本文较长,建议收藏!由于篇幅限制,文内精简了部分代码,但不影响阅读 ...

最新文章

  1. Python破解验证码技术,识别率高达百分之八十
  2. 原生JavaScript实战之搜索框筛选功能
  3. leetcode 446. Arithmetic Slices II - Subsequence | 446. 等差数列划分 II - 子序列(动态规划)
  4. Protobuf学习笔记
  5. SCCM 2012 R2---安装客户端代理软件
  6. 消费者服务消费延时分析
  7. 大数据如何影响百姓生活
  8. JavaScript-No.01 JavaScript实现封装、继承、多态
  9. 如何实现台达触摸屏与台达PLC之间的远距离无线数据交换?
  10. Pymol入门教程--基础
  11. Diffusion Models扩散模型与深度学习(数学原理和代码解读)
  12. python 如何通过海表面高度数据计算海表地转流速、并绘制流线图
  13. 为什么网站总显示服务器不能创建对象,automation服务器不能创建对象”的问题的解决方案总结大全...
  14. OpenCV 自带示例sample中的双目校正stereo_calib.cpp 安装与解读
  15. undertale人物_传说之下全人物介绍 人物评价解析
  16. Visual Studio快捷键大全
  17. 课代表:ChatGPT及大模型专题研讨会
  18. 致敬!向中外9名杰出女数学家
  19. DirectX中的粒子系统
  20. web界面配置视频 华为ac_华为AC控制器管理AP配置

热门文章

  1. C# 如何设置 richTextBoxr的边距
  2. 使用opensl 的BufferQueueAudioPlayer对wav文件的播放
  3. 【matlab】解决每次打开.m文件都会弹出新窗口
  4. java day38【Servlet 、HTTP协议 、Request】
  5. Luogu2543[AHOI2004]奇怪的字符串 (动态规划 LCS)
  6. PS把一张白纸里的黑色图形抠出来
  7. 【XLL 框架库函数】 TempInt/TempInt12
  8. selenium--python如何定位一组元素并返回文本值
  9. poj 1276 Cash Machine 背包问题
  10. Mysql分页之limit用法与limit优化