Home > Net >  Getting an error trying to invoke the predict() function to calculate model predictions using create
Getting an error trying to invoke the predict() function to calculate model predictions using create

Time:11-12

Here's my beginning code:

import numpy as np

Create predict() function accepting a prediction probabilities list and threshold value

def predict(predict_prob,thresh):
    pred = []  
    for i in range(len(predict_prob)):
        if predict_prob[i] >= thresh: 
            pred.append(1) 
        else:
            pred.append(0)
    return pred

Here's what I'm trying to do:

invoke the predict() function to calculate the model predictions using those variables. Save this output as preds and print it out.

prediction probabilities

probs = [0.886,0.375,0.174,0.817,0.574,0.319,0.812,0.314,0.098,0.741,
         0.847,0.202,0.31,0.073,0.179,0.917,0.64,0.388,0.116,0.72]

threshold value

thresh = 0.5

prediction values

preds = predict(predict_prob, thresh) #calling predict ERROR

Here's the error:

NameError: name 'predict_prob' is not defined

But it is defined earlier when I created the function, so not sure why it's giving an error when I ran the earlier code and it didn't error on me.

CodePudding user response:

You are calling the function the wrong way. In this case, it should be:

preds = predict(probs, thresh)

You can't use 'predict_prob' outside of the function. It is defined inside and because of that it is a local variable. Also, you have 'probs' and 'thresh' perfectly prepared.

I hope that helps.

  • Related