Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active July 17, 2019 18:58
Show Gist options
  • Save michelkana/7f76e41a896e2697c84c7677d220dcf4 to your computer and use it in GitHub Desktop.
Save michelkana/7f76e41a896e2697c84c7677d220dcf4 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
# Let's create a sample line
x = np.sort(np.random.uniform(-2.0, 3.0, 100))
y = 2*x + 1
# Let's add normal noise to the line
y = y + np.random.normal(0, 1, 100)
# Let's compute the intercept and slope of
# regression line
beta_1 = np.sum(np.multiply((x - np.mean(x)),(y - np.mean(y))))/
np.sum((x - np.mean(x))**2)
beta_0 = np.mean(y) - beta_1*np.mean(x)
# Let's plot the data and the regression line
plt.plot(x,y, label='data')
plt.plot(x, beta_0 + beta_1*x, label='regression line')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('sample line with noise');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment