我需要对数据集中的所有分类列进行均值(目标)编码。为了简化这个问题,假设我的数据集中有 2 列,第一列是标签列,第二列是分类列。
例如
label | cate1
0 | abc
1 | abc
0 | def
0 | def
1 | ghi
Run Code Online (Sandbox Code Playgroud)
因此根据均值编码策略:https://towardsdatascience.com/why-you-should-try-mean-encoding-17057262cd0
输出应该是这样的
label | cate1
0 | 0.5
1 | 0.5
0 | 0.0
0 | 0.0
1 | 1.0
Run Code Online (Sandbox Code Playgroud)
我尝试用 Koalas 来解决这个问题,但失败了。这是我尝试过的:
for col_name in convert_cols:
cat_mean_dict = dict()
# get category name <-> count dictionary
cur_col_cate_count_ = ks_df[col_name].value_counts().to_dict()
print(cur_col_cate_count_)
# calculate all different categories positive result count and mean value
start_time = time.time()
for key …Run Code Online (Sandbox Code Playgroud)