<jupyter_start><jupyter_text>S&P500 Stock Price PredictionIn this project we are going to predict S&P500 Stock price using linear regression.<jupyter_code>#Importing Libraries
import pandas as pd
from datetime import datetime

#Reading data
df = pd.read_csv("sphist.csv")
df
#Converting date to datetime type
df['Date'] = pd.to_datetime(df['Date'])
df.info()
df['Date'] > datetime(year=2015, month=4, day=1)
#Sorting Values and resetting index
df.sort_values('Date', inplace = True)
df.reset_index(drop = True, inplace = True)
df.head()
#Calculatin rolling average for different time periods
df['5d_avg'] = df['Close'].rolling(window = 5).mean().shift(1)
df['30d_avg'] = df['Close'].rolling(window = 30).mean().shift(1)
df['365d_avg'] = df['Close'].rolling(window = 365).mean().shift(1)
df.head()
#Calculating rolling standard deviation for different time period
df['5d_std'] =  df['Close'].rolling(window = 5).std().shift(1)
df['365d_std'] = df['Close'].rolling(window = 5).std().shift(1)

df['5d_365d_avg_rat'] = df['5d_avg'] / df['365d_avg']
df['5d_365d_std_rat'] = df['5d_std'] / df['365d_std']
df.head()
df.dropna(inplace = True)
df.reset_index(drop = True, inplace = True)
train = df[df['Date'] < datetime(year =2013, month = 1, day = 1)]
test = df[df['Date'] >= datetime(year =2013, month = 1, day = 1)]
#Predicting close price using linear regression
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import numpy as np

lr = LinearRegression()
lr.fit(train[['5d_avg','30d_avg', '365d_avg', '5d_std', '5d_365d_avg_rat', '365d_std','5d_365d_std_rat']], train['Close'])
predict = lr.predict(test[['5d_avg','30d_avg', '365d_avg', '5d_std', '5d_365d_avg_rat', '365d_std','5d_365d_std_rat']])
#Calculating mse, mae and rmse
mse = mean_squared_error(predict, test['Close'])
mae = mean_absolute_error(predict, test['Close'])
rmse = np.sqrt(mse)
mse,rmse,mae<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1664145234493192, 'fraction_numerical': 0.049924357034795766, 'mean_word_length': 7.232365145228216, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '121336', 'license': 'MIT', 'n_tokens_mistral': 800, 'n_tokens_neox': 728, 'n_words': 166, 'path': 'stock_price_prediction.ipynb', 'repo_name': 'pepelawycliffe/stock_price_prediction', 'size': '1983'}