Home > database >  cant unpack non-iterable float object in function with performance metrics
cant unpack non-iterable float object in function with performance metrics

Time:09-29

I am displaying the actual target values, stored as y, for a classification modeling task, along with the probabilistic predictions (probability of the positive class) stored as pred_probs.

# Actual y values saved as y:
np.random.seed(0)
y = np.round(np.random.rand(20)).astype(int)

# Predictions (probabilities of the positive class) saved as pred_probs:
np.random.seed(1)
pred_probs = np.round(np.random.rand(20),decimals=2)

print('Y values are {}'.format(y))
print('Predicted probabilities are {}'.format(pred_probs))

I then tried to create a function calc_metrics(). The function takes as inputs the true values y and the probabilistic predictions pred_probs and then calculates the following metrics on the predictions, using a threshold of >= 0.5 for predicting the positive class (1): Accuracy Recall Precision F1 Score I am trying to return a list of the four metrics above for the positive class (1) as float values, in the order shown above. I am purposefully not using SciKit Learn functions.

def calc_metrics(y,pred_probs):
    # YOUR CODE HERE
# evaluation
    preds=[int(x>0.5) for x in pred_probs]


# accuracy
    correct = 0
    for i in range(len(y)):
        if y[i]==preds[i]:
            correct  = 1
        acc = (float(correct /float(len(y)) * 100))
    #accurate_preds=sum(preds==y)
    #acc=accurate_preds/len(y)
    #acc = np.sum(np.equal(y_true,y_pred))/len(y)

## precision recall and fscore

    tp=sum([(preds[i]==1)&(y[i]==1) for i in range(len(y))])
    fp=sum([(preds[i]==1)&(y[i]==0) for i in range(len(y))])
    fn=sum([(preds[i]==0)&(y[i]==1) for i in range(len(y))])

    precision= (float(tp/(tp fp)))
    recall= (float(tp/(tp fn)))
    f1=(float(2*precision*recall/(precision recall)))
    
    return acc
    return recall
    return precision
    return f1

    raise NotImplementedError()

But when I run this test cell

acc,recall,precision,f1 = calc_metrics(y,pred_probs)
print('Your function calculated the metrics as follows:')
print('Accuracy: {:.3f}'.format(acc))
print('Recall: {:.3f}'.format(recall))
print('Precision: {:.3f}'.format(precision))
print('F1 Score: {:.3f}'.format(f1))

I get this error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-7a8e96fb7154> in <module>
      1 # Test cell
----> 2 acc,recall,precision,f1 = calc_metrics(y,pred_probs)
      3 print('Your function calculated the metrics as follows:')
      4 print('Accuracy: {:.3f}'.format(acc))
      5 print('Recall: {:.3f}'.format(recall))

TypeError: cannot unpack non-iterable float object

CodePudding user response:

Return only returns once, then get's out of the function.

Instead of:

return acc
return recall
return precision
return f1

Use:

return acc, recall, precision, f1
  • Related