0

什么不起作用:

def ATVScore(x,p,d):
    if x <= d[p][0.25]:
        return 4
    elif x <= d[p][0.50]:
        return 3
    elif x <= d[p][0.75]: 
        return 2
    else:
        return 1



df_segmented['atv_quartile'] = df_segmented['Average_Transaction_Value'].apply(ATVScore, args = ('Average_Transaction_Value', quantiles,))

什么工作:

分位数定义:

quantiles = df_final_table.quantile(q=[0.25,0.5,0.75])

quantiles = quantiles.to_dict()

def RScore(x,p,d):
    if x <= d[p][0.25]:
        return 1
    elif x <= d[p][0.50]:
        return 2
    elif x <= d[p][0.75]: 
        return 3
    else:
        return 4
    
def FMScore(x,p,d):
    if x <= d[p][0.25]:
        return 4
    elif x <= d[p][0.50]:
        return 3
    elif x <= d[p][0.75]: 
        return 2
    else:
        return 1

df_segmented['recency_quartile'] = df_segmented['recency'].apply(RScore, args = ('recency', quantiles,))
df_segmented['frequency_quartile'] = df_segmented['frequency'].apply(FMScore, args = ('frequency', quantiles,))
df_segmented['monetary_quartile'] = df_segmented['monetary'].apply(FMScore, args = ('monetary', quantiles,))

数据: 数据

错误:

错误

4

1 回答 1

0

您的代码的问题很可能出在quantiles的内容中。

创建此变量后,打印其内容。可能它包含以下内容:

{'recency':   {0.25: 350.0, 0.5: 500.0, 0.75: 600.0},
 'frequency': {0.25: 370.0, 0.5: 520.0, 0.75: 620.0},
 'monetary':  {0.25: 390.0, 0.5: 540.0, 0.75: 640.0}}

(作为每个嵌入式字典的值,我传递了任意值)。

现在,当您应用ATVScore并将剩余参数作为 'Average_Transaction_Value'作为p并将分位数作为d时,此函数:

  • 尝试执行if x <= d[p][0.25]:(几乎在 StackTrace 的末尾),
  • 执行d[p]它会尝试在quantiles中找到“Average_Transaction_Value” 键,
  • 由于分位数不包含此键,因此会引发KeyError异常。

'Average_Transaction_Value'键添加到quantiles,其值类似于{0.25: 410.0, 0.5: 560.0, 0.75: 660.0}} 并且您的代码应该可以正常运行。

于 2020-09-08T10:34:11.587 回答