[1] He H , Yang B , Garcia E A , et al. ADASYN: Adaptive synthetic sampling approach for imbalanced learning[C]// Neural Networks, 2008. IJCNN 2008. (IEEE World Congress on Computational Intelligence). IEEE International Joint Conference on. IEEE, 2008.
ADASYN : Adaptive Synthetic Sampling Approach for Imbalanced Learning。
ADASYN方法通过生成合成样本以两种方式改进了对数据分布的学习:
- 减少类不平衡带来的偏差。
- 自适应地将分类决策边界向困难的实例移动。
算法描述
输入: 为训练集,包含
个样本{
,
} ,
, 其中
是n维特征空间X中的一个实例,
∈
{-1,+1} 与
相关的类别标签,将
和
分别定义为少数类样本的数量和多数类样本的数量。因此
≤
,
+
=
。
算法步骤
- 计算类不平衡度。
∈ (0, 1]
2. 如果 (
是类不平衡比率的预设阈值)
步骤1.计算少数类需要合成的样本的数量:
其中 β∈[0,1]是用于在生成合成样本后制定所需的平衡级别的参数。β = 1表示操作之后形成一个完全平衡的数据集。
步骤2. 对于少数类中每个样本 , 根据n维空间中的欧几里得距离找到k邻近,并计算
:
=
,
= 1,...,
其中 为
的k邻近中多数类的占比,
∈[0, 1]。
步骤3. 对 进行标准化处理,可得每个样本
在正在中的占比。
步骤4. 计算少数类中每个样本 需要生成的合成样本的个数。
步骤5. 根据SMOTE算法生成样本生成合成样本。
其中 是n维空间中的差分向量,
是一个随机数:
∈[0,1]。
Python实现
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.over_sampling import ADASYN
plt.figure(figsize=(20,20)) # 创建一个10 * 10 英寸的图像
plt.subplots_adjust(bottom=.05,top=.9,left=.05,right=.8)
plt.subplot(421)
X, y = make_classification(n_classes=2, class_sep=2,
weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0,
n_features=20, n_clusters_per_class=1, n_samples=100,
random_state=10)
plt.scatter(X[:,0],X[:,1],marker='o',c=y)
"""
n_classes int 可选(default = 0) 类别或者标签数量
class_sep float 可选(default = 1.0) 乘以超立方体大小的因子。 较大的值分散了簇/类,并使分类任务更容易。默认为1
weights floats列表 or None 可选(default = None) 每个类的权重,用于分配样本点
n_informative int 可选 (default=2) 多信息特征数量
n_redundant int 可选 (default=2) 冗余信息,informative特征的随机线性组合
n_features int 可选 (default=20) 特征个数= n_informative() + n_redundant + n_repeated
n_clusters_per_class int 可选(default=2) 每个class中cluster数量
n_samples int 可选(default = 100) 样本数量
random_state 随机
"""
print('Original dataset shape %s' % Counter(y))
plt.subplot(422)
ada = ADASYN(random_state=42)
X_res, y_res = ada.fit_resample(X, y)
plt.scatter(X_res[:,0],X_res[:,1],marker='o',c=y_res)
plt.show()
print('Resampled dataset shape %s' % Counter(y_res))
执行结果