Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

a function to create a null correlation heatmap in python

def plot_null_correlations(df):    
  # create a correlation matrix only for columns with at least    
  # one missing value    
	cols_with_missing_vals = df.columns[df.isnull().sum() > 0]    
  	missing_corr = df[cols_with_missing_vals].isnull().corr()    
  # create a triangular mask to avoid repeated values and make    
  # the plot easier to read    
  	missing_corr = missing_corr.iloc[1:, :-1]    
  	mask = np.triu(np.ones_like(missing_corr), k=1)    
  # plot a heatmap of the values    
  	plt.figure(figsize=(20,12))    
  	ax = sns.heatmap(missing_corr, vmin=-1, vmax=1, cmap='RdBu', mask=mask, annot=True)    
  # round the labels and hide labels for values near zero    
  	for text in ax.texts:        
    	t = float(text.get_text())        
    	if -0.05 < t < 0.01:            
      		text.set_text('')        
		else:            
      		text.set_text(round(t, 2))    
	plt.show()
 
PREVIOUS NEXT
Tagged: #function #create #null #correlation #heatmap #python
ADD COMMENT
Topic
Name
2+6 =