KeyError:尝试运行预测模型时为True / False

时间:2020-11-07 16:20:58

标签: python pandas naivebayes

我正在尝试通过运行以下代码来检查模型是否正确预测了亚马逊评论的正面还是负面:

def predict_category(s, X_train=X_train, model=model):
    pred = model.predict([s])
    return y_train.loc[y_train[pred[0]]]

predict_category('These deserved a 5 star because of the price and their strength. Will be buying again when we eventually run out.')`

如果评论为肯定,则必须返回True,如果评论为否,则必须返回False。 它确实以下面的KeyError形式为我返回了正确的答案,但有任何解决方法的想法吗?

KeyError Traceback (most recent call last) <ipython-input-66-5ab570b98937> in <module> pandas\_libs\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()
KeyError: True

1 个答案:

答案 0 :(得分:0)

我不是专家,但是您的问题是经典的:您需要调试。因此,将您的代码更改为更易于管理的

def predict_category(s, X_train=X_train, model=model):
    pred = model.predict([s])
    a = pred[0]
    b = y_train[a] 
    c = y_train.loc[b]
    return c

比启动调试器并逐步调试通过。完成后,您将知道导致异常的行,并且可以确定问题所在。

最后,回到这里,告诉大家问题出在哪里。