-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict.py
More file actions
67 lines (58 loc) · 2.1 KB
/
predict.py
File metadata and controls
67 lines (58 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import csv
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
import pandas as pd
nlines=25
ndays=nlines/5*7
stockname='AAPL'
dates = []
prices = []
today=pd.Timestamp.today()
def get_data(filename):
with open(filename, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
dates.append(row[0])
prices.append(float(row[1]))
return
def show_plot(dates, prices):
linear_mod1 = linear_model.LinearRegression()
linear_mod2 = linear_model.Ridge(alpha=2000)
linear_mod3 = linear_model.Lasso(alpha=20)
dates = np.reshape(dates, (len(dates),1))
prices = np.reshape(prices, (len(prices),1))
linear_mod1.fit(dates,prices)
linear_mod2.fit(dates,prices)
linear_mod3.fit(dates,prices)
plt.scatter(dates,prices,color='black')
plt.plot(dates,linear_mod1.predict(dates), color='red', linewidth=1, label="Linear")
plt.plot(dates,linear_mod2.predict(dates), color='green', linewidth=1, label="Ridge")
plt.plot(dates,linear_mod3.predict(dates), color='blue', linewidth=1, label="Lasso")
plt.legend(loc='upper left', frameon=False)
plt.xlabel('Day')
plt.ylabel('Stock Price')
plt.title('Linear stock prediction '+stockname)
plt.show()
def predict_price(dates, prices, x):
linear_mod = linear_model.LinearRegression()
dates = np.reshape(dates, (len(dates),1))
prices = np.reshape(prices, (len(prices),1))
linear_mod.fit(dates,prices)
predicted_price = linear_mod.predict(x)
return predicted_price[0][0], linear_mod.coef_[0][0], linear_mod.intercept_[0]
get_data(stockname+'.csv')
dates = dates[-nlines:]
prices = prices[-nlines:]
pdates=({})
pdates['daystring'] = dates
pdates['day'] = ndays - (today-pd.to_datetime(dates)).days
#print(dates)
#print(prices)
show_plot(pdates['day'],prices)
predicted_price, coefficient, constant = predict_price(pdates['day'],prices,[[ndays+1]])
print("Linear Regression model")
print("Day",ndays+1)
print("Predicted Price:",predicted_price)
print("ax+b, a =",coefficient," b =",constant)