3

我有一个包含多标签数据的数据集。共有 20 个标签(从 0 到 20),它们之间的分布不平衡。以下是数据概览:

|id   |label|value       |
|-----|-----|------------|
|95534|0    |65.250002088|
|95535|18   |            |
|95536|0    |            |
|95536|0    |100         |
|95536|0    |            |
|95536|0    |53.68547236 |
|95536|0    |            |
|95537|1    |            |
|95538|0    |            |
|95538|0    |            |
|95538|0    |            |
|95538|0    |656.06155202|
|95538|0    |            |
|95539|2    |            |
|5935 |0    |            |
|5935 |0    |150         |
|5935 |0    |50          |
|5935 |0    |24.610985335|
|5935 |0    |            |
|5935 |0    |223.81789584|
|5935 |0    |148.1805218 |
|5935 |0    |110.9712538 |
|34147|19   |73.62651909 |
|34147|19   |            |
|34147|19   |53.35958016 |
|34147|19   |            |
|34147|19   |            |
|34147|19   |            |
|34147|19   |393.54029411|

我希望对数据进行过度采样并在标签之间取得平衡。我遇到了一些方法,例如SMOTESMOTENC但它们都需要将数据拆分为训练集和测试集,并且它们不适用于稀疏数据。有什么方法可以在拆分前的预处理步骤中对整个数据执行此操作?

4

2 回答 2

2

实际上从理论上讲,您不需要对测试集进行上采样。

在类不平衡设置中,人为地平衡测试/验证集没有任何意义:这些集必须保持真实,即您想在现实世界设置中测试分类器性能,例如,负类将包括 99%样本中,以查看您的模型在预测 1% 的感兴趣的正类类别方面的表现如何,而不会出现太多误报。人为地夸大少数类或减少多数类将导致不切实际的性能指标,与您试图解决的现实世界问题没有真正的关系。

重新平衡仅在训练集中才有意义,以防止分类器简单天真地将所有实例分类为负值,以达到 99% 的感知准确度。

因此,您可以放心,在您描述的设置中,重新平衡仅对训练集/折叠采取行动。

于 2020-09-11T17:47:05.237 回答
2

要对行进行采样,以便label以相等的概率对每个行进行采样:

  • 绘制一行给定标签的概率应该是1/n_labels
  • 为给定标签绘制给定行的概率l应该是1/n_rows该标签中的 n_rows

那么每一行的概率是p_row = 1/(n_labels*n_rows)。您可以使用 groupby 生成这些值并将它们传递给 df.sample,如下所示:

import numpy as np
import pandas as pd

df_dict = {'id': {0: 95535, 1: 95536, 2: 95536, 3: 95536, 4: 95536, 5: 95536, 6: 95537, 7: 95538, 8: 95538, 9: 95538, 10: 95538, 11: 95538, 12: 95539, 13: 5935, 14: 5935, 15: 5935, 16: 5935, 17: 5935, 18: 5935, 19: 5935, 20: 5935, 21: 34147, 22: 34147, 23: 34147, 24: 34147, 25: 34147, 26: 34147, 27: 34147}, 'label': {0: 18, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 1, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 2, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 19, 22: 19, 23: 19, 24: 19, 25: 19, 26: 19, 27: 19}, 'value': {0: '            ', 1: '            ', 2: '100         ', 3: '            ', 4: '53.68547236 ', 5: '            ', 6: '            ', 7: '            ', 8: '            ', 9: '            ', 10: '656.06155202', 11: '            ', 12: '            ', 13: '            ', 14: '150         ', 15: '50          ', 16: '24.610985335', 17: '            ', 18: '223.81789584', 19: '148.1805218 ', 20: '110.9712538 ', 21: '73.62651909 ', 22: '            ', 23: '53.35958016 ', 24: '            ', 25: '            ', 26: '            ', 27: '393.54029411'}}    

df = pd.DataFrame.from_dict(d)

# create column that includes counts by label
n_labels = df.label.nunique()
n_rows = df.groupby("label").id.transform("count")
weights = 1/(n_rows*n_labels)

# sanity check probabilities:
bool(np.sum(weights) == 1)    

df_samples = df.sample(n=40000, weights=weights, replace=True, random_state=19)

验证标签绘制是否大致一致:

print(df_samples.label.value_counts()/len(df_samples))

# sampling frequency by group:
# 0     0.203325
# 2     0.201075
# 18    0.200925
# 19    0.198850
# 1     0.195825
于 2020-09-11T18:05:40.777 回答