【问题标题】:Over sample binary [duplicate]过度样本二进制文件[重复]
【发布时间】:2019-04-22 00:25:03
【问题描述】:

我有一些由类 (X) 和一些二进制 (Y) 组成的数据。我想通过对较小的班级进行过度抽样来平衡班级人数。例如,如果我开始:

Df_01 = pd.DataFrame({'X' : [1,1,1,1,1,1,1,2,2],
                      'Y1': [1,1,1,1,1,0,0,0,1],
                      'Y2': [0,0,0,0,0,1,0,0,0]})

那我想得到:

Df_02 = pd.DataFrame({'X' : [1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2],
                      'Y1': [1,1,1,1,1,0,0,0,1,0,1,0,1,0,1,0],
                      'Y2': [0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0]})

我已经尝试过:

# Sort the data by class
Ma_01 = Df_01.groupby('X')
Di_01 = {}
for name, group in Ma_01:
    Di_01[str(name)] = group

# Size of each class
Se_01 = Df_01.groupby('X').size()

# Size of the biggest class
In_Bi = max(Se_01)

# How much over sampling would equalise the class sizes?
Se_Ra =  In_Bi / Se_01
Di_Ra =  Se_Ra.to_dict()

但是当我尝试时:

# Copy each dataframe
Di_03 = {}
for x in Di_01:
    for y in range(int(Di_Ra[int(x)])):
        if not Di_03:
            Di_03[x] = Di_01[x]
        else:
            Di_03[x] = Di_03[x] .append(Di_01[x])

# Concatonate the dictionary to a single dataframe
df_03 = pd.concat(Di_03.values(), ignore_index=True)

我明白了

KeyError: '2'

【问题讨论】:

    标签: python binary discrete-mathematics


    【解决方案1】:

    感谢您找到重复的 Matthew Strawbridge! Ayhan 对我的数据的原始作品的回答:

    max_size = Df_01['X'].value_counts().max()
    
    lst = [Df_01]
    
    for class_index, group in Df_01.groupby('X'):
        lst.append(group.sample(max_size-len(group), replace=True))
    
    Df_03 = pd.concat(lst)
    

    【讨论】:

    • 正如 Ayhan 在原文中提到的,我应该“可能会添加一些噪音”。这是因为过采样数据可能非常重复,这会导致机器学习工具过度拟合过采样数据。我会使用 SMOTE 来添加噪音,但我认为这不适用于二进制。我会看看我是否可以在类中交换值。
    猜你喜欢
    • 2012-08-19
    • 2020-06-19
    • 2015-10-04
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多