serhii.net

In the middle of the desert you can say anything you want

25 May 2023

Pandas seaborn pretty correlation code

Old code I wrote for making ds.corr() more readable, looked for it three times already ergo its place is here.

Basically: removes all small correlations, and optionally plots a colorful heatmap of that.

2023-05-25-203455_432x335_scrot.png

def plot_corr(res:pd.DataFrame):
        import seaborn as sns
        sns.heatmap(res, annot=True,fmt=".1f",cmap="coolwarm")
    
def get_biggest_corr(ds_corr: pd.DataFrame, limit: float=0.8, remove_diagonal=True, remove_nans=True,plot=False) -> pd.DataFrame:
	import numpy as np  # just in case
    res =  ds_corr[(ds_corr>limit) | (ds_corr<-limit)]
    if remove_diagonal:
        np.fill_diagonal(res.values, np.nan)
    if remove_nans:
        res = res.dropna(how='all', axis=0)
        res = res.dropna(how='all', axis=1)
    if plot:
        plot_corr(res)
    else:
        return res
Nel mezzo del deserto posso dire tutto quello che voglio.