-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_regression_analysis.py
More file actions
50 lines (38 loc) · 1.37 KB
/
Copy pathstock_regression_analysis.py
File metadata and controls
50 lines (38 loc) · 1.37 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
# Import dependencies
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import seaborn as sns
import datetime as dt
import yfinance as yf
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Set up stock data
stock = 'AAPL'
start_date = dt.date.today() - dt.timedelta(days=365 * 10)
end_date = dt.date.today()
# Download and load data
data = yf.download(stock, start_date, end_date)
# Drop unused columns
data = data.drop(columns=['Adj Close'])
# Split data into X and y
X = data.drop(['Close'], axis=1)
y = data['Adj Close']
# Split X and y into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
# Create a linear regression model
regression_model = LinearRegression()
# Train the model
regression_model.fit(X_train, y_train)
# Get the intercept for the model
intercept = regression_model.intercept_
print("The intercept for our model is: {}".format(intercept))
# Test the model
score = regression_model.score(X_test, y_test)
print("The score for our model is: {}".format(score))
# Predict the next day's price
latest_data = data.tail(1).values.tolist()
next_day_price = regression_model.predict(latest_data)[0]
print("The predicted price for the next trading day is: {}".format(next_day_price))