一般直接pip安装即可,安装不成功可能是因为 没有安装imblearn需要的Python模块,对应安装即可
pip install -U imbalanced-learn
imblearn中的过采样方法: Over-sampling methods — Version 0.9.0 (imbalanced-learn.org)
过采样示例:
>>> from collections import Counter >>> from sklearn.datasets import make_classification >>> from imblearn.over_sampling import SMOTE # 加载数据集 >>> 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=1000, random_state=10) # 输出原数据集样本各类别数量 >>> print('Original dataset shape %s' % Counter(y)) 输出:Original dataset shape Counter({1: 900, 0: 100})
#调用SMOTE类中的fit_resample方法重新采样数据集 >>> sm = SMOTE(random_state=42) >>> X_res, y_res = sm.fit_resample(X, y) # 数据X的维度只能小于等于两维 # 输出过采样后数据集样本各类别数量 >>> print('Resampled dataset shape %s' % Counter(y_res)) 输出:Resampled dataset shape Counter({0: 900, 1: 900}) # 过采样后各类别数量
imblearn中的下采样方法:Under-sampling methods — Version 0.9.0 (imbalanced-learn.org)
ClusterCentroids下采样示例
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.under_sampling import ClusterCentroids
#加载数据集
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=1000, random_state=10)
print('Original dataset shape %s' % Counter(y))
#采样ClusterCentroids下采样
cc = ClusterCentroids(random_state=42)
X_res, y_res = cc.fit_resample(X, y)
print('Resampled dataset shape %s' % Counter(y_res))
输出结果:
Original dataset shape Counter({1: 900, 0: 100})
Resampled dataset shape Counter({0: 100, 1: 100})