Python - Pandas,Resample数据集,具有平衡类

Shl*_*rtz 7 python numpy machine-learning dataset pandas

使用以下数据框,只有2个可能的标签:

   name  f1  f2  label
0     A   8   9      1
1     A   5   3      1
2     B   8   9      0
3     C   9   2      0
4     C   8   1      0
5     C   9   1      0
6     D   2   1      0
7     D   9   7      0
8     D   3   1      0
9     E   5   1      1
10    E   3   6      1
11    E   7   1      1
Run Code Online (Sandbox Code Playgroud)

我编写了一个代码,通过'name'列对数据进行分组,并将结果转换为numpy数组,因此每一行都是特定组的所有样本的集合,而标签是另一个numpy数组:

数据:

[[8 9] [5 3] [0 0]] # A lable = 1
[[8 9] [0 0] [0 0]] # B lable = 0
[[9 2] [8 1] [9 1]] # C lable = 0
[[2 1] [9 7] [3 1]] # D lable = 0
[[5 1] [3 6] [7 1]] # E lable = 1
Run Code Online (Sandbox Code Playgroud)

标贴:

[[1]
 [0]
 [0]
 [0]
 [1]]
Run Code Online (Sandbox Code Playgroud)

码:

import pandas as pd
import numpy as np


def prepare_data(group_name):
    df = pd.read_csv("../data/tmp.csv")


    group_index = df.groupby(group_name).cumcount()
    data = (df.set_index([group_name, group_index])
            .unstack(fill_value=0).stack())



    target = np.array(data['label'].groupby(level=0).apply(lambda x: [x.values[0]]).tolist())
    data = data.loc[:, data.columns != 'label']
    data = np.array(data.groupby(level=0).apply(lambda x: x.values.tolist()).tolist())
    print(data)
    print(target)


prepare_data('name')
Run Code Online (Sandbox Code Playgroud)

我想从过度代表的类中重新采样和删除实例.

[[8 9] [5 3] [0 0]] # A lable = 1
[[8 9] [0 0] [0 0]] # B lable = 0
[[9 2] [8 1] [9 1]] # C lable = 0
# group D was deleted randomly from the '0' labels 
[[5 1] [3 6] [7 1]] # E lable = 1
Run Code Online (Sandbox Code Playgroud)

这将是一个可接受的解决方案,因为删除D(标记为'0')将产生2*标签'1'和2*标签'0'的平衡数据集.

Joe*_*008 7

一个非常简单的方法。取自 sklearn 文档和 Kaggle。

from sklearn.utils import resample

df_majority = df[df.label==0]
df_minority = df[df.label==1]

# Upsample minority class
df_minority_upsampled = resample(df_minority, 
                                 replace=True,     # sample with replacement
                                 n_samples=20,    # to match majority class
                                 random_state=42) # reproducible results

# Combine majority class with upsampled minority class
df_upsampled = pd.concat([df_majority, df_minority_upsampled])

# Display new class counts
df_upsampled.label.value_counts()
Run Code Online (Sandbox Code Playgroud)


a_g*_*est 4

如果每个都name被精确地标记为 1 label(例如全部A都是1),您可以使用以下内容:

  1. name将s分组label并检查哪个标签有多余的内容(就唯一名称而言)。
  2. 从过多的标签类中随机删除名称,以解决多余的问题。
  3. 选择数据框中不包含已删除名称的部分。

这是代码:

labels = df.groupby('label').name.unique()
# Sort the over-represented class to the head.
labels = labels[labels.apply(len).sort_values(ascending=False).index]
excess = len(labels.iloc[0]) - len(labels.iloc[1])
remove = np.random.choice(labels.iloc[0], excess, replace=False)
df2 = df[~df.name.isin(remove)]
Run Code Online (Sandbox Code Playgroud)