【问题标题】:Sampling based on dependent data基于依赖数据的抽样
【发布时间】:2020-01-23 04:18:24
【问题描述】:

我的数据框包含 6 列的 10M 条记录。类(is_active)非常不平衡(1-96%,0-4%)。

数据框看起来像 -

id      age    gender    salary   bonus  is_active
1        27     M         76543    762     0
2        36     F         87352    1050    1
3        29     M         76598    7364    1
4        61     M         74632    3475    0

我想要一个数据框,其中我的因变量 (is_active) 比率应为 1-75%,0-25%。

在python中怎么做?

【问题讨论】:

  • 根据 is_active 将你的 df 一分为二。然后使用 0.75 和 0.25 的样本

标签: python-3.x pandas scikit-learn


【解决方案1】:

一种方法是尝试这个,

df_1 = df[df['is_active']==1]
df_2 = df[df['is_active']==0]
df_1= df_1.sample(n = len(df_2)*3)
df=pd.concat([df_1, df_2], ignore_index=True)
  1. 根据 is_active 将数据框分成两部分。
  2. 取负样本中的所有行。
  3. 根据 df_1 的长度对 df_2 进行采样
  4. 连接两个数据帧。

最后一次测试:

print (df['is_active'].value_counts(normalize=True))

【讨论】:

  • @jezrael - 你能解释一下 df_1= df_1.sample(n = len(df_2)*3)...因为如果我想做 60:40 那我应该怎么做?有吗?有什么公式吗?
  • @JohnDavis - 公式是 1/.25 它是 4,所以对你来说是 int(1 / 0.4)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
相关资源
最近更新 更多