vendredi 4 décembre 2020

How to resolve ZeroDivisionError: division by zero in pyspark

while working with pyspark logistic regression i got zero division error.   
    # compute TN, TP, FN, and FP
    predictions_lr.groupBy('indexedLabel', 'prediction').count().show()
    # Calculate the elements of the confusion matrix
    TN = predictions_lr.filter('prediction = 0 AND indexedLabel = prediction').count()
    TP = predictions_lr.filter('prediction = 1 AND indexedLabel = prediction').count()
    FN = predictions_lr.filter('prediction = 0 AND indexedLabel <> prediction').count()
    FP = predictions_lr.filter('prediction = 1 AND indexedLabel <> prediction').count()
    # calculate accuracy, precision, recall, and F1-score
    accuracy = (TN + TP) / (TN + TP + FN + FP)
    precision = TP / (TP + FP)
    recall = TP / (TP + FN)
    F =  2 * (precision*recall) / (precision + recall)
    print('n precision: %0.3f' % precision)
    print('n recall: %0.3f' % recall)
    print('n accuracy: %0.3f' % accuracy)
    print('n F1 score: %0.3f' % F) 

the tracebaack of the code results was mentioned below:

Traceback

   +------------+----------+------+
    |indexedLabel|prediction| count|
    +------------+----------+------+
    |         1.0|       0.0| 11559|
    |         0.0|       0.0|480248|
    +------------+----------+------+


    ---------------------------------------------------------------------------
    ZeroDivisionError                         Traceback (most recent call last)
    <ipython-input-23-9a66bdac7789> in <module>()
          8 # calculate accuracy, precision, recall, and F1-score
          9 accuracy = (TN + TP) / (TN + TP + FN + FP)
    ---> 10 precision = TP / (TP + FP)
         11 recall = TP / (TP + FN)
         12 F =  2 * (precision*recall) / (precision + recall)
    
    ZeroDivisionError: division by zero

can anyone suggest me why this error occured and how to resolve it...since i'm new to python ..if any silly errors please forgive me

Aucun commentaire:

Enregistrer un commentaire