Skip to content

Instantly share code, notes, and snippets.

@pragatibaheti
Created April 7, 2020 12:51
Show Gist options
  • Save pragatibaheti/25ff47918bf779fa6204ed9c326752f1 to your computer and use it in GitHub Desktop.
Save pragatibaheti/25ff47918bf779fa6204ed9c326752f1 to your computer and use it in GitHub Desktop.
# we can use sklearn algorithms in NLTK
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
# Define models to train
names = ["SVM","K Nearest Neighbors", "Decision Tree", "Random Forest", "Logistic Regression", "SGD Classifier",
"Naive Bayes", "SVM Linear"]
classifier = [ SVC(kernel = 'linear'),KNeighborsClassifier(), DecisionTreeClassifier(), RandomForestClassifier(), LogisticRegression(), SGDClassifier(max_iter = 100), MultinomialNB(), SVC(kernel = 'linear')]
models = zip(names, classifier)
for name, model in models:
nltk_model = SklearnClassifier(model)
nltk_model.train(training)
accuracy = nltk.classify.accuracy(nltk_model, testing)*100
print("{} Accuracy: {}".format(name, accuracy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment