-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock_price_predictor.py
81 lines (61 loc) · 2.48 KB
/
stock_price_predictor.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
Stock price predictor
Script built upon tutorial at
https://medium.com/towards-data-science/predicting-stock-prices-in-50-lines-of-python-c2c56a84b03d
'''
from sklearn.svm import SVR
import csv
import matplotlib.pyplot as plt
import numpy as np
def extract_date_and_price_from_row(row):
date = int(row[0].split('-')[0])
price = float(row[1])
return date, price
def extract_dates_and_prices_from_stock_file(stock_file_name):
dates = []
prices = []
with open(stock_file_name, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
date, price = extract_date_and_price_from_row(row)
dates.append(date)
prices.append(price)
return dates, prices
def build_prediction_technique_on_data(dates,prices,prediction_technique):
dates = np.reshape(dates, (len(dates), 1))
prediction_technique.fit(dates, prices)
return prediction_technique
def build_plot_structure(dates,prices):
plt.scatter(dates,prices,color="black",label="Data")
plt.xlabel('Dates')
plt.ylabel('Price')
plt.title('Support Vector Reg')
plt.legend()
def plot_predictions_for(prediction_technique,color,label):
def plot_predictions(dates, prices, x):
plt.plot(dates,
prediction_technique.predict(dates),
color=color,
label=label)
return prediction_technique.predict(x)[0]
return plot_predictions
def display_plot():
plt.show()
def svr_lin(): return SVR(kernel='linear', C=1e3)
def svr_poly(): return SVR(kernel='poly', C=1e3, degree=2)
def svr_rbf(): return SVR(kernel='rbf',C=1e3, gamma=0.1)
if __name__ == '__main__':
dataset = 'bbas3.csv'
stock_data_dates, stock_data_prices = extract_dates_and_prices_from_stock_file(dataset)
s_lin = build_prediction_technique_on_data(stock_data_dates,stock_data_prices,svr_lin())
s_poly = build_prediction_technique_on_data(stock_data_dates,stock_data_prices,svr_poly())
s_rbf = build_prediction_technique_on_data(stock_data_dates,stock_data_prices,svr_rbf())
build_plot_structure(stock_data_dates,stock_data_prices)
plotters = []
plotters.append(plot_predictions_for(s_lin,"green","Linear Model"))
plotters.append(plot_predictions_for(s_poly,"blue","Polynomial Model"))
plotters.append(lot_predictions_for(s_rbf,"red","RBF Model"))
for plotter in plotters:
print(plotter(stock_data_dates,stock_data_prices,29))
display_plot()