In [ ]:
#todays task
#1. Visualize correlation coefficient via Heat Map
# ** Summarize the linear relationship between numeric variables
In [ ]:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
df = pd.read_csv('winequality.csv')
df.head()
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | quality | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 | 5 |
| 1 | 7.8 | 0.88 | 0.00 | 2.6 | 0.098 | 25.0 | 67.0 | 0.9968 | 3.20 | 0.68 | 9.8 | 5 |
| 2 | 7.8 | 0.76 | 0.04 | 2.3 | 0.092 | 15.0 | 54.0 | 0.9970 | 3.26 | 0.65 | 9.8 | 5 |
| 3 | 11.2 | 0.28 | 0.56 | 1.9 | 0.075 | 17.0 | 60.0 | 0.9980 | 3.16 | 0.58 | 9.8 | 6 |
| 4 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 | 5 |
In [ ]:
#extract all numeric variables in this dataset
new_df = df.iloc[:,0:11]
new_df.head()
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 |
| 1 | 7.8 | 0.88 | 0.00 | 2.6 | 0.098 | 25.0 | 67.0 | 0.9968 | 3.20 | 0.68 | 9.8 |
| 2 | 7.8 | 0.76 | 0.04 | 2.3 | 0.092 | 15.0 | 54.0 | 0.9970 | 3.26 | 0.65 | 9.8 |
| 3 | 11.2 | 0.28 | 0.56 | 1.9 | 0.075 | 17.0 | 60.0 | 0.9980 | 3.16 | 0.58 | 9.8 |
| 4 | 7.4 | 0.70 | 0.00 | 1.9 | 0.076 | 11.0 | 34.0 | 0.9978 | 3.51 | 0.56 | 9.4 |
In [ ]:
#obtain the pair-wise corelation coefficent
coco = new_df.corr()
coco
Out[ ]:
| fixed acidity | volatile acidity | citric acid | residual sugar | chlorides | free sulfur dioxide | total sulfur dioxide | density | pH | sulphates | alcohol | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| fixed acidity | 1.000000 | -0.256131 | 0.671703 | 0.114777 | 0.093705 | -0.153794 | -0.113181 | 0.668047 | -0.682978 | 0.183006 | -0.061668 |
| volatile acidity | -0.256131 | 1.000000 | -0.552496 | 0.001918 | 0.061298 | -0.010504 | 0.076470 | 0.022026 | 0.234937 | -0.260987 | -0.202288 |
| citric acid | 0.671703 | -0.552496 | 1.000000 | 0.143577 | 0.203823 | -0.060978 | 0.035533 | 0.364947 | -0.541904 | 0.312770 | 0.109903 |
| residual sugar | 0.114777 | 0.001918 | 0.143577 | 1.000000 | 0.055610 | 0.187049 | 0.203028 | 0.355283 | -0.085652 | 0.005527 | 0.042075 |
| chlorides | 0.093705 | 0.061298 | 0.203823 | 0.055610 | 1.000000 | 0.005562 | 0.047400 | 0.200632 | -0.265026 | 0.371260 | -0.221141 |
| free sulfur dioxide | -0.153794 | -0.010504 | -0.060978 | 0.187049 | 0.005562 | 1.000000 | 0.667666 | -0.021946 | 0.070377 | 0.051658 | -0.069408 |
| total sulfur dioxide | -0.113181 | 0.076470 | 0.035533 | 0.203028 | 0.047400 | 0.667666 | 1.000000 | 0.071269 | -0.066495 | 0.042947 | -0.205654 |
| density | 0.668047 | 0.022026 | 0.364947 | 0.355283 | 0.200632 | -0.021946 | 0.071269 | 1.000000 | -0.341699 | 0.148506 | -0.496180 |
| pH | -0.682978 | 0.234937 | -0.541904 | -0.085652 | -0.265026 | 0.070377 | -0.066495 | -0.341699 | 1.000000 | -0.196648 | 0.205633 |
| sulphates | 0.183006 | -0.260987 | 0.312770 | 0.005527 | 0.371260 | 0.051658 | 0.042947 | 0.148506 | -0.196648 | 1.000000 | 0.093595 |
| alcohol | -0.061668 | -0.202288 | 0.109903 | 0.042075 | -0.221141 | -0.069408 | -0.205654 | -0.496180 | 0.205633 | 0.093595 | 1.000000 |
In [ ]:
#heatmap
plt.figure(figsize=(7,7))
sns.heatmap(coco, cmap='RdYlGn_r', annot=True, vmin=-1, vmax= 1,
square=True, linewidth=0.5) #_r in the color reverses it
plt.show()
In [ ]:
tri_matrix = np.triu(coco)
tri_matrix
Out[ ]:
array([[ 1. , -0.25613089, 0.67170343, 0.11477672, 0.09370519,
-0.15379419, -0.11318144, 0.66804729, -0.68297819, 0.18300566,
-0.06166827],
[ 0. , 1. , -0.55249568, 0.00191788, 0.06129777,
-0.01050383, 0.07647 , 0.02202623, 0.23493729, -0.26098669,
-0.20228803],
[ 0. , 0. , 1. , 0.14357716, 0.20382291,
-0.06097813, 0.03553302, 0.36494718, -0.54190414, 0.31277004,
0.10990325],
[ 0. , 0. , 0. , 1. , 0.05560954,
0.187049 , 0.20302788, 0.35528337, -0.08565242, 0.00552712,
0.04207544],
[ 0. , 0. , 0. , 0. , 1. ,
0.00556215, 0.04740047, 0.20063233, -0.26502613, 0.37126048,
-0.22114054],
[ 0. , 0. , 0. , 0. , 0. ,
1. , 0.66766645, -0.02194583, 0.0703775 , 0.05165757,
-0.06940835],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 1. , 0.07126948, -0.06649456, 0.04294684,
-0.20565394],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 1. , -0.34169933, 0.14850641,
-0.49617977],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. , -0.1966476 ,
0.20563251],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 1. ,
0.09359475],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
1. ]])
In [ ]:
#refine the existing heatmap -> only need the lower half of the matrix
plt.figure(figsize=(7,7))
sns.heatmap(coco, cmap='RdYlGn_r', annot=True, vmin=-1, vmax= 1,
square=True, linewidth=0.5, mask=tri_matrix,
fmt='.1f') #_r in the color reverses it
plt.show()
In [ ]: