Created
July 18, 2015 18:15
-
-
Save itarato/e7fe52c0262c4c3d3d1b to your computer and use it in GitHub Desktop.
Linear regression with normal equation
This file contains hidden or 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
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def plot_result(O): | |
| sample_size = 50 | |
| linX = np.linspace(np.min(npdata[:,1]), np.max(npdata[:,1]), num=sample_size) | |
| testX = np.concatenate((np.ones((sample_size, 1)), linX.reshape((sample_size, 1))), axis=1) | |
| y = np.dot(testX, O) | |
| plt.plot(linX, y[:,0], 'r-') | |
| if __name__ == '__main__': | |
| data = file("/Users/itarato/Desktop/weight.csv", 'r') | |
| npdata = np.genfromtxt(data, delimiter=',', skip_header=1) | |
| m = len(npdata) | |
| Y = npdata[:, 0].reshape((m, 1)) | |
| plt.plot(npdata[:,1], npdata[:,0], 'b^') | |
| X = np.concatenate((np.ones((m, 1)), npdata[:, 1].reshape((m, 1))), axis=1) | |
| O = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), Y) | |
| plot_result(O) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment