9. 数据集加载工具#
imblearn.datasets
包是对
sklearn.datasets
包的补充。该包提供了以下内容:(i) 一组用于进行系统基准测试的不平衡数据集,以及 (ii) 一个从原始平衡数据集创建不平衡数据集的实用工具。
9.1. 用于基准测试的不平衡数据集#
fetch_datasets
允许获取27个不平衡且二值化的数据集。以下数据集可用:
ID
名称
仓库与目标
比率
#S
#F
1
大肠杆菌
UCI, 目标: imU
8.6:1
336
7
2
光学数字
UCI, 目标: 8
9.1:1
5,620
64
3
卫星图像
UCI, 目标: 4
9.3:1
6,435
36
4
pen_digits
UCI, 目标: 5
9.4:1
10,992
16
5
鲍鱼
UCI, 目标: 7
9.7:1
4,177
10
6
病态甲状腺功能正常
UCI, 目标: 病态甲状腺功能减退
9.8:1
3,163
42
7
光谱仪
UCI, 目标: >=44
11:1
531
93
8
car_eval_34
UCI, 目标: 好, 非常好
12:1
1,728
21
9
isolet
UCI, 目标: A, B
12:1
7,797
617
10
美国犯罪
UCI, 目标: >0.65
12:1
1,994
100
11
酵母_ml8
LIBSVM, 目标: 8
13:1
2,417
103
12
场景
LIBSVM, 目标: >一个标签
13:1
2,407
294
13
libras_move
UCI, 目标: 1
14:1
360
90
14
甲状腺疾病
UCI, 目标: sick
15:1
3,772
52
15
coil_2000
KDD, CoIL, 目标: 少数
16:1
9,822
85
16
心律失常
UCI, 目标: 06
17:1
452
278
17
太阳耀斑_m0
UCI, 目标: M->0
19:1
1,389
32
18
油
UCI, 目标: 少数群体
22:1
937
49
19
car_eval_4
UCI, 目标: vgood
26:1
1,728
21
20
葡萄酒质量
UCI, wine, 目标: <=4
26:1
4,898
11
21
字母图片
UCI, 目标: Z
26:1
20,000
16
22
yeast_me2
UCI, 目标: ME2
28:1
1,484
8
23
网页
LIBSVM, w7a, 目标: 少数类
33:1
34,780
300
24
臭氧水平
UCI, 臭氧, 数据
34:1
2,536
72
25
乳腺X光检查
UCI, 目标: 少数群体
42:1
11,183
6
26
蛋白质同源
KDD CUP 2004, 少数
11:1
145,751
74
27
鲍鱼_19
UCI, 目标: 19
130:1
4,177
10
可以选择一个特定的数据集如下:
>>> from collections import Counter
>>> from imblearn.datasets import fetch_datasets
>>> ecoli = fetch_datasets()['ecoli']
>>> ecoli.data.shape
(336, 7)
>>> print(sorted(Counter(ecoli.target).items()))
[(-1, 301), (1, 35)]
9.2. 不平衡生成器#
make_imbalance
将原始数据集转换为不平衡数据集。此行为由参数 sampling_strategy
驱动,其行为与其他重采样算法类似。sampling_strategy
可以作为字典给出,其中键对应于类别,值是类别中的样本数量:
>>> from sklearn.datasets import load_iris
>>> from imblearn.datasets import make_imbalance
>>> iris = load_iris()
>>> sampling_strategy = {0: 20, 1: 30, 2: 40}
>>> X_imb, y_imb = make_imbalance(iris.data, iris.target,
... sampling_strategy=sampling_strategy)
>>> sorted(Counter(y_imb).items())
[(0, 20), (1, 30), (2, 40)]
请注意,如果字典中没有提到某个类,则该类的所有样本都会通过:
>>> sampling_strategy = {0: 10}
>>> X_imb, y_imb = make_imbalance(iris.data, iris.target,
... sampling_strategy=sampling_strategy)
>>> sorted(Counter(y_imb).items())
[(0, 10), (1, 50), (2, 50)]
可以定义一个函数并直接传递给
sampling_strategy
,而不是使用字典:
>>> def ratio_multiplier(y):
... multiplier = {0: 0.5, 1: 0.7, 2: 0.95}
... target_stats = Counter(y)
... for key, value in target_stats.items():
... target_stats[key] = int(value * multiplier[key])
... return target_stats
>>> X_imb, y_imb = make_imbalance(iris.data, iris.target,
... sampling_strategy=ratio_multiplier)
>>> sorted(Counter(y_imb).items())
[(0, 25), (1, 35), (2, 47)]
它也可以与pandas dataframe一起使用:
>>> from sklearn.datasets import fetch_openml
>>> df, y = fetch_openml(
... 'iris', version=1, return_X_y=True, as_frame=True)
>>> df_resampled, y_resampled = make_imbalance(
... df, y, sampling_strategy={'Iris-setosa': 10, 'Iris-versicolor': 20},
... random_state=42)
>>> df_resampled.head()
sepallength sepalwidth petallength petalwidth
13 4.3 3.0 1.1 0.1
39 5.1 3.4 1.5 0.2
30 4.8 3.1 1.6 0.2
45 4.8 3.0 1.4 0.3
17 5.1 3.5 1.4 0.3
>>> Counter(y_resampled)
Counter({'Iris-virginica': 50, 'Iris-versicolor': 20, 'Iris-setosa': 10})