the_baahubali

Untitled

Apr 24th, 2022
1,897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. import csv
  2.  
  3. SPAM_WORDS = ["money"]
  4.  
  5. num_spam = 0
  6. num_ham = 0
  7. num_total = 0
  8. counter = 0
  9.  
  10. num_tp = 0
  11. num_tn = 0
  12. num_fp = 0
  13. num_fn = 0
  14.  
  15. with open('emails.csv') as file:
  16.    
  17.     lines = csv.reader(file, quotechar = '"',)
  18.     next(lines, None)
  19.    
  20.     for line in lines:
  21.         sno, label, content, labelnum = line
  22.        
  23.         num_total = num_total + 1
  24.         found_spam = []
  25.        
  26.        
  27.        
  28.         for word in SPAM_WORDS:
  29.             if word in content:
  30.                 num_spam = num_spam + 1
  31.                 found_spam.append(word)
  32.                 predicted_label = 'spam'
  33.             else:
  34.                 num_ham = num_ham + 1
  35.                 predicted_label = 'ham'
  36.                
  37.        
  38.         print('\n\n\nEmail #:', num_total)
  39.         print(f'\nEmail Sno: {sno} | Actual Label: {label}')      
  40.         print(f'\n\nContent: {content}')
  41.        
  42.         if len(found_spam):
  43.             print('\n -> Found SPAM words:', ' | '.join(found_spam))
  44.            
  45.         if label == 'spam' and predicted_label == 'spam':
  46.             num_tp = num_tp + 1
  47.         elif label == 'ham' and predicted_label == 'ham':
  48.             num_tn = num_tn + 1
  49.         elif label == 'spam' and predicted_label == 'ham':
  50.             num_fn = num_fn + 1
  51.         elif label == 'ham' and predicted_label == 'spam':
  52.             num_fp = num_fp + 1
  53.            
  54.         print(f'\nPrediction: {predicted_label} | Correct Prediction? {label == predicted_label}')
  55.         print(f'Total Emails Processed: {num_total} | Total Hams Predicted: {num_ham} | Total Spams Predicted: {num_spam}')
  56.         print(f'True Positive: {num_tp} | True Negative: {num_tn}')
  57.         print(f'False Positive: {num_fp} | False Negative: {num_fn}')
  58.        
  59.         print(f'\nSpam detected as spam: {num_tp}/{num_tp+num_fn} ({100*num_tp/(num_tp+num_fn) if (num_tp+num_fn) != 0 else 0} %)')
  60.         print(f'\nHam detected as spam: {num_fp}/{num_tn+num_fp} ({100*num_fp/(num_tn+num_fp) if (num_tn+num_fp) != 0 else 0} %)')
  61.        
  62.        
  63.        
  64.  
  65.    
  66.        
  67.        
  68.        
  69.        
Advertisement
Add Comment
Please, Sign In to add comment