<jupyter_start><jupyter_text>Imports and loading dataLoading the boston dataset directly from its "[source](http://lib.stat.cmu.edu/datasets/boston)".<jupyter_code>from sklearn.model_selection import train_test_split

import pandas as pd
import numpy as np

import statsmodels.api as sm
from statsmodels.stats.outliers_influence import variance_inflation_factor
import load
data = load.boston()
X_train, X_test, y_train, y_test = train_test_split(data.drop('PRICE', axis=1), np.log(data['PRICE']), test_size=0.2, random_state=10)
x_incl_const = sm.add_constant(X_train)

model = sm.OLS(y_train, x_incl_const)
results = model.fit()<jupyter_output><empty_output><jupyter_text>Testing for Multicollinearity $$ TAX = \alpha_0 + \alpha_1RM + \alpha_2NOX+...+\alpha_{12}LSTAT $$$$ VIF_{TAX}=\frac{1}{(1-R^2_{TAX})} $$<jupyter_code>variance_inflation_factor(exog=np.asarray(x_incl_const), exog_idx=1)
variance_inflation_factor(exog=x_incl_const.values, exog_idx=1)
# Both ways above return the same array:
np.array_equal(x_incl_const.values, np.asarray(x_incl_const)), np.all(x_incl_const.values == np.asarray(x_incl_const))
x_incl_const.shape[1], len(x_incl_const.columns) # Two ways to get the amount of columns
vif = [variance_inflation_factor(exog=x_incl_const.values, exog_idx=idx) for idx in range(x_incl_const.shape[1])]
pd.DataFrame(
    {
        'coef_name': x_incl_const.columns,
        'VIF': np.around(vif,2)
    }
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13007570543702685, 'fraction_numerical': 0.012388162422573986, 'mean_word_length': 7.033149171270718, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '550620', 'license': 'MIT', 'n_tokens_mistral': 562, 'n_tokens_neox': 568, 'n_words': 95, 'path': 'Section_05/01_Boston/09_Boston-Multicollinearity.ipynb', 'repo_name': 'ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp', 'size': '1453'}