Created
September 13, 2018 13:22
-
-
Save bbengfort/2261d7322a1771e4c3055b4ba35d5a6d to your computer and use it in GitHub Desktop.
Create OLS example image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline | |
import numpy as np | |
import pandas as pd | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
from sklearn.datasets import make_regression | |
from sklearn.preprocessing import StandardScaler | |
from matplotlib.patches import Rectangle | |
sns.set_context('notebook') | |
sns.set_style('whitegrid') | |
sns.set_palette('deep') | |
_, ax = plt.subplots(figsize=(9,6)) | |
X, y, coef = make_regression(n_samples=10, n_features=1, noise=35.0, coef=True, random_state=32) | |
X = X.ravel() | |
lims = (X.min()-0.5, X.max()+0.5) | |
xl = np.linspace(*lims) | |
yl = np.dot(xl,coef) | |
yp = np.dot(X,coef) | |
ax.plot(xl, yl, ls='--') | |
ax.scatter(X, y,) | |
# ax.set_xticklabels([]) | |
# ax.set_yticklabels([]) | |
ax.set_xlabel('$x$') | |
ax.set_ylabel('$y$') | |
ax.set_xlim(*lims) | |
for xi, yi, yhi in zip(X, y, yp): | |
err = yhi - yi | |
rect = Rectangle((xi,yi), err/100, err, alpha=0.3, ls='solid', fill=True, ec='r', fc='r') | |
ax.add_patch(rect) | |
plt.savefig("figures/ordinary_least_squares.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment