Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- SPAM_WORDS = ["money"]
- num_spam = 0
- num_ham = 0
- num_total = 0
- counter = 0
- num_tp = 0
- num_tn = 0
- num_fp = 0
- num_fn = 0
- with open('emails.csv') as file:
- lines = csv.reader(file, quotechar = '"',)
- next(lines, None)
- for line in lines:
- sno, label, content, labelnum = line
- num_total = num_total + 1
- found_spam = []
- for word in SPAM_WORDS:
- if word in content:
- num_spam = num_spam + 1
- found_spam.append(word)
- predicted_label = 'spam'
- else:
- num_ham = num_ham + 1
- predicted_label = 'ham'
- print('\n\n\nEmail #:', num_total)
- print(f'\nEmail Sno: {sno} | Actual Label: {label}')
- print(f'\n\nContent: {content}')
- if len(found_spam):
- print('\n -> Found SPAM words:', ' | '.join(found_spam))
- if label == 'spam' and predicted_label == 'spam':
- num_tp = num_tp + 1
- elif label == 'ham' and predicted_label == 'ham':
- num_tn = num_tn + 1
- elif label == 'spam' and predicted_label == 'ham':
- num_fn = num_fn + 1
- elif label == 'ham' and predicted_label == 'spam':
- num_fp = num_fp + 1
- print(f'\nPrediction: {predicted_label} | Correct Prediction? {label == predicted_label}')
- print(f'Total Emails Processed: {num_total} | Total Hams Predicted: {num_ham} | Total Spams Predicted: {num_spam}')
- print(f'True Positive: {num_tp} | True Negative: {num_tn}')
- print(f'False Positive: {num_fp} | False Negative: {num_fn}')
- 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} %)')
- 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} %)')
Advertisement
Add Comment
Please, Sign In to add comment