【问题标题】:Smoothing by bin boundaries using pandas/numpy使用 pandas/numpy 按 bin 边界平滑
【发布时间】:2021-01-16 09:36:51
【问题描述】:

我已经使用 pandas.cut 函数形成了垃圾箱。现在,为了按 bin 边界执行平滑,我使用 groupby 函数计算每个 bin 的最小值和最大值
最小值

    date    births  with noise
bin         
A   1959-01-31  23  19.921049
B   1959-01-02  27  25.921175
C   1959-01-01  30  32.064698
D   1959-01-08  35  38.507170
E   1959-01-05  41  45.022163
F   1959-01-13  47  51.821755
G   1959-03-27  56  59.416700
H   1959-09-23  73  70.140119

最大值-

    date    births  with noise
bin         
A   1959-07-12  30  25.161292
B   1959-12-11  35  31.738422
C   1959-12-27  42  38.447807
D   1959-12-20  48  44.919703
E   1959-12-31  56  51.274550
F   1959-12-30  59  57.515927
G   1959-11-05  68  63.970382
H   1959-09-23  73  70.140119

现在我想替换原始数据框中的值。如果该值小于(其 bin 的)平均值,则将其替换为(该 bin 的)最小值,如果大于平均值,则将其替换为最大值。
我的数据框看起来像这样-

    date    births  with noise  bin smooth_val_mean
0   1959-01-01  35  36.964692   C   35.461173
1   1959-01-02  32  29.861393   B   29.592061
2   1959-01-03  30  27.268515   B   29.592061
3   1959-01-04  31  31.513148   B   29.592061
4   1959-01-05  44  46.194690   E   47.850101

我应该如何使用 pandas/numpy 做到这一点?

【问题讨论】:

  • 您显示了每个箱的 max/min 值,但我没有看到平均值。另外,您要同时替换 birthswith noise 列吗?
  • 与最大值/最小值类似,我也预先计算了平均值。而且,只是“有噪音”列。 (或者两者兼而有之,我只是在寻找程序)。 @QuangHoang

标签: python pandas numpy data-mining smoothing


【解决方案1】:

让我们试试这个功能:

def thresh(col):
    means = df['bin'].replace(df_mean[col])
    mins = df['bin'].replace(df_min[col])
    maxs = df['bin'].replace(df_max[col])
    
    signs = np.signs(df[col] - means)
    
    df[f'{col}_smooth'] = np.select((signs==1, signs==-1), (maxs, mins), means)

for col in ['with noise']:
    thresh(col)

【讨论】:

  • 声明 signs = df[col] - means 给出错误-“具有 dtype 类别的对象无法执行 numpy op 减法”
  • @SatashreeRoy 尝试使用replace 而不是map。查看更新。
  • 谢谢,现在可以使用了。但是,我对signs 感到困惑。如果它不是 1 或 -1 怎么办? (例如,我观察到很少有数据点给出符号= ~2 )。我仍然希望该值被任何边界值平滑,而不是平均值。 @QuangHoang
  • @SatashreeRoy 抱歉,它应该被 np.signs 包裹,当值分别为正数、0、负数时返回 1,0,-1。查看更新。
猜你喜欢
  • 2016-08-21
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2021-05-10
  • 1970-01-01
  • 2017-09-12
相关资源
最近更新 更多