2010-05-27 21:50:20 +08:00
|
|
|
"""
|
|
|
|
|
===============================
|
|
|
|
|
Plot classification probability
|
|
|
|
|
===============================
|
2010-06-16 17:35:24 +08:00
|
|
|
|
|
|
|
|
Plot the classification probability for different classifiers. We use a 3
|
2014-08-06 06:42:21 +08:00
|
|
|
class dataset, and we classify it with a Support Vector classifier, L1
|
|
|
|
|
and L2 penalized logistic regression, and L2 penalized logistic
|
|
|
|
|
regression with a multinomial setting.
|
2010-06-16 17:35:24 +08:00
|
|
|
|
|
|
|
|
The logistic regression is not a multiclass classifier out of the box. As
|
|
|
|
|
a result it can identify only the first class.
|
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
|
|
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.linear_model import LogisticRegression
|
|
|
|
|
from sklearn.svm import SVC
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
C = 1.0
|
|
|
|
|
|
2010-06-16 17:35:24 +08:00
|
|
|
# Create different classifiers. The logistic regression cannot do
|
|
|
|
|
# multiclass out of the box.
|
2012-12-25 20:16:05 +08:00
|
|
|
classifiers = {'L1 logistic': LogisticRegression(C=C, penalty='l1'),
|
|
|
|
|
'L2 logistic': LogisticRegression(C=C, penalty='l2'),
|
2013-07-28 00:44:22 +08:00
|
|
|
'Linear SVC': SVC(kernel='linear', C=C, probability=True,
|
2014-08-06 06:42:21 +08:00
|
|
|
random_state=0),
|
|
|
|
|
'Multinomial Logistic': LogisticRegression(
|
|
|
|
|
C=C, solver='lbfgs', multi_class='multinomial'
|
|
|
|
|
)}
|
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=.2, top=.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)
|
|
|
|
|
classif_rate = np.mean(y_pred.ravel() == y.ravel()) * 100
|
2012-12-25 20:16:05 +08:00
|
|
|
print("classif_rate for %s : %f " % (name, classif_rate))
|
2010-06-16 17:35:24 +08:00
|
|
|
|
|
|
|
|
# View probabilities=
|
|
|
|
|
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)),
|
2014-05-15 10:35:13 +08:00
|
|
|
extent=(3, 9, 1, 5), origin='lower')
|
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():
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(X[idx, 0], X[idx, 1], marker='o', c='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()
|