Last active
July 5, 2020 16:18
-
-
Save dmesquita/1dca6966fffe06d2882c9dabeb8e9ada to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import fetch_20newsgroups | |
from sklearn.feature_extraction.text import TfidfVectorizer | |
from sklearn.naive_bayes import MultinomialNB | |
from sklearn.metrics import precision_recall_curve, auc | |
categories = ["comp.graphics","sci.space"] | |
newsgroups_train = fetch_20newsgroups(subset='train', categories=categories) | |
newsgroups_test = fetch_20newsgroups(subset='test', categories=categories) | |
newsgroups_all = fetch_20newsgroups(subset='all', categories=categories) | |
vectorizer = TfidfVectorizer() | |
vectorizer.fit(newsgroups_all.data) | |
X = vectorizer.transform(newsgroups_train.data) | |
clf = MultinomialNB(alpha=0.1) | |
clf.fit(X,newsgroups_train.target) | |
X_predict = vectorizer.transform(newsgroups_test.data) | |
y_pred_scores = clf.predict_proba(X_predict) | |
y_true = newsgroups_test.target | |
precision, recall, _ = precision_recall_curve(y_true, y_pred_scores[:, -1]) | |
auc = auc(recall, precision) | |
print(auc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment