Gigi
Gigi

Reputation: 23

KeyError: True/False when trying to run prediction model

I am trying to check if the model predicts correctly if the Amazon review is positive or negative by running the following code:

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.')`

It has to return True if the review is positive and False if it is negative. It does return me the right answer BUT in the form of the KeyError below, any ideas how to solve it?

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

Upvotes: 1

Views: 324

Answers (1)

Costantino Grana
Costantino Grana

Reputation: 3418

I'm no expert on this, but your problem is a classic one: you need to debug. So change your code to a more manageable

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

Than fire up your debugger and step by step debug through this. After you've done it you will know the line which is causing the exception and you will be able to nail down the problem.

Finally, come back here and tell everybody what the problem was.

Upvotes: 1

Related Questions