python imblearn toolbox 解决数据不平衡问题(三)——under-sampling下采样

下采样即对多数类样本(正例)进行处理,使其样本数目降低。在imblearn toolbox中主要有两种方式:Prototype generation(原型生成)Prototype selection (原型选择) 。前者生成不同于原正例样本的新的正例样本,后者对原正例样本进行筛选。

一、Prototype generation

算法即对多数类样本生成新的样本去替代原样本,使得样本数目减少,且新样本是生成的而不是选取的。
该类上采样主要的类为ClusterCentroids,即用K-means的中心代替原样本。基本用法python代码如下:

from imblearn.under_sampling import ClusterCentroids
cc = ClusterCentroids(random_state=0)
X_resampled, y_resampled = cc.fit_resample(X,y)

二、 Prototype selection

算法即从原数据集(original set)中选取一些样本,根据选取思路主要分为以下两类:

  • the controlled under-sampling techniques
  • the cleaning under-sampling techniques
    前者会严格控制下采样后正例的样本量,而后者采取启发式算法删除正例样本,不严格控制下采样后正例的样本量。

2.1 the controlled under-sampling techniques

1. RandomUnderSampler 直接随机选取删除法

from imblearn.under_sampling import RandomUnderSampler
rus = RandomUnderSampler(random_state=0)
X_resampled, y_resampled = rus.fit_resample(X, y)
print(sorted(Counter(y_resampled).items())

注意1:当设置replacement=True时,采用bootstrap
注意2: 允许非结构化数据,例如数据中含字符串

2. NearMiss 基于NN启发式算法

from imblearn.under_sampling import NearMiss
nm1 = NearMiss(version=1)
X_resampled_num1, y_resampled = nm1.fit_resample(X, y)

version=1:选取正例样本中与N个最近邻负样本平均距离最短的样本
version=2:选取正例样本中与N个最远邻负样本平均距离最短的样
version=3:2-steps 算法,首先对于每个负类样本,他们的M近邻正样本保留。然后,选取到N近邻负平均聚类最大。

2.2 Cleaning under-sampling techniques

1. Tomek’s link (TomeLinks)
定义:两个来自不同类别的两个样本 x x , y y 的一个Tomek’s link定义为不存在样本 z z ,使得 d ( x , z ) < d ( x , y ) d(x,z)<d(x,y) or d ( y , z ) < d ( x , y ) d(y,z)<d(x,y) ,换句话说,Tomek’s link存在,如果两个样本是各自的最近邻(怀疑原doc写错了)。

参数sampling_strategy控制the link的哪个样本会被移除,即:sampling_strategy='auto’会移除正例样本;sampling_strategy='all’会移除所有样本。

2. Edited data set using neareast neighbours(EditeNearestNeighbours)
运用一个NN算法并通过移除与他们的近邻not agree "enough"的方式来’edit’数据集。

from imblearn.under_sampling import EditedNearestNeighbours
enn = EditedNearestNeighbours()
X_resampled, y_resampled = enn.fit_resample(X, y)

目前有两个准则:: kind_sel='mode’和 kind_sel=‘all’
#这个算法只执行一次

变体:RepeatedEditedNearestNeighbours
算法比起EditeNearestNeighbours的改进为:自动重复ENN算法多次。

from imblearn.under_sampling import RepeatedEditedNearestNeighbours
renn = RepeatedEditedNearestNeighbours()

变体:ALLKNN
算法比起RepeatedEditedNearestNeighbours,改进为:在每次迭代会KNN中邻居的数目都会增多。

from imblearn.under_sampling import RepeatedEditedNearestNeighbours
renn = RepeatedEditedNearestNeighbours()

3. Condensed(浓缩的) nearest neighbors and derived algorithms(CondensedNearestNeighbour)
使用一个1近邻原则迭代地决定是否移除一个样本,缺点是对噪声太敏感。

from imblearn.under_sampling import CondensedNearestNeighbour
cnn = CondensedNearestNeighbour(random_state=0)

Step1:把所有负类样本放到集合C
Step2:从要进行下采样的类中选取一个元素加入C,该类其它集合加入S
Step3:遍历S,对每个元素进行采样,采用1-NN算法进行分类,将分类错误的加入C
Step4:迭代S直到没有元素要加入C

变体:OneSideSelection
与CondensedNearestNeighbour相比,使用TomeLinks来remove noisy samples.

from imblearn.under_sampling import OneSidedSelection
oss = OneSidedSelection(random_state=0)

变体:NeighbourhoodCleaningRule
use the nnion of samples to be rejected between the ENN and the output a 3-NN分类.

from imblearn.under_sampling import NeighbourhoodCleaningRule
ncr = NeighbourhoodCleaningRule()

4. Instance hardness threshold(InstanceHardnessThreshold)
在数据集中训练一个分类器,并且样本带有低概率会被移除。

from sklearn.linear_model import LogisticRegression
from imblearn.under_sampling import InstanceHardnessThreshold
iht = InstanceHardnessThreshold(random_state=0,
                                 estimator=LogisticRegression(
                                     solver='lbfgs', multi_class='auto')

estimator可以接受任何有一个method predict_proba的scikit-learn分类

猜你喜欢

转载自blog.csdn.net/mathlxj/article/details/89645321