2012-10-27 21:10:31 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
2013-07-28 19:07:20 +08:00
|
|
|
=====================
|
|
|
|
|
Classifier comparison
|
|
|
|
|
=====================
|
2013-06-26 22:34:18 +08:00
|
|
|
|
2012-10-27 21:10:31 +08:00
|
|
|
A comparison of a several classifiers in scikit-learn on synthetic datasets.
|
|
|
|
|
The point of this example is to illustrate the nature of decision boundaries
|
|
|
|
|
of different classifiers.
|
|
|
|
|
This should be taken with a grain of salt, as the intuition conveyed by
|
|
|
|
|
these examples does not necessarily carry over to real datasets.
|
|
|
|
|
|
2013-07-28 19:07:20 +08:00
|
|
|
Particularly in high-dimensional spaces, data can more easily be separated
|
2012-10-27 22:40:14 +08:00
|
|
|
linearly and the simplicity of classifiers such as naive Bayes and linear SVMs
|
2013-07-28 19:07:20 +08:00
|
|
|
might lead to better generalization than is achieved by other classifiers.
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2012-10-27 23:25:22 +08:00
|
|
|
The plots show training points in solid colors and testing points
|
|
|
|
|
semi-transparent. The lower right shows the classification accuracy on the test
|
2012-10-27 23:47:49 +08:00
|
|
|
set.
|
2012-10-27 21:10:31 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2012-10-27 21:10:31 +08:00
|
|
|
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
|
|
|
|
# Andreas Müller
|
|
|
|
|
# Modified for documentation by Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-10-27 21:10:31 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-12-27 04:13:58 +08:00
|
|
|
from matplotlib.colors import ListedColormap
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import train_test_split
|
2012-10-27 23:25:22 +08:00
|
|
|
from sklearn.preprocessing import StandardScaler
|
2012-10-28 00:27:33 +08:00
|
|
|
from sklearn.datasets import make_moons, make_circles, make_classification
|
2015-06-06 01:36:36 +08:00
|
|
|
from sklearn.neural_network import MLPClassifier
|
2012-10-27 21:10:31 +08:00
|
|
|
from sklearn.neighbors import KNeighborsClassifier
|
|
|
|
|
from sklearn.svm import SVC
|
2015-03-14 22:09:52 +08:00
|
|
|
from sklearn.gaussian_process import GaussianProcessClassifier
|
|
|
|
|
from sklearn.gaussian_process.kernels import RBF
|
2012-10-27 21:10:31 +08:00
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
2013-02-03 20:58:08 +08:00
|
|
|
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
|
2012-10-27 21:10:31 +08:00
|
|
|
from sklearn.naive_bayes import GaussianNB
|
2015-03-20 11:11:33 +08:00
|
|
|
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
|
2012-10-27 21:10:31 +08:00
|
|
|
|
|
|
|
|
h = .02 # step size in the mesh
|
|
|
|
|
|
2015-03-14 22:09:52 +08:00
|
|
|
names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process",
|
2015-06-06 01:36:36 +08:00
|
|
|
"Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
|
|
|
|
|
"Naive Bayes", "QDA"]
|
2015-03-14 22:09:52 +08:00
|
|
|
|
2012-12-25 20:16:05 +08:00
|
|
|
classifiers = [
|
|
|
|
|
KNeighborsClassifier(3),
|
2012-10-28 00:27:33 +08:00
|
|
|
SVC(kernel="linear", C=0.025),
|
2012-10-27 22:40:14 +08:00
|
|
|
SVC(gamma=2, C=1),
|
2017-08-29 07:58:55 +08:00
|
|
|
GaussianProcessClassifier(1.0 * RBF(1.0)),
|
2012-10-27 22:40:14 +08:00
|
|
|
DecisionTreeClassifier(max_depth=5),
|
|
|
|
|
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
|
2015-06-06 01:36:36 +08:00
|
|
|
MLPClassifier(alpha=1),
|
2013-02-03 20:58:08 +08:00
|
|
|
AdaBoostClassifier(),
|
2012-10-27 21:10:31 +08:00
|
|
|
GaussianNB(),
|
2015-03-20 11:11:33 +08:00
|
|
|
QuadraticDiscriminantAnalysis()]
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2012-12-25 20:16:05 +08:00
|
|
|
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
|
|
|
|
|
random_state=1, n_clusters_per_class=1)
|
2012-10-28 00:27:33 +08:00
|
|
|
rng = np.random.RandomState(2)
|
|
|
|
|
X += 2 * rng.uniform(size=X.shape)
|
|
|
|
|
linearly_separable = (X, y)
|
|
|
|
|
|
2012-10-27 22:40:14 +08:00
|
|
|
datasets = [make_moons(noise=0.3, random_state=0),
|
2012-10-28 00:27:33 +08:00
|
|
|
make_circles(noise=0.2, factor=0.5, random_state=1),
|
|
|
|
|
linearly_separable
|
|
|
|
|
]
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
figure = plt.figure(figsize=(27, 9))
|
2012-11-14 15:35:05 +08:00
|
|
|
i = 1
|
2012-10-27 23:25:22 +08:00
|
|
|
# iterate over datasets
|
2015-10-20 21:47:51 +08:00
|
|
|
for ds_cnt, ds in enumerate(datasets):
|
2012-10-27 23:25:22 +08:00
|
|
|
# preprocess dataset, split into training and test part
|
2012-10-27 21:10:31 +08:00
|
|
|
X, y = ds
|
2012-10-27 23:25:22 +08:00
|
|
|
X = StandardScaler().fit_transform(X)
|
2015-03-14 22:09:52 +08:00
|
|
|
X_train, X_test, y_train, y_test = \
|
|
|
|
|
train_test_split(X, y, test_size=.4, random_state=42)
|
2012-10-27 23:25:22 +08:00
|
|
|
|
2012-12-27 04:13:58 +08:00
|
|
|
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
|
|
|
|
|
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
|
|
|
|
|
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
|
|
|
|
|
np.arange(y_min, y_max, h))
|
|
|
|
|
|
|
|
|
|
# just plot the dataset first
|
2014-05-15 04:31:03 +08:00
|
|
|
cm = plt.cm.RdBu
|
2012-12-27 04:13:58 +08:00
|
|
|
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
|
2014-05-15 04:31:03 +08:00
|
|
|
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
|
2015-10-20 21:47:51 +08:00
|
|
|
if ds_cnt == 0:
|
|
|
|
|
ax.set_title("Input data")
|
2012-12-27 04:13:58 +08:00
|
|
|
# Plot the training points
|
2017-03-13 17:58:12 +08:00
|
|
|
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
|
|
|
|
|
edgecolors='k')
|
2012-12-27 04:13:58 +08:00
|
|
|
# and testing points
|
2017-03-13 17:58:12 +08:00
|
|
|
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6,
|
|
|
|
|
edgecolors='k')
|
2012-12-27 04:13:58 +08:00
|
|
|
ax.set_xlim(xx.min(), xx.max())
|
|
|
|
|
ax.set_ylim(yy.min(), yy.max())
|
|
|
|
|
ax.set_xticks(())
|
|
|
|
|
ax.set_yticks(())
|
|
|
|
|
i += 1
|
|
|
|
|
|
2012-10-27 23:25:22 +08:00
|
|
|
# iterate over classifiers
|
2012-11-14 15:35:05 +08:00
|
|
|
for name, clf in zip(names, classifiers):
|
2014-05-15 04:31:03 +08:00
|
|
|
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
|
2012-10-27 23:25:22 +08:00
|
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
score = clf.score(X_test, y_test)
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2013-04-12 02:51:28 +08:00
|
|
|
# Plot the decision boundary. For that, we will assign a color to each
|
2016-04-25 11:59:40 +08:00
|
|
|
# point in the mesh [x_min, x_max]x[y_min, y_max].
|
2012-10-27 21:10:31 +08:00
|
|
|
if hasattr(clf, "decision_function"):
|
|
|
|
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
|
|
|
|
else:
|
|
|
|
|
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
|
|
|
|
|
|
|
|
|
|
# Put the result into a color plot
|
|
|
|
|
Z = Z.reshape(xx.shape)
|
2012-12-27 04:13:58 +08:00
|
|
|
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
|
2012-10-27 21:10:31 +08:00
|
|
|
|
|
|
|
|
# Plot also the training points
|
2017-03-13 17:58:12 +08:00
|
|
|
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
|
|
|
|
|
edgecolors='k')
|
2012-10-27 23:25:22 +08:00
|
|
|
# and testing points
|
2012-12-27 04:13:58 +08:00
|
|
|
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
|
2017-03-13 17:58:12 +08:00
|
|
|
edgecolors='k', alpha=0.6)
|
2012-10-27 21:10:31 +08:00
|
|
|
|
|
|
|
|
ax.set_xlim(xx.min(), xx.max())
|
|
|
|
|
ax.set_ylim(yy.min(), yy.max())
|
|
|
|
|
ax.set_xticks(())
|
|
|
|
|
ax.set_yticks(())
|
2015-10-20 21:47:51 +08:00
|
|
|
if ds_cnt == 0:
|
|
|
|
|
ax.set_title(name)
|
2012-10-27 23:25:22 +08:00
|
|
|
ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
|
|
|
|
|
size=15, horizontalalignment='right')
|
2012-11-14 15:35:05 +08:00
|
|
|
i += 1
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2015-03-14 22:09:52 +08:00
|
|
|
plt.tight_layout()
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|