# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Simple Linear Regression for stock using scikit-learn
#

# + outputHidden=false inputHidden=false
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import seaborn as sns
# %matplotlib inline

import warnings
warnings.filterwarnings("ignore")

import yfinance as yf
yf.pdr_override()

# + outputHidden=false inputHidden=false
stock = 'AAPL'
start = '2016-01-01' 
end = '2018-01-01'
data = yf.download(stock, start, end)
data.head()

# + outputHidden=false inputHidden=false
df = data.reset_index()
df.head()

# + outputHidden=false inputHidden=false
X = df.drop(['Date','Close'], axis=1, inplace=True)
y = df[['Adj Close']]

# + outputHidden=false inputHidden=false
df = df.as_matrix()

# + outputHidden=false inputHidden=false
from sklearn.model_selection import train_test_split

# Split X and y into X_
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.25,  random_state=0)

# + outputHidden=false inputHidden=false
from sklearn.linear_model import LinearRegression

regression_model = LinearRegression()
regression_model.fit(X_train, y_train)

# + outputHidden=false inputHidden=false
intercept = regression_model.intercept_[0]

print("The intercept for our model is {}".format(intercept))

# + outputHidden=false inputHidden=false
regression_model.score(X_test, y_test)

# + outputHidden=false inputHidden=false
from sklearn.metrics import mean_squared_error

y_predict = regression_model.predict(X_test)

regression_model_mse = mean_squared_error(y_predict, y_test)

regression_model_mse

# + outputHidden=false inputHidden=false
math.sqrt(regression_model_mse)

# + outputHidden=false inputHidden=false
# input the latest Open, High, Low, Close, Volume
# predicts the next day price
regression_model.predict([[167.81, 171.75, 165.19, 166.48, 37232900]])

StarcoderdataJupyterScriptsDedupFiltered