-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictive_analytics.py
More file actions
172 lines (150 loc) · 8.42 KB
/
predictive_analytics.py
File metadata and controls
172 lines (150 loc) · 8.42 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
###############################################################################
# Importing Libraries #
# import the necessary packages by using command: pip install <package_name> #
###############################################################################
# For data processing
import pandas as pd
import numpy as np
# For date and time
import datetime as dt
import dateutil
# For Linear Regression & Linear-SVR
from sklearn.linear_model import LinearRegression
from sklearn import linear_model
from sklearn import metrics
from sklearn.svm import LinearSVR
# For graphs
import matplotlib.pyplot as plt
from matplotlib import pylab
###############################################################################
# Predictive Analytics Menu #
###############################################################################
def predictive_menu(stock_all_data,stock_closing_price):
print("-" * 100)
predictive_menu = ("\nLinear Models for Predictive Analysis:\n1. Linear Regression Model\n2. Linear SVR Model\n3. Go back to Previous Menu\n")
print(predictive_menu)
print("-" * 100)
predictive_choice = input("Please enter your choice: ")
while predictive_choice != "3":
try:
if predictive_choice == "1":
linear_regression(stock_all_data,stock_closing_price)
elif predictive_choice == "2":
SVR_model(stock_all_data,stock_closing_price)
else:
print("\n" , "*" * 10 , "Input Error: Please enter a valid option." , "*" * 10, "\n")
except ValueError:
print("\n" , "*" * 10 , "Input Error: Please enter a valid option." , "*" * 10, "\n")
except Exception as e:
print("\n" , "*" * 10 , "Sorry an error occurred. Please try again with valid input." , "*" * 10, "\n")
print(e)
print("-" * 100)
print(predictive_menu)
print("-" * 100)
predictive_choice = input("Please enter your choice: ")
###############################################################################
# Stock Price Prediction using Linear Regression #
###############################################################################
def linear_regression(stock_all_data_lr,stock_closing_price):
stock_all_data = stock_all_data_lr.copy()
start_day = pd.to_datetime(np.min(stock_all_data.index))
stock_all_data['index_days'] = (pd.to_datetime(stock_all_data.index) - start_day).days
x_dates = stock_all_data['index_days'].tolist()
y_prices = stock_all_data['Close'].tolist()
linear_mod = linear_model.LinearRegression()
x_dates = np.reshape(x_dates,(len(x_dates),1))
y_prices = np.reshape(y_prices,(len(y_prices),1))
linear_mod.fit(x_dates,y_prices)
predicted_price1 = linear_mod.predict(x_dates)
print("-" * 100)
linear_menu = "\nLinear Regression Model:\n1. Enter date for prediction\n2. Go back to Previous Menu\n"
print(linear_menu)
print("-" * 100)
choice_linear = input("Enter your choice: ")
while choice_linear != "2":
try:
if choice_linear == "1":
predicted_date = input("Enter the date for prediction in YYYY-MM-DD format: ")
if(dt.datetime.strptime(predicted_date, '%Y-%m-%d')):
print("-" * 100)
print("Details on the Prediction and Error:")
print("-" * 100)
print("Co-efficient of Accuracy: ", linear_mod.score(x_dates,y_prices))
print("\nRoot Mean Squared Error: ",np.sqrt(metrics.mean_squared_error(y_prices,predicted_price1)))
print("\nR2 Score: ", (metrics.r2_score(y_prices,predicted_price1)))
print("\nMean Absolute Error: ", metrics.mean_absolute_error(y_prices,predicted_price1))
print("\nMean Squared Error: ", metrics.mean_squared_error(y_prices,predicted_price1))
predict_diff = (pd.to_datetime(predicted_date) - start_day).days
temp= np.reshape(predict_diff,(1,-1))
predicted_price2 = linear_mod.predict(temp)
print('\nSlope: ', np.asscalar(np.squeeze(linear_mod.coef_)))
print('\nIntercept: ', linear_mod.intercept_[0])
print(f"\nPredicted price for {predicted_date} date is :{predicted_price2[0][0]}")
print("-"* 100)
plt.figure(1, figsize=(15,10))
plt.title('Linear Regression | Closing Price vs Time', fontdict = {'fontsize' : 15})
plt.scatter(x_dates, y_prices, edgecolor='w', label='Actual Price')
plt.plot(x_dates, linear_mod.predict(x_dates), color='r', label='Predicted Price')
plt.xlabel('Integer Date', fontsize = 12)
plt.ylabel('Stock Price', fontsize = 12)
plt.legend()
plt.show()
else:
print("Please enter the date in YYYY-MM-DD format.")
else:
print("\n" , "*" * 10 , "Input Error: Please enter a valid option." , "*" * 10, "\n")
except ValueError:
print("\n" , "*" * 10 , "Input Error: Please enter a valid option." , "*" * 10, "\n")
except:
print("\n" , "*" * 10 , "Sorry an error occurred. Please try again with valid input." , "*" * 10, "\n")
print("-"* 100)
print(linear_menu)
print("-"* 100)
choice_linear = input("Enter your choice: ")
###############################################################################
# Stock Price Prediction using SVR Model #
###############################################################################
def SVR_model(stock_all_data_svr,stock_closing_price):
stock_all_data = stock_all_data_svr.copy()
x_dates = []
y_prices = []
start_day = pd.to_datetime(np.min(stock_all_data.index))
stock_all_data['index_days'] = (pd.to_datetime(stock_all_data.index) - start_day).days
x_dates = stock_all_data['index_days'].tolist()
y_prices = stock_all_data['Close'].tolist()
x_dates = np.reshape(x_dates,(len(x_dates),1))
y_prices = np.reshape(y_prices,(len(y_prices),1))
svr_lin = LinearSVR(C=1e3)
svr_lin.fit(x_dates, y_prices.ravel())
print("-" * 100)
svr_menu = "\nLinear SVR Model:\n1. Enter date for prediction\n2. Go back to Previous Menu\n"
print(svr_menu)
print("-" * 100)
choice_SVR = input("Enter your choice: ")
while choice_SVR != "2":
try:
if choice_SVR == "1":
predicted_date_SVR = input("Enter the date for prediction in YYYY-MM-DD format: ")
if(dt.datetime.strptime(predicted_date_SVR, '%Y-%m-%d')):
plt.scatter(x_dates, y_prices, color = 'black', label = 'Data')
plt.plot(x_dates, svr_lin.predict(x_dates), color = 'red', label = 'Linear Model')
plt.xlabel('Date')
plt.ylabel("Price")
plt.title("Support Vector Regression (SVR)")
plt.legend()
plt.show()
predict_diff_svr = (pd.to_datetime(predicted_date_SVR) - start_day).days
print("-" * 100)
print(F"The Stock Close Price on {predicted_date_SVR}: ")
print("Linear Kernel: $", svr_lin.predict(np.array(predict_diff_svr).reshape(-1,1))[0])
else:
print("\n" , "*" * 10 , "Input Error: Please enter a valid option." , "*" * 10, "\n")
else:
print("\n" , "*" * 10 , "Input Error: Please enter a valid option." , "*" * 10, "\n")
except Exception as e:
print("\n" , "*" * 10 , "Sorry an error occurred. Please try again with valid input." , "*" * 10, "\n")
print(e.args[0])
print("-" * 100)
print(svr_menu)
print("-" * 100)
choice_SVR = input("Enter your choice: ")