2010-05-27 21:50:20 +08:00
|
|
|
"""
|
|
|
|
|
===============================
|
|
|
|
|
Plot classification probability
|
|
|
|
|
===============================
|
2010-06-16 17:35:24 +08:00
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
Plot the classification probability for different classifiers. We use a 3 class
|
|
|
|
|
dataset, and we classify it with a Support Vector classifier, L1 and L2
|
|
|
|
|
penalized logistic regression with either a One-Vs-Rest or multinomial setting,
|
|
|
|
|
and Gaussian process classification.
|
2010-06-16 17:35:24 +08:00
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
Linear SVC is not a probabilistic classifier by default but it has a built-in
|
|
|
|
|
calibration option enabled in this example (`probability=True`).
|
|
|
|
|
|
|
|
|
|
The logistic regression with One-Vs-Rest is not a multiclass classifier out of
|
|
|
|
|
the box. As a result it has more trouble in separating class 2 and 3 than the
|
|
|
|
|
other estimators.
|
2010-05-27 21:50:20 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-06-05 00:15:44 +08:00
|
|
|
|
2010-04-23 01:58:11 +08:00
|
|
|
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2010-04-23 01:58:11 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2010-04-23 01:58:11 +08:00
|
|
|
import numpy as np
|
|
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
from sklearn.metrics import accuracy_score
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.linear_model import LogisticRegression
|
|
|
|
|
from sklearn.svm import SVC
|
2015-08-02 17:28:02 +08:00
|
|
|
from sklearn.gaussian_process import GaussianProcessClassifier
|
|
|
|
|
from sklearn.gaussian_process.kernels import RBF
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import datasets
|
2010-04-23 01:58:11 +08:00
|
|
|
|
|
|
|
|
iris = datasets.load_iris()
|
2011-12-20 01:16:51 +08:00
|
|
|
X = iris.data[:, 0:2] # we only take the first two features for visualization
|
2010-04-23 01:58:11 +08:00
|
|
|
y = iris.target
|
|
|
|
|
|
|
|
|
|
n_features = X.shape[1]
|
|
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
C = 10
|
2015-08-02 17:28:02 +08:00
|
|
|
kernel = 1.0 * RBF([1.0, 1.0]) # for GPC
|
2010-04-23 01:58:11 +08:00
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
# Create different classifiers.
|
|
|
|
|
classifiers = {
|
|
|
|
|
"L1 logistic": LogisticRegression(
|
|
|
|
|
C=C, penalty="l1", solver="saga", multi_class="multinomial", max_iter=10000
|
|
|
|
|
),
|
|
|
|
|
"L2 logistic (Multinomial)": LogisticRegression(
|
|
|
|
|
C=C, penalty="l2", solver="saga", multi_class="multinomial", max_iter=10000
|
|
|
|
|
),
|
|
|
|
|
"L2 logistic (OvR)": LogisticRegression(
|
|
|
|
|
C=C, penalty="l2", solver="saga", multi_class="ovr", max_iter=10000
|
|
|
|
|
),
|
|
|
|
|
"Linear SVC": SVC(kernel="linear", C=C, probability=True, random_state=0),
|
|
|
|
|
"GPC": GaussianProcessClassifier(kernel),
|
|
|
|
|
}
|
2010-06-16 17:35:24 +08:00
|
|
|
|
|
|
|
|
n_classifiers = len(classifiers)
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(figsize=(3 * 2, n_classifiers * 2))
|
|
|
|
|
plt.subplots_adjust(bottom=0.2, top=0.95)
|
2010-06-16 17:35:24 +08:00
|
|
|
|
2014-08-06 06:42:21 +08:00
|
|
|
xx = np.linspace(3, 9, 100)
|
|
|
|
|
yy = np.linspace(1, 5, 100).T
|
|
|
|
|
xx, yy = np.meshgrid(xx, yy)
|
|
|
|
|
Xfull = np.c_[xx.ravel(), yy.ravel()]
|
|
|
|
|
|
2014-02-02 19:52:31 +08:00
|
|
|
for index, (name, classifier) in enumerate(classifiers.items()):
|
2010-06-16 17:35:24 +08:00
|
|
|
classifier.fit(X, y)
|
|
|
|
|
|
|
|
|
|
y_pred = classifier.predict(X)
|
2018-09-25 01:22:40 +08:00
|
|
|
accuracy = accuracy_score(y, y_pred)
|
|
|
|
|
print("Accuracy (train) for %s: %0.1f%% " % (name, accuracy * 100))
|
2010-06-16 17:35:24 +08:00
|
|
|
|
2018-09-25 01:22:40 +08:00
|
|
|
# View probabilities:
|
2010-06-16 17:35:24 +08:00
|
|
|
probas = classifier.predict_proba(Xfull)
|
|
|
|
|
n_classes = np.unique(y_pred).size
|
|
|
|
|
for k in range(n_classes):
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.subplot(n_classifiers, n_classes, index * n_classes + k + 1)
|
|
|
|
|
plt.title("Class %d" % k)
|
2010-06-16 17:35:24 +08:00
|
|
|
if k == 0:
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.ylabel(name)
|
|
|
|
|
imshow_handle = plt.imshow(
|
|
|
|
|
probas[:, k].reshape((100, 100)), extent=(3, 9, 1, 5), origin="lower"
|
2014-05-15 10:35:13 +08:00
|
|
|
)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2010-06-16 17:35:24 +08:00
|
|
|
idx = y_pred == k
|
2010-11-06 22:42:55 +08:00
|
|
|
if idx.any():
|
2018-07-17 13:08:04 +08:00
|
|
|
plt.scatter(X[idx, 0], X[idx, 1], marker="o", c="w", edgecolor="k")
|
2010-06-16 17:35:24 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
ax = plt.axes([0.15, 0.04, 0.7, 0.05])
|
|
|
|
|
plt.title("Probability")
|
|
|
|
|
plt.colorbar(imshow_handle, cax=ax, orientation="horizontal")
|
2010-04-23 01:58:11 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|