Skip to content

Instantly share code, notes, and snippets.

@sungyongchoi
Created July 15, 2016 07:03
Show Gist options
  • Save sungyongchoi/dd33dee939c3e7e6d49a6f4a06dd2046 to your computer and use it in GitHub Desktop.
Save sungyongchoi/dd33dee939c3e7e6d49a6f4a06dd2046 to your computer and use it in GitHub Desktop.
Least Square Line through Python
# python 3.0
import numpy as np
import matplotlib.pyplot as mpl
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 5, 3, 8, 7])
A = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(A, y)[0]
import matplotlib.pyplot as plt
plt.plot(x, y, 'o', label='Original data', markersize=10)
plt.plot(x, m*x + c, 'r', label='Fitted line')
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment