Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement in confusion() #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/tweet_classifier_BERT.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,17 @@ def confusion(prediction, truth):
- 0 and 0 (True Negative)
- 0 and 1 (False Negative)
"""
# Getting values
prediction = np.argmax(prediction, axis=1).flatten()
truth = truth.flatten()
confusion_vector = prediction / truth
# Element-wise division of the 2 arrays returns a new tensor which holds a
# unique value for each case:
# 1 where prediction and truth are 1 (True Positive)
# inf where prediction is 1 and truth is 0 (False Positive)
# nan where prediction and truth are 0 (True Negative)
# 0 where prediction is 0 and truth is 1 (False Negative)

true_positives = np.sum(confusion_vector == 1)
false_positives = np.sum(confusion_vector == float('inf'))
true_negatives = np.sum(np.isnan(confusion_vector))
false_negatives = np.sum(confusion_vector == 0)

# Applying filtering
positives = np.where(prediction == 1)
negatives = np.where(prediction == 0)
# Building the confusion matrix
true_positives = np.size(np.where(truth[positives] == 1))
false_positives = np.size(np.where(truth[positives] == 0))
true_negatives = np.size(np.where(truth[negatives] == 0))
false_negatives = np.size(np.where(truth[negatives] == 1))
return true_positives, false_positives, true_negatives, false_negatives


Expand Down