text_plain_output_1.png
from sklearn.datasets import load_boston

from sklearn.linear_model import Ridge, RidgeCV

from sklearn.preprocessing import StandardScaler

from statsmodels.stats.outliers_influence import variance_inflation_factor

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

from sklearn.datasets import load_boston
data = load_boston()

df = pd.DataFrame(data.data, columns=data.feature_names)

df['MEDV'] = data.target

X = df.drop(columns='MEDV', axis=1)
y = df['MEDV']

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

from statsmodels.stats.outliers_influence import variance_inflation_factor
vif = pd.DataFrame()
vif['VIF'] = [variance_inflation_factor(X_scaled, i) for i in range(X_scaled.shape[1])]
vif['Features'] = X.columns
vif

X.drop(columns=['TAX'], axis=1)

from sklearn.linear_model import Ridge, RidgeCV
alphas = np.random.uniform(0, 10, 50)
ridge_cv = RidgeCV(alphas=alphas, cv=10, normalize=True)
ridge_cv.fit(X_train, y_train)

alpha = ridge_cv.alpha_
alpha

ridge = Ridge(alpha=ridge_cv.alpha_)
ridge.fit(X_train, y_train)

ridge.score(X_train, y_train)

ridge.score(X_test, y_test)

def adj_r2(X, y, model):
    r2 = model.score(X, y)
    n = X.shape[0]
    p = X.shape[1]
    adjusted_r2 = 1 - (1 - r2) * (n - 1) / (n - p - 1)
    return adjusted_r2

print(adj_r2(X_train, y_train, ridge))