<jupyter_start><jupyter_text>Preprocessing<jupyter_code># import relevant statistical packages
import numpy as np
import pandas as pd
# import relevant data visualisation packages
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# load Default dataset
url = "/Users/arpanganguli/Documents/Professional/Finance/ISLR/Datasets/Weekly.csv"
Weekly = pd.read_csv(url, index_col = 'Unnamed: 0')
Weekly.head()
Weekly.info()
dfX = Weekly.drop(columns='Direction')
dfy = Weekly['Direction']
dfX.head()
dfy.head()<jupyter_output><empty_output><jupyter_text>7.a. Logistic regression model predicting Direction using Lag1 and Lag2<jupyter_code>from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
X = dfX[['Lag1', 'Lag2']]
y = dfy
glmfit = LogisticRegression(solver='liblinear').fit(X, y)
glmpred = glmfit.predict(X)
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y, glmpred)
conf_mat
round((conf_mat[0][1] + conf_mat[1][0]) / y.shape[0], 4)<jupyter_output><empty_output><jupyter_text>7.b. Logistic regression model predicting Direction using Lag1 and Lag2 *using all but the first observation*<jupyter_code>X_train = dfX[['Lag1', 'Lag2']].iloc[1:]
y_train = dfy.iloc[1:]
X_test = np.asarray(dfX[['Lag1', 'Lag2']].iloc[0]).reshape(-1,2)
y_test = np.asarray(dfy.iloc[0])
y_test
glmfit = LogisticRegression(solver='liblinear').fit(X_train, y_train)
glmpred = glmfit.predict(X_test)
glmpred<jupyter_output><empty_output><jupyter_text>7.c. No, prediction was 'Up' and the true Direction was 'Down'  7.d. Recurring logistic regression using for-loop<jupyter_code>n = len(dfX)
n
glmpred_df = pd.DataFrame()
X = dfX[['Lag1', 'Lag2']]
y = dfy
for j in range(1, n):
    X_train, y_train, X_test, y_test = X.drop([j]), y.drop([j]), np.asarray(X.iloc[j]).reshape(-1,2), np.asarray(y.iloc[j], dtype=object)
    glmfit = LogisticRegression(solver='liblinear').fit(X_train, y_train)
    glmpred = glmfit.predict(X_test)
    if glmpred == y_test:
        glmpred_df = glmpred_df.append([0])
    else:
        glmpred_df = glmpred_df.append([1])
glmpred_df.reset_index(drop=True,inplace=True)
glmpred_df.columns = ['Error']
glmpred_df.head()
glmpred_df.shape
LOOCV_estimate = glmpred_df.mean()
LOOCV_estimate<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13991228070175438, 'fraction_numerical': 0.014912280701754385, 'mean_word_length': 6.6033333333333335, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '131201', 'license': 'MIT', 'n_tokens_mistral': 916, 'n_tokens_neox': 890, 'n_words': 172, 'path': 'Chapter 5/Applied Exercises/7.ipynb', 'repo_name': 'arpanganguli/ISLR-Python', 'size': '2280'}