Created
July 15, 2016 07:03
-
-
Save sungyongchoi/dd33dee939c3e7e6d49a6f4a06dd2046 to your computer and use it in GitHub Desktop.
Least Square Line through Python
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
# 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