Created
July 18, 2015 17:51
-
-
Save itarato/93f6cba908ce45461d36 to your computer and use it in GitHub Desktop.
Linear regression of my weight
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-') | |
| def feature_scale(data): | |
| for i in range(len(data[0])): | |
| std = np.std(data[:,i]) | |
| median = np.median(data[:,i]) | |
| data[:,i] = (data[:,i] - median) / std | |
| if __name__ == '__main__': | |
| data = file("/Users/itarato/Desktop/weight.csv", 'r') | |
| npdata = np.genfromtxt(data, delimiter=',', skip_header=1) | |
| feature_scale(npdata) | |
| m = len(npdata) | |
| Y = npdata[:, 0].reshape((m, 1)) | |
| a = 0.01 | |
| iteration = 10000 | |
| plt.plot(npdata[:,1], npdata[:,0], 'b^') | |
| X = np.concatenate((np.ones((m, 1)), npdata[:, 1].reshape((m, 1))), axis=1) | |
| O = np.array([0, 1]).reshape((2,1)) | |
| a_per_m = (a / m) | |
| for i in range(iteration): | |
| h = np.dot(X, O) | |
| diff = h - Y | |
| O = O - (np.sum(X * diff, axis=0) * a_per_m).reshape((2, 1)) | |
| plot_result(O) | |
| print(O) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment