Advertisement
elena1234

correlation coefficiant and p in Python

May 12th, 2022 (edited)
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. from scipy import stats
  6.  
  7.  
  8. df_no_nulls_diesel = df_diesel[["Years Automobile", "Price BGN", "Kilometers", "Horsepower"]].dropna().copy()
  9. df_no_nulls_diesel.corr()
  10.  
  11. g = sns.lmplot(x = "Years Automobile", y = "Price BGN", data = df_no_nulls_diesel, height=12, aspect=1)
  12. g.fig.suptitle('Diesel engines: Years Automobile vs Price BGN')
  13. plt.show()
  14.  
  15. g = sns.heatmap(np.round(df_no_nulls_diesel.corr(), 2), annot = True).set(title='Diesel engines: Correlation Analysis')
  16. g
  17. plt.show()
  18.  
  19. df_no_nulls_diesel.mpg.corr(df_no_nulls_diesel.years)
  20.  
  21.  
  22. ########################################################
  23. # jointplot returns a jointgrid, which we need to assign to a variable in order to add an annotation
  24. # This line is almost like the original, but it seems that fill is needed explicitly now.
  25. # And most importantly, ".annotate" is not just deprecated. It's gone.
  26. jg = sns.jointplot(x='BMXLEG', y='BMXARML', data=da, kind='kde', fill=True)
  27.  
  28. # To get the correlation, we need to consider only the records with NA values for either measurement.
  29. da_no_nulls = da[['BMXLEG', 'BMXARML']].dropna()
  30. pearsonr, p = stats.pearsonr(da_no_nulls.BMXLEG, da_no_nulls.BMXARML)
  31. pearson_str = f'pearsonr = {pearsonr:.2f}; p = {p}'
  32.  
  33. # Placing the annotation somewhere readable requires that we find the max of the axes
  34. jg.ax_joint.text(
  35.     jg.ax_joint._axes.xaxis.get_data_interval()[1],
  36.     jg.ax_joint._axes.yaxis.get_data_interval()[1],
  37.     pearson_str,
  38.     horizontalalignment='right')
  39. plt.show()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement