Skip to content

Instantly share code, notes, and snippets.

@curious-attempt-bunny
Created January 4, 2017 06:31
Show Gist options
  • Save curious-attempt-bunny/57aec19fdff46c5ef0fa80ceb7d08f62 to your computer and use it in GitHub Desktop.
Save curious-attempt-bunny/57aec19fdff46c5ef0fa80ceb7d08f62 to your computer and use it in GitHub Desktop.
Compare classifiers
# Compare Classifiers
from matplotlib import pyplot
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
results = []
names = []
scoring = 'accuracy'
for name, model in models:
kfold = KFold(n_splits=10, random_state=7)
cv_results = cross_val_score(model, X, Y, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
fig = pyplot.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
pyplot.boxplot(results)
ax.set_xticklabels(names)
pyplot.show()
@curious-attempt-bunny
Copy link
Author

Note that this snippet originates from the Machine Learning Mastery book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment