Seaborn Heatmap
Seabon Heatmap
Correlation
- Feature간의 상관관계를 확인할 때 corr()함수를 사용한다.Iris Dataset을 불러온 뒤 상관관계를 확인해 보자.
import pandas as pd
import numpy as np
import seaborn as sns
import pydataset
iris = pydataset.data('iris')
iris.corr()
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | |
---|---|---|---|---|
Sepal.Length | 1.000000 | -0.117570 | 0.871754 | 0.817941 |
Sepal.Width | -0.117570 | 1.000000 | -0.428440 | -0.366126 |
Petal.Length | 0.871754 | -0.428440 | 1.000000 | 0.962865 |
Petal.Width | 0.817941 | -0.366126 | 0.962865 | 1.000000 |
- 두 Feature간의 상관관계가 양 수이면 두 Feature간에 양의 상관관계가 있다고 하고 음수이면 음의 상관관계가 있다고 한다.
양의 상관관계가 있는 두 Feature는 한 Feature가 증가하는 양상을 보이면 다른 Feature도 동일한 양상을 보이며, Correlation이 클 수록 그 양상이 더 비슷 하다 할 수 있다.
Seaborn.heatmap
Seaborn의 heatmap을 이용하여 corr()을 한눈에 알아 볼 수 있게 시각화 할 수 있다.
sns.heatmap(iris.corr())
c:\users\sjhsj\appdata\local\programs\python\python38\lib\site-packages\matplotlib\backends\backend_agg.py:238: RuntimeWarning: Glyph 8722 missing from current font. font.set_text(s, 0.0, flags=flags) c:\users\sjhsj\appdata\local\programs\python\python38\lib\site-packages\matplotlib\backends\backend_agg.py:201: RuntimeWarning: Glyph 8722 missing from current font. font.set_text(s, 0, flags=flags)
<AxesSubplot:>
- annot option을 활용하여 각 Cell에 correlation 지표를 할당 할 수 있다.
sns.heatmap(iris.corr(), annot=True)
<AxesSubplot:>
Seaborn의 Size를 조절하는 몇가지 방법
sns.set(rc = {'figure.figsize':(15,8)})
sns.heatmap(iris.corr())
<AxesSubplot:>
- matplotlib를 활용하여 Size 조정
import matplotlib.pyplot as plt
plt.gcf().set_size_inches(15, 8)
sns.heatmap(iris.corr(), annot=True)
<AxesSubplot:>
plt.figure(figsize = (15,8))
sns.heatmap(iris.corr(), annot=True)
<AxesSubplot:>
댓글남기기