checking for multicollinearity

Solutions on MaxInterview for checking for multicollinearity by the best coders in the world

showing results for - "checking for multicollinearity"
Claudia
16 Aug 2020
1import numpy as np
2import pandas as pd
3import seaborn as sns
4import matplotlib.pyplot as plt
5
6sns.set_theme(style="white")
7
8# Reading a CSV into a datafame:
9
10df = pd.read_csv("csv_filepath)
11
12# Compute the correlation matrix
13corr = df.corr()
14
15# Generate a mask for the upper triangle
16mask = np.triu(np.ones_like(corr, dtype=bool))
17
18# Set up the matplotlib figure
19f, ax = plt.subplots(figsize=(11, 9))
20
21# Generate a custom diverging colormap
22cmap = sns.diverging_palette(230, 20, as_cmap=True)
23
24# Draw the heatmap with the mask and correct aspect ratio
25sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
26            square=True, linewidths=.5, cbar_kws={"shrink": .5})