Skip to content

Instantly share code, notes, and snippets.

@AdamSpannbauer
Last active September 1, 2020 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamSpannbauer/c99c366b0c7d5b6c4920a46c32d738e5 to your computer and use it in GitHub Desktop.
Save AdamSpannbauer/c99c366b0c7d5b6c4920a46c32d738e5 to your computer and use it in GitHub Desktop.
VIF utility function for checking multicollinearity with stats models
import warnings
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import variance_inflation_factor
def print_vif(x):
"""Utility for checking multicollinearity assumption
:param x: input features to check using VIF. This is assumed to be a pandas.DataFrame
:return: nothing is returned the VIFs are printed as a pandas series
"""
# Silence numpy FutureWarning about .ptp
with warnings.catch_warnings():
warnings.simplefilter("ignore")
x = sm.add_constant(x)
vifs = []
for i in range(x.shape[1]):
vif = variance_inflation_factor(x.values, i)
vifs.append(vif)
print('VIF results\n-------------------------------')
print(pd.Series(vifs, index=x.columns))
print('-------------------------------\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment