text_plain_output_1.png
from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import GridSearchCV, cross_val_score, train_test_split

from sklearn.naive_bayes import MultinomialNB

from sklearn.neural_network import MLPClassifier

from sklearn.pipeline import Pipeline

from sklearn.svm import SVC

maxent_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', LogisticRegression())])
nb_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', MultinomialNB())])
svm_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', SVC())])
nn_pipeline = Pipeline([('tfidf', TfidfVectorizer()), ('classifier', MLPClassifier())])

maxent_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__C': [0.1, 1, 10]}
maxent_grid = GridSearchCV(maxent_pipeline, maxent_params, cv=10, scoring='accuracy')
maxent_grid.fit(X_train, y_train)
best_maxent_clf = maxent_grid.best_estimator_

nb_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__alpha': [0.1, 1, 10]}
nb_grid = GridSearchCV(nb_pipeline, nb_params, cv=10, scoring='accuracy')
nb_grid.fit(X_train, y_train)
best_nb_clf = nb_grid.best_estimator_

svm_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__C': [0.1, 1, 10]}
svm_grid = GridSearchCV(svm_pipeline, svm_params, cv=10, scoring='accuracy')
svm_grid.fit(X_train, y_train)
best_svm_clf = svm_grid.best_estimator_

nn_params = {'tfidf__ngram_range': [(1, 1), (1, 2)], 'classifier__hidden_layer_sizes': [(100,), (200,), (300,)]}
nn_grid = GridSearchCV(nn_pipeline, nn_params, cv=10, scoring='accuracy')
nn_grid.fit(X_train, y_train)
best_nn_clf = nn_grid.best_estimator_

print('Best-fitting model in each category:')
print('MaxEnt (Logistic Regression):', maxent_grid.best_params_)
print('Naïve Bayes:', nb_grid.best_params_)
print('SVM:', svm_grid.best_params_)
print('Neural Network:', nn_grid.best_params_)
print('\nScores:')
print('MaxEnt (Logistic Regression):', maxent_grid.best_score_)
print('Naïve Bayes:', nb_grid.best_score_)
print('SVM:', svm_grid.best_score_)
print('Neural Network:', nn_grid.best_score_)