Created
January 4, 2017 06:31
-
-
Save curious-attempt-bunny/57aec19fdff46c5ef0fa80ceb7d08f62 to your computer and use it in GitHub Desktop.
Compare classifiers
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
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this snippet originates from the Machine Learning Mastery book.