2012-10-27 21:10:31 +08:00
|
|
|
"""
|
2013-07-28 19:07:20 +08:00
|
|
|
=====================
|
|
|
|
|
Classifier comparison
|
|
|
|
|
=====================
|
2013-06-26 22:34:18 +08:00
|
|
|
|
2023-08-24 20:28:56 +08:00
|
|
|
A comparison of several classifiers in scikit-learn on synthetic datasets.
|
2012-10-27 21:10:31 +08:00
|
|
|
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
|
|
|
|
2021-10-22 21:33:22 +08:00
|
|
|
"""
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2024-07-23 04:05:03 +08:00
|
|
|
# Authors: The scikit-learn developers
|
2024-06-13 21:51:09 +08:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-10-27 21:10:31 +08:00
|
|
|
import numpy as np
|
2012-12-27 04:13:58 +08:00
|
|
|
from matplotlib.colors import ListedColormap
|
2023-06-21 23:50:07 +08:00
|
|
|
|
2012-10-28 00:27:33 +08:00
|
|
|
from sklearn.datasets import make_circles, make_classification, make_moons
|
2015-03-20 11:11:33 +08:00
|
|
|
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
|
2013-02-03 20:58:08 +08:00
|
|
|
from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier
|
2015-03-14 22:09:52 +08:00
|
|
|
from sklearn.gaussian_process import GaussianProcessClassifier
|
|
|
|
|
from sklearn.gaussian_process.kernels import RBF
|
2022-03-29 22:36:31 +08:00
|
|
|
from sklearn.inspection import DecisionBoundaryDisplay
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import train_test_split
|
2012-10-28 00:27:33 +08:00
|
|
|
from sklearn.naive_bayes import GaussianNB
|
2012-10-27 21:10:31 +08:00
|
|
|
from sklearn.neighbors import KNeighborsClassifier
|
2015-06-06 01:36:36 +08:00
|
|
|
from sklearn.neural_network import MLPClassifier
|
2022-09-09 16:38:19 +08:00
|
|
|
from sklearn.pipeline import make_pipeline
|
2012-10-27 23:25:22 +08:00
|
|
|
from sklearn.preprocessing import StandardScaler
|
2012-10-27 21:10:31 +08:00
|
|
|
from sklearn.svm import SVC
|
|
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
2021-10-07 16:13:00 +08:00
|
|
|
|
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),
|
2023-08-02 20:22:46 +08:00
|
|
|
SVC(kernel="linear", C=0.025, random_state=42),
|
|
|
|
|
SVC(gamma=2, C=1, random_state=42),
|
|
|
|
|
GaussianProcessClassifier(1.0 * RBF(1.0), random_state=42),
|
|
|
|
|
DecisionTreeClassifier(max_depth=5, random_state=42),
|
|
|
|
|
RandomForestClassifier(
|
|
|
|
|
max_depth=5, n_estimators=10, max_features=1, random_state=42
|
|
|
|
|
),
|
|
|
|
|
MLPClassifier(alpha=1, max_iter=1000, random_state=42),
|
2024-10-09 04:15:43 +08:00
|
|
|
AdaBoostClassifier(random_state=42),
|
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
|
2015-03-14 22:09:52 +08:00
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
|
|
|
X, y, test_size=0.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() - 0.5, X[:, 0].max() + 0.5
|
|
|
|
|
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
|
|
|
|
|
|
|
|
|
|
# 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")
|
2017-12-05 23:55:00 +08:00
|
|
|
# Plot the 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"
|
|
|
|
|
)
|
2022-03-29 22:36:31 +08:00
|
|
|
ax.set_xlim(x_min, x_max)
|
|
|
|
|
ax.set_ylim(y_min, y_max)
|
2012-12-27 04:13:58 +08:00
|
|
|
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)
|
2022-09-09 16:38:19 +08:00
|
|
|
|
|
|
|
|
clf = make_pipeline(StandardScaler(), clf)
|
2012-10-27 23:25:22 +08:00
|
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
score = clf.score(X_test, y_test)
|
2022-03-29 22:36:31 +08:00
|
|
|
DecisionBoundaryDisplay.from_estimator(
|
|
|
|
|
clf, X, cmap=cm, alpha=0.8, ax=ax, eps=0.5
|
|
|
|
|
)
|
2012-10-27 21:10:31 +08:00
|
|
|
|
2017-12-05 23:55:00 +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"
|
|
|
|
|
)
|
2017-12-05 23:55:00 +08:00
|
|
|
# Plot the 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
|
|
|
|
2022-03-29 22:36:31 +08:00
|
|
|
ax.set_xlim(x_min, x_max)
|
|
|
|
|
ax.set_ylim(y_min, y_max)
|
2012-10-27 21:10:31 +08:00
|
|
|
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(
|
2022-03-29 22:36:31 +08:00
|
|
|
x_max - 0.3,
|
|
|
|
|
y_min + 0.3,
|
2012-10-27 23:25:22 +08:00
|
|
|
("%.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()
|