Skip to content

Instantly share code, notes, and snippets.

@jenncross
Created March 26, 2019 19:01
Show Gist options
  • Save jenncross/dc361820d1823ca1088037ec4599e81e to your computer and use it in GitHub Desktop.
Save jenncross/dc361820d1823ca1088037ec4599e81e to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
x = np.linspace(1,100,200)
error = (np.random.normal(size=200))*1000 #generate random errror centered at 0
y = 20*x + 100 + error #example data with slope, intercept and random error
#y = x**2 +20*x + 100 + error
plt.figure()
plt.plot(x,y,".")
plt.show()
x_m = x.mean()
y_m = y.mean()
x_distances = x - x_m
y_distances = y - y_m
numer = np.sum(x_distances*y_distances)
denom = np.sum(x_distances**2)
slope = numer/denom
intercept = y_m - slope*x_m
plt.figure()
plt.plot(x,y,".", x, intercept+slope*x, "-")
plt.show()
"""
errors = y - (intercept+slope*x)
plt.figure()
plt.plot(x,errors,"r.")
plt.show()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment