2012-02-24 23:51:49 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
2016-10-25 16:19:57 +08:00
|
|
|
Logistic function
|
2012-02-24 23:51:49 +08:00
|
|
|
=========================================================
|
2013-06-06 18:30:42 +08:00
|
|
|
|
2016-10-25 16:19:57 +08:00
|
|
|
Shown in the plot is how the logistic regression would, in this
|
2012-04-28 04:53:09 +08:00
|
|
|
synthetic dataset, classify values as either 0 or 1,
|
2016-10-25 16:19:57 +08:00
|
|
|
i.e. class one or two, using the logistic curve.
|
2012-02-24 23:51:49 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
# Code source: Gael Varoquaux
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-02-24 23:51:49 +08:00
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2021-11-08 23:32:11 +08:00
|
|
|
from sklearn.linear_model import LogisticRegression, LinearRegression
|
2019-01-08 15:35:19 +08:00
|
|
|
from scipy.special import expit
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2021-11-08 23:32:11 +08:00
|
|
|
# Generate a toy dataset, it's just a straight line with some Gaussian noise:
|
2011-12-18 19:39:53 +08:00
|
|
|
xmin, xmax = -5, 5
|
|
|
|
|
n_samples = 100
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
X = np.random.normal(size=n_samples)
|
2020-06-24 22:51:51 +08:00
|
|
|
y = (X > 0).astype(float)
|
2012-04-28 04:53:09 +08:00
|
|
|
X[X > 0] *= 4
|
|
|
|
|
X += 0.3 * np.random.normal(size=n_samples)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
X = X[:, np.newaxis]
|
2018-09-25 01:22:40 +08:00
|
|
|
|
|
|
|
|
# Fit the classifier
|
2021-11-08 23:32:11 +08:00
|
|
|
clf = LogisticRegression(C=1e5)
|
2011-12-18 19:39:53 +08:00
|
|
|
clf.fit(X, y)
|
|
|
|
|
|
|
|
|
|
# and plot the result
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(1, figsize=(4, 3))
|
|
|
|
|
plt.clf()
|
|
|
|
|
plt.scatter(X.ravel(), y, color="black", zorder=20)
|
2011-12-18 19:39:53 +08:00
|
|
|
X_test = np.linspace(-5, 10, 300)
|
2012-04-28 04:53:09 +08:00
|
|
|
|
2019-01-08 15:35:19 +08:00
|
|
|
loss = expit(X_test * clf.coef_ + clf.intercept_).ravel()
|
2016-10-25 16:19:57 +08:00
|
|
|
plt.plot(X_test, loss, color="red", linewidth=3)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2021-11-08 23:32:11 +08:00
|
|
|
ols = LinearRegression()
|
2011-12-18 19:39:53 +08:00
|
|
|
ols.fit(X, y)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(X_test, ols.coef_ * X_test + ols.intercept_, linewidth=1)
|
|
|
|
|
plt.axhline(0.5, color=".5")
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.ylabel("y")
|
|
|
|
|
plt.xlabel("X")
|
2016-10-25 16:19:57 +08:00
|
|
|
plt.xticks(range(-5, 10))
|
|
|
|
|
plt.yticks([0, 0.5, 1])
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.ylim(-0.25, 1.25)
|
|
|
|
|
plt.xlim(-4, 10)
|
2016-10-25 16:19:57 +08:00
|
|
|
plt.legend(
|
|
|
|
|
("Logistic Regression Model", "Linear Regression Model"),
|
|
|
|
|
loc="lower right",
|
|
|
|
|
fontsize="small",
|
|
|
|
|
)
|
2018-09-25 01:22:40 +08:00
|
|
|
plt.tight_layout()
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|