|
13 | 13 | # Code source: Gael Varoquaux
|
14 | 14 | # License: BSD 3 clause
|
15 | 15 |
|
16 |
| -import numpy as np |
17 | 16 | import matplotlib.pyplot as plt
|
18 |
| - |
19 |
| -from sklearn.linear_model import LogisticRegression, LinearRegression |
| 17 | +import numpy as np |
20 | 18 | from scipy.special import expit
|
| 19 | +from sklearn.linear_model import LinearRegression, LogisticRegression |
21 | 20 |
|
22 | 21 | # Generate a toy dataset, it's just a straight line with some Gaussian noise:
|
23 | 22 | xmin, xmax = -5, 5
|
|
37 | 36 | # and plot the result
|
38 | 37 | plt.figure(1, figsize=(4, 3))
|
39 | 38 | plt.clf()
|
40 |
| -plt.scatter(X.ravel(), y, color="black", zorder=20) |
| 39 | +plt.scatter(X.ravel(), y, label="example data", color="black", zorder=20) |
41 | 40 | X_test = np.linspace(-5, 10, 300)
|
42 | 41 |
|
43 | 42 | loss = expit(X_test * clf.coef_ + clf.intercept_).ravel()
|
44 |
| -plt.plot(X_test, loss, color="red", linewidth=3) |
| 43 | +plt.plot(X_test, loss, label="Logistic Regression Model", color="red", linewidth=3) |
45 | 44 |
|
46 | 45 | ols = LinearRegression()
|
47 | 46 | ols.fit(X, y)
|
48 |
| -plt.plot(X_test, ols.coef_ * X_test + ols.intercept_, linewidth=1) |
| 47 | +plt.plot( |
| 48 | + X_test, |
| 49 | + ols.coef_ * X_test + ols.intercept_, |
| 50 | + label="Linear Regression Model", |
| 51 | + linewidth=1, |
| 52 | +) |
49 | 53 | plt.axhline(0.5, color=".5")
|
50 | 54 |
|
51 | 55 | plt.ylabel("y")
|
|
55 | 59 | plt.ylim(-0.25, 1.25)
|
56 | 60 | plt.xlim(-4, 10)
|
57 | 61 | plt.legend(
|
58 |
| - ("Logistic Regression Model", "Linear Regression Model"), |
59 | 62 | loc="lower right",
|
60 | 63 | fontsize="small",
|
61 | 64 | )
|
|
0 commit comments