H2O目标平均编码器“帧以相同顺序发送”错误

时间:2019-03-22 19:34:12

标签: python-3.x pyspark h2o

我正在跟踪H2O示例,以在Sparking Water(火花水2.4.2和H2O 3.22.04)中运行目标均值编码。在以下所有段落中运行良好

from h2o.targetencoder import TargetEncoder

# change label to factor
input_df_h2o['label'] = input_df_h2o['label'].asfactor()

# add fold column for Target Encoding
input_df_h2o["cv_fold_te"] = input_df_h2o.kfold_column(n_folds = 5, seed = 54321)

# find all categorical features
cat_features = [k for (k,v) in input_df_h2o.types.items() if v in ('string')]
# convert string to factor
for i in cat_features:
    input_df_h2o[i] = input_df_h2o[i].asfactor()

# target mean encode
targetEncoder = TargetEncoder(x= cat_features, y = y, fold_column = "cv_fold_te", blending_avg=True)
targetEncoder.fit(input_df_h2o)

但是当我开始使用用于调整目标编码器的数据集来运行转换代码时(请参见下面的代码):

ext_input_df_h2o = targetEncoder.transform(frame=input_df_h2o,
                                    holdout_type="kfold", # mean is calculating on out-of-fold data only; loo means leave one out
                                    is_train_or_valid=True,
                                    noise = 0, # determines if random noise should be added to the target average
                                    seed=54321)

我将遇到错误,如

Traceback (most recent call last):
  File "/tmp/zeppelin_pyspark-6773422589366407956.py", line 331, in <module>
    exec(code)
  File "<stdin>", line 5, in <module>
  File "/usr/lib/envs/env-1101-ver-1619-a-4.2.9-py-3.5.3/lib/python3.5/site-packages/h2o/targetencoder.py", line 97, in transform
    assert self._encodingMap.map_keys['string'] == self._teColumns
AssertionError

我在其源代码http://docs.h2o.ai/h2o/latest-stable/h2o-py/docs/_modules/h2o/targetencoder.html中找到了该代码 enter image description here 但是如何解决这个问题?它与用于运行 fit 的表相同。

2 个答案:

答案 0 :(得分:2)

问题是因为您尝试对多个分类特征进行编码。我认为这是H2O的错误,但是您可以解决将转换器置于对所有分类名称进行迭代的for循环中的问题。

import numpy as np
import pandas as pd
import h2o
from h2o.targetencoder import TargetEncoder
h2o.init()

df = pd.DataFrame({
    'x_0': ['a'] * 5 + ['b'] * 5,
    'x_1': ['c'] * 9 + ['d'] * 1,
    'x_2': ['a'] * 3 + ['b'] * 7,
    'y_0': [1, 1, 1, 1, 0, 1, 0, 0, 0, 0]
})

hf = h2o.H2OFrame(df)
hf['cv_fold_te'] = hf.kfold_column(n_folds=2, seed=54321)
hf['y_0'] = hf['y_0'].asfactor()
cat_features = ['x_0', 'x_1', 'x_2']

for item in cat_features:
    target_encoder = TargetEncoder(x=[item], y='y_0', fold_column = 'cv_fold_te')
    target_encoder.fit(hf)
    hf = target_encoder.transform(frame=hf, holdout_type='kfold',
                                  seed=54321, noise=0.0)
hf

答案 1 :(得分:0)

感谢大家让我们知道。断言是一种预防措施,因为我不确定是否可以更改订单。其余代码是在考虑了这一假设的情况下编写的,因此无论如何都可以安全地使用更改后的顺序,但是断言被遗忘了。添加了测试并删除了断言。现在,此问题已修复并合并。应该在即将发布的修订版本中可用。 0xdata.atlassian.net/browse/PUBDEV-6474