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
|
|
|
|
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
|
2022-03-29 22:36:31 +08:00
|
|
|
from sklearn.inspection import DecisionBoundaryDisplay
|
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
|
|
|
|
2022-03-29 22:36:31 +08:00
|
|
|
_, ax = plt.subplots(figsize=(4, 3))
|
|
|
|
|
DecisionBoundaryDisplay.from_estimator(
|
|
|
|
|
logreg,
|
|
|
|
|
X,
|
|
|
|
|
cmap=plt.cm.Paired,
|
|
|
|
|
ax=ax,
|
|
|
|
|
response_method="predict",
|
|
|
|
|
plot_method="pcolormesh",
|
|
|
|
|
shading="auto",
|
|
|
|
|
xlabel="Sepal length",
|
|
|
|
|
ylabel="Sepal width",
|
|
|
|
|
eps=0.5,
|
|
|
|
|
)
|
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)
|
2012-02-24 23:51:49 +08:00
|
|
|
|
2022-03-29 22:36:31 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2012-02-24 23:51:49 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|