2012-02-24 23:51:49 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- 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
|
|
|
print(__doc__)
|
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
|
|
|
|
2012-04-28 04:53:09 +08:00
|
|
|
from sklearn import linear_model
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
# this is our test set, it's just a straight line with some
|
2014-06-02 04:53:23 +08:00
|
|
|
# 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)
|
|
|
|
|
y = (X > 0).astype(np.float)
|
2012-04-28 04:53:09 +08:00
|
|
|
X[X > 0] *= 4
|
|
|
|
|
X += .3 * np.random.normal(size=n_samples)
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
X = X[:, np.newaxis]
|
|
|
|
|
# run the classifier
|
|
|
|
|
clf = linear_model.LogisticRegression(C=1e5)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
def model(x):
|
2012-04-28 04:53:09 +08:00
|
|
|
return 1 / (1 + np.exp(-x))
|
|
|
|
|
loss = model(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
|
|
|
|
|
|
|
|
ols = linear_model.LinearRegression()
|
|
|
|
|
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(.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(-.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')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|