Skip to content

Instantly share code, notes, and snippets.

@mitallast
Created March 8, 2016 13:25
Show Gist options
  • Select an option

  • Save mitallast/22bbcabfae48e16cc7a8 to your computer and use it in GitHub Desktop.

Select an option

Save mitallast/22bbcabfae48e16cc7a8 to your computer and use it in GitHub Desktop.
Predict next day open price for USD by previous 5 days
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()
@mitallast
Copy link
Copy Markdown
Author

Example output
2016-03-08 16 25 27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment