Created
March 8, 2016 13:25
-
-
Save mitallast/22bbcabfae48e16cc7a8 to your computer and use it in GitHub Desktop.
Predict next day open price for USD by previous 5 days
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 pandas_datareader.data as web | |
| import matplotlib.pyplot as plt | |
| from sklearn.linear_model import LinearRegression | |
| import numpy as np | |
| import datetime | |
| start = datetime.datetime(2010, 1, 1) | |
| end = datetime.datetime(2015, 3, 8) | |
| usd = web.DataReader("USD", 'google', start, end) | |
| usd = usd.dropna() | |
| l, s = usd.shape | |
| n = 5 | |
| data = np.zeros((l-n, (s * (n-1) + 1))) | |
| for i in xrange(0, l-n): | |
| data[i] = usd.values[i:i+5, :].flatten()[:-4] | |
| train = data[:l-20, :] | |
| test = data[l-20:, :] | |
| X_train = train[:, :-1] | |
| y_train = train[:, -1] | |
| X_test = test[:, :-1] | |
| y_test = test[:, -1] | |
| slope = np.linalg.lstsq(X_train, y_train)[0] | |
| print "slope:\n", slope | |
| y_pred = np.dot(X_test, slope) | |
| plt.plot(y_test) | |
| plt.plot(y_pred) | |
| plt.show() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output
