Skip to content

Instantly share code, notes, and snippets.

@Alakhator
Created Mar 4, 2020
Embed
What would you like to do?
# Function to calculate VIF
def calculate_vif(data):
vif_df = pd.DataFrame(columns = ['Var', 'Vif'])
x_var_names = data.columns
for i in range(0, x_var_names.shape[0]):
y = data[x_var_names[i]]
x = data[x_var_names.drop([x_var_names[i]])]
r_squared = sm.OLS(y,x).fit().rsquared
vif = round(1/(1-r_squared),2)
vif_df.loc[i] = [x_var_names[i], vif]
return vif_df.sort_values(by = 'Vif', axis = 0, ascending=False, inplace=False)
X=df.drop(['Salary'],axis=1)
calculate_vif(X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment