make_imbalance#
- imblearn.datasets.make_imbalance(X, y, *, sampling_strategy=None, random_state=None, verbose=False, **kwargs)[source]#
将数据集转换为具有特定采样策略的不平衡数据集。
一个简单的玩具数据集,用于可视化聚类和分类算法。
更多内容请参阅用户指南。
- Parameters:
- X{array-like, dataframe} of shape (n_samples, n_features)
包含需要不平衡处理的数据的矩阵。
- yarray-like of shape (n_samples,)
X中每个样本对应的标签。
- sampling_strategydict or callable,
用于重新采样数据集的比率。
当
dict
时,键对应于目标类别。值对应于每个目标类别所需的样本数量。当可调用时,函数接受
y
并返回一个dict
。键对应于目标类别。值对应于每个类别所需的样本数量。
- random_stateint, RandomState instance or None, default=None
如果是整数,random_state 是随机数生成器使用的种子; 如果是 RandomState 实例,random_state 是随机数生成器; 如果是 None,随机数生成器是 np.random 使用的 RandomState 实例。
- verbosebool, default=False
显示有关采样的信息。
- **kwargsdict
传递给
sampling_strategy
的额外关键字参数的字典。
- Returns:
- X_resampled{ndarray, dataframe} of shape (n_samples_new, n_features)
包含不平衡数据的数组。
- y_resampledndarray of shape (n_samples_new)
X_resampled
对应的标签。
注释
请参阅 多类分类与欠采样, 创建不平衡数据集, 以及 如何在imbalanced-learn中使用sampling_strategy.
示例
>>> from collections import Counter >>> from sklearn.datasets import load_iris >>> from imblearn.datasets import make_imbalance
>>> data = load_iris() >>> X, y = data.data, data.target >>> print(f'Distribution before imbalancing: {Counter(y)}') Distribution before imbalancing: Counter({0: 50, 1: 50, 2: 50}) >>> X_res, y_res = make_imbalance(X, y, ... sampling_strategy={0: 10, 1: 20, 2: 30}, ... random_state=42) >>> print(f'Distribution after imbalancing: {Counter(y_res)}') Distribution after imbalancing: Counter({2: 30, 1: 20, 0: 10})