不均衡数据的处理方法

大部分的真实数据都是不均衡的,如果数据量太悬殊,很影响后续模型的训练

我的数据,真的是很废了

Counter({1: 20863, 2: 19187, 9: 8654, 5: 2543, 4: 1060, 7: 488, 13: 466, 6: 200, 3: 21, 11: 16, 10: 6, 14: 4, 12: 3})

这几天也看了很多对数据进行处理的方法,除非迫不得己,不会草率的只用欠采样或者过采样,尤其是我这种数据,太伤了

1、首先查看一下数据分布

from collections import Counter

print(Counter(label))

2、ADASYN

from imblearn.over_sampling import ADASYN 
adasyn = ADASYN(n_neighbors=3, random_state=42)
X_res, y_res = adasyn.fit_resample(mag, label)
print(Counter(y_res))

不修改n_neighbors的话,好像默认值是5还是6来着

3、SMOTEENN

from imblearn.combine import SMOTEENN
sm=SMOTEENN()
X_res, y_res = sm.fit_sample(mag, label)
print(Counter(y_res))

3、BorderlineSMOTE

from imblearn.over_sampling import BorderlineSMOTE
bsmote = BorderlineSMOTE(k_neighbors=3, random_state=42)
X_res, y_res = bsmote.fit_resample(mag, label)
print(Counter(y_res))

4、SMOTETomek

from imblearn.combine import SMOTETomek
smote_tomek = SMOTETomek(random_state=0)
X_res, y_res = smote_tomek.fit_resample(mag,label)
print(Counter(y_res))

5、SMOTE

from imblearn.over_sampling import SMOTE

smo = SMOTE(random_state=42,k_neighbors=3)
X_smo, y_smo = smo.fit_resample(mag,label)

sampling_strategy 可以设置不同类型的具体数目,例如label =14,生成5个数据

from imblearn.over_sampling import SMOTE
smo = SMOTE(sampling_strategy={14: 5 },random_state=42,k_neighbors=3)
X_smo, y_smo = smo.fit_resample(mag,label)
print(Counter(y_smo))

你可能感兴趣的:(python,开发语言)