forked from amisha-w/StockOracle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LR.py
113 lines (89 loc) · 3.69 KB
/
LR.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
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
import numpy as np
import numpy as np
from datetime import datetime
import smtplib
import time
import os
from selenium import webdriver
#For Prediction
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing,svm
from sklearn.model_selection import train_test_split
#For Stock Data
from iexfinance.stocks import Stock
from iexfinance.stocks import get_historical_data
import pandas_datareader
import pandas as pd
from pandas_datareader import data
from sklearn.svm import SVR
from yahoo_fin import stock_info as si
import json
'''def getStocks(n):
predictData('CLF',2)'''
def predictData(stock,days):
start = datetime(2016, 1, 1)
end = datetime.now()
#Outputting the Historical data into a .csv for later use
df = data.get_data_yahoo(stock, start, end)
#print(df.head())
# Get the Adjusted Close Price
df = df[['Adj Close']]
# Take a look at the new data
#print(df.head())
# A variable for predicting 'n' days out into the future
forecast_out = int(days) #'n=30' days
#Create another column (the target ) shifted 'n' units up
df['Prediction'] = df[['Adj Close']].shift(-forecast_out)
#print the new data set
#print(df.tail())
### Create the independent data set (X) #######
# Convert the dataframe to a numpy array
X = np.array(df.drop(['Prediction'],1))
#Remove the last '30' rows
X = X[:-forecast_out]
#print(X)
### Create the dependent data set (y) #####
# Convert the dataframe to a numpy array
y = np.array(df['Prediction'])
# Get all of the y values except the last '30' rows
y = y[:-forecast_out]
#print(y)
# Split the data into 80% training and 20% testing
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Create and train the Support Vector Machine (Regressor)
# svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
# svr_rbf.fit(x_train, y_train)
# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
# svr_rbf_confidence = svr_rbf.score(x_test, y_test)
# print("svm confidence: ", svr_rbf_confidence)
# Testing Model: Score returns the coefficient of determination R^2 of the prediction.
# The best possible score is 1.0
# Create and train the Linear Regression Model
lr = LinearRegression()
# Train the model
lr.fit(x_train, y_train)
lr_confidence = lr.score(x_test, y_test)
print("lr confidence: ", lr_confidence)
# Set x_forecast equal to the last 30 rows of the original data set from Adj. Close column
x_forecast = np.array(df.drop(['Prediction'],1))[-forecast_out:]
#print(x_forecast)
# Print linear regression model predictions for the next '30' days
lr_prediction = lr.predict(x_forecast)
print(lr_prediction[0])
val=si.get_live_price(stock)
#print("Current Price of {} is {}".format(stock,val))
predictResponse = "The Current Price of {} is ${} <br> The predicted price for upcoming {} days is:".format(stock,val,days)
for elements in lr_prediction:
predictResponse += "<br> ${0:.3f}".format(elements)
lr_confidence = lr_confidence *100
#predictResponse += "<br>I'm {0:.3f}% sure about the prices".format(lr_confidence)
print(predictResponse)
#print("lr ans")
return (predictResponse)
# Print support vector regressor model predictions for the next '30' days
# svm_prediction = svr_rbf.predict(x_forecast)
# print("svm ans")
# print(svm_prediction)
predictData('CLF',5)
#getStocks(5)