栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python不平衡数据处理库imblearn安装和使用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python不平衡数据处理库imblearn安装和使用

一般直接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})

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/858616.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号