Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
109 lines (88 loc) · 3.12 KB
/
Copy pathmodel.py
File metadata and controls
109 lines (88 loc) · 3.12 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
import datetime as dt
from datetime import date
import yfinance as yf
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import sklearn.metrics as sm
from sklearn import metrics
# model
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import BayesianRidge
from sklearn.neighbors import KNeighborsRegressor
from sklearn import preprocessing, svm
# load the data
df = yf.download("tsla", period='5y', interval="1d")
df.reset_index(inplace=True)
# getting only the adj close value for prediction
df=df[["Adj Close"]]
print(df.head())
# variable for predicting 'n' number of days in the future
forecast_out=10
# creating a new column to store the predicted values (dependent column)
df["Prediction"] = df[["Adj Close"]].shift(-forecast_out)
# creating the independent data set
x=np.array(df.drop(["Prediction"],1))
# removing the last 'n' rows from the independent column
x=x[:-forecast_out]
# creating the dependent data set
y=np.array(df[['Prediction']])
# removing the last 'n' rows from the independent column
y=y[:-forecast_out]
#splitting the data into training and testing data
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2) #20% testing
# Linear Regression model
clflr=LinearRegression()
clflr.fit(X_train, y_train)
# KNN Regression
clfknn = KNeighborsRegressor(n_neighbors=2)
clfknn.fit(X_train, y_train)
# Bayesian Regression
clfbay = BayesianRidge(compute_score=True)
clfbay.fit(X_train, y_train)
# SVM
clfsvm = svm.SVR(kernel='rbf')
clfsvm.fit(X_train, y_train)
# Lasso Regression
clflas = Lasso()
clflas.fit(X_train, y_train)
# create an array equal to the last 'n' number of days
x_forecast = np.array(df.drop(['Prediction'],1))[-forecast_out:]
model_pred = clfsvm.predict(x_forecast)
LR_pred = clflr.predict(X_test)
KNN_pred = clfknn.predict(X_test)
BAY_pred = clfbay.predict(X_test)
SVM_pred = clfsvm.predict(X_test)
LAS_pred = clflas.predict(X_test)
print("confidencereg: ", clflr.score(y_test, LR_pred))
print("confidencelas: ", clflas.score(y_test, LAS_pred))
print("confidencesvm: ", clfsvm.score(y_test, SVM_pred))
print("confidencebay: ", clfbay.score(y_test, BAY_pred))
print("confidenceknn: ", clfknn.score(y_test, KNN_pred))
print("Test LR MAE: ",metrics.mean_absolute_error(y_test,LR_pred))
print("Test LAS MAE: ",metrics.mean_absolute_error(y_test,LAS_pred))
print("Test SVM MAE: ",metrics.mean_absolute_error(y_test,SVM_pred))
print("Test BAY MAE: ",metrics.mean_absolute_error(y_test,BAY_pred))
print("Test KNN MAE: ",metrics.mean_absolute_error(y_test,KNN_pred))
# create a list with the future 'n' number of days
dates = []
current = dt.date.today()
for i in range(11):
current += dt.timedelta(days=1)
dates.append(current)
print(dates)
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=dates,
y=model_pred.flatten(),
mode='lines+markers',
name='data'))
fig.update_layout(
title="Predicted Close Price of next " + str(10) + " days",
xaxis_title="Date",
yaxis_title="Closed Price"
)