2012-02-24 23:51:49 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
2012-02-28 08:05:16 +08:00
|
|
|
Logistic Regression 3-class Classifier
|
2012-02-24 23:51:49 +08:00
|
|
|
=========================================================
|
2013-06-06 18:43:32 +08:00
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
Show below is a logistic-regression classifiers decision boundaries on the
|
2018-09-25 01:22:40 +08:00
|
|
|
first two dimensions (sepal length and width) of the `iris
|
|
|
|
|
<https://en.wikipedia.org/wiki/Iris_flower_data_set>`_ dataset. The datapoints
|
|
|
|
|
are colored according to their labels.
|
2012-02-24 23:51:49 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
|
|
|
|
# Modified for documentation by Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-02-24 23:51:49 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2018-09-25 01:22:40 +08:00
|
|
|
from sklearn.linear_model import LogisticRegression
|
|
|
|
|
from sklearn import datasets
|
2012-02-24 23:51:49 +08:00
|
|
|
|
|
|
|
|
# import some data to play with
|
|
|
|
|
iris = datasets.load_iris()
|
2012-04-28 18:04:36 +08:00
|
|
|
X = iris.data[:, :2] # we only take the first two features.
|
2012-02-24 23:51:49 +08:00
|
|
|
Y = iris.target
|
|
|
|
|
|
2018-11-12 09:33:42 +08:00
|
|
|
# Create an instance of Logistic Regression Classifier and fit the data.
|
2020-09-02 22:04:06 +08:00
|
|
|
logreg = LogisticRegression(C=1e5)
|
2012-02-28 08:05:16 +08:00
|
|
|
logreg.fit(X, Y)
|
2012-02-24 23:51:49 +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-04-28 18:04:36 +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
|
2018-09-25 01:22:40 +08:00
|
|
|
h = 0.02 # step size in the mesh
|
2012-02-24 23:51:49 +08:00
|
|
|
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
|
|
|
|
|
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])
|
|
|
|
|
|
|
|
|
|
# Put the result into a color plot
|
|
|
|
|
Z = Z.reshape(xx.shape)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(1, figsize=(4, 3))
|
|
|
|
|
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)
|
2012-02-24 23:51:49 +08:00
|
|
|
|
|
|
|
|
# Plot also the training points
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors="k", cmap=plt.cm.Paired)
|
|
|
|
|
plt.xlabel("Sepal length")
|
|
|
|
|
plt.ylabel("Sepal width")
|
2012-02-24 23:51:49 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlim(xx.min(), xx.max())
|
|
|
|
|
plt.ylim(yy.min(), yy.max())
|
|
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2012-02-24 23:51:49 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|