Python - Stock Prediction

Mar 26, 2022
   44

Model can be exported with 'joblib'


Collab Link: https://colab.research.google.com/drive/1wSb6UInI5dMGJ1u-c8arJYbVCErCuM5o?usp=sharing


Dependencies


  1. pip install quandl



import quandl
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split

# Get Data from Quandl
df = quandl.get("BITFINEX/BTCUSD", authtoken="-aBPy-8nsYvmnQ3sasXS")

# Isolate High and Low Columns
dfh = df[['High']]
dfl = df[['Low']]

# Predicting into the Future
forecast_out = 30 #'n=30' days

# Create more Columns (the target or dependent variable) shifted 'n' units up
dfh['Prediction'] = df[['High']].shift(-forecast_out)
dfl['Prediction'] = df[['Low']].shift(-forecast_out)

# Independent Data Set
X_h = np.array(dfh.drop(['Prediction'],1))
X_l = np.array(dfl.drop(['Prediction'],1))

# Remove the last 'n' rows
X_h = X_h[:-forecast_out]
X_l = X_l[:-forecast_out]

# Dependent Data Set
y_h = np.array(dfh['Prediction'])
y_l = np.array(dfl['Prediction'])

# Remove the last 'n' rows
y_h = y_h[:-forecast_out]
y_l = y_l[:-forecast_out]

# Split and Train
x_train_h, x_test_h, y_train_h, y_test_h = train_test_split(X_h, y_h, test_size=0.1)
x_train_l, x_test_l, y_train_l, y_test_l = train_test_split(X_l, y_l, test_size=0.1)

# LinearRegression (lr)
lr_h = LinearRegression()
lr_l = LinearRegression()
# Train the model
lr_h.fit(x_train_h, y_train_h)
lr_l.fit(x_train_l, y_train_l)

lr_confidence_h = lr_h.score(x_test_h, y_test_h)
print("lr confidence HIGH: ", lr_confidence_h)


lr_confidence_l = lr_l.score(x_test_l, y_test_l)
print("lr confidence LOW: ", lr_confidence_l)