Skip to content

Instantly share code, notes, and snippets.

@HeenaR17
Created October 25, 2020 19:29
Show Gist options
  • Save HeenaR17/3594b615681c4ab9c8dc527322629a73 to your computer and use it in GitHub Desktop.
Save HeenaR17/3594b615681c4ab9c8dc527322629a73 to your computer and use it in GitHub Desktop.
from sklearn.naive_bayes import MultinomialNBnb_classifier = MultinomialNB()nb_classifier.fit(X_train, y_train)
nb_y_pred = nb_classifier.predict(X_test)
from sklearn.metrics import accuracy_score, precision_score, recall_score
score1 = accuracy_score(y_test, nb_y_pred)
score2 = precision_score(y_test, nb_y_pred)
score3 = recall_score(y_test, nb_y_pred)
print("---- Scores ----")
print("Accuracy score is: {}%".format(round(score1*100,2)))
print("Precision score is: {}".format(round(score2,2)))
print("Recall score is: {}".format(round(score3,2)))
best_accuracy = 0.0
alpha_val = 0.0
for i in np.arange(0.1,1.1,0.1):
temp_classifier = MultinomialNB(alpha=i)
temp_classifier.fit(X_train, y_train)
temp_y_pred = temp_classifier.predict(X_test)
score = accuracy_score(y_test, temp_y_pred)
print("Accuracy score for alpha={} is: {}%".format(round(i,1), round(score*100,2)))
if score>best_accuracy:
best_accuracy = score
alpha_val = i
print('--------------------------------------------')
print('The best accuracy is {}% with alpha value as {}'.format(round(best_accuracy*100, 2), round(alpha_val,1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment