>RE::VISION CRM

Python데이터분석

[파이썬] 히트맵 heatmap 을 활용한 탐색적 분석 예제

YONG_X 2019. 8. 21. 13:20


[파이썬] 

     히트맵 heatmap 을 활용한 탐색적 분석 


예제에서 사용할 연습용 데이터 파일 :

dff01.csv


추가 데이터 (상관관계 분석용 )::







Numpy Practice in EDA :: Retail Customer Analysis -- BuyIt.com

In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from numpy.polynomial.polynomial import polyfit
import matplotlib.style as style 
from IPython.display import Image
import warnings
warnings.filterwarnings('ignore')

Heatmapping


[전용준. 리비젼컨설팅]

? 실무적인 EDA 탐색적 분석에서는 여러 변수들의 조합에 대한 동일 패턴 반복 검토 흔함
? Heatmap 은 EDA 탐색적 분석에서 반복적 사용하기 좋음 = 매우 유용
? matplotlib.scatter() 사용하는 방식 Vs. seaborn.heatmap() 선택?

In [2]:
# dff01 = dfc01[dfc01['sex']=='F'][['age','height','weight','amt_strbk','amt_book']]
# dff01.to_csv(dataPath + 'dff01.csv', index=False)
# dff01 = pd.read_csv(dataPath + 'dff01.csv')
# 블로그로부터 CSV 파일 형식의 데이터 불러오기
dff01 = pd.read_csv('https://t1.daumcdn.net/cfile/blog/992CFF3B5D5CC70C2C?download')
dff01.head()
Out[2]:
ageheightweightamt_strbkamt_book
02815752223000
12815447351000
22815552213000
3271554400
42815551175000
In [3]:
# matplotlib scatter 사용
plt.scatter(dff01.weight, dff01.height)
plt.xlabel('WEIGHT')
plt.ylabel('HEIGHT')
plt.show()
plt.scatter(dff01.amt_strbk, dff01.amt_book)
plt.xlabel('STARBUCKS')
plt.ylabel('BOOK')
plt.show()
In [4]:
dff01['rto_strbk'] = dff01.amt_strbk / (dff01.amt_strbk + dff01.amt_book + 0.001)
plt.hist(dff01.rto_strbk, bins=50)
plt.title('STARBUCKS / (STARBUCKS + BOOK) = RATIO')
plt.show()
colors1 = ['red' if x>=0.99 else 'blue' for x in dff01.rto_strbk]
plt.scatter(dff01.weight, dff01.height,
        alpha=0.1, color=colors1)
plt.xlabel('WEIGHT')
plt.ylabel('HEIGHT')
plt.title('RED: STARBUCKS')
plt.show()
In [5]:
dfc02z = dff01[['rto_strbk', 'height', 'weight']].groupby(['height', 'weight']).mean().reset_index()
dfc02z1 = dff01[['rto_strbk', 'height', 'weight']].groupby(['height', 'weight']).count().reset_index()
dfc02z1.columns = ['height', 'weight', 'cnt_cust']
dfc02z2 = dfc02z.merge(dfc02z1, how='left', on=['height', 'weight'])
print(dfc02z2.head(3))
colors1 = [(x, 0, 1-x) for x in dfc02z2.rto_strbk]
# ncust = mnmx_scl2(dfc02z2.cnt_cust)
# plt.hist(dfc02z2.cnt_cust, bins=50)
# plt.show()
dfc02z2 = dfc02z2[dfc02z2.cnt_cust>=5]
# ncust = mnmx_scl2(dfc02z2.cnt_cust)
# plt.hist(dfc02z2.cnt_cust, bins=50)
# plt.show()
plt.scatter(dfc02z2.weight, dfc02z2.height, 
            color=colors1, marker='s')
plt.legend(['cust'])
plt.xlabel('weight')
plt.ylabel('height')
plt.suptitle('LADY, STARBUCKS or BOOK?')
plt.title('(red: STARKBUCKS)', size=10, color='r')
plt.show()
   height  weight  rto_strbk  cnt_cust
0     153      44        1.0         1
1     153      53        0.0         1
2     154      43        1.0         1
In [6]:
dfc02z21 = dfc02z2
dfc02z21.drop(['cnt_cust'], axis=1)
dfc02z21.head()
Out[6]:
heightweightrto_strbkcnt_cust
16155460.7427906
21155511.0000006
30156430.6666676
31156440.81250016
32156450.75000012

seaborn의 heatmap 기능을 사용해본다면?

In [7]:
import seaborn as sns
# 피보팅을!
dfc02z21 = dfc02z21.pivot(index='height', columns='weight',
                         values='rto_strbk')
sns.heatmap(dfc02z21, 
            cmap='RdBu',
            square=True)
plt.show()
In [8]:
print('피보팅된 테이블\n--------------')
dfc02z21[[45,46,47,48,49,50]].head()
피보팅된 테이블
--------------
Out[8]:
weight454647484950
height
155NaN0.742790NaNNaNNaNNaN
1560.7500000.9285710.9269970.8270610.7692310.941176
1570.8600320.7894740.8617000.7392360.8876820.834955
1580.7829340.8163330.8407930.8762520.7671020.841637
1591.0000000.8138320.7873590.8512080.9375000.736842
In [9]:
import seaborn as sns
dfc02z21 = dfc02z2.pivot(index='height', columns='weight', 
                         values='rto_strbk')
# Y축 값을 재정렬 (큰 값에서 작은 값의 순서로)
dfc02z21 = dfc02z21.sort_values('height', ascending=False)
sns.heatmap(dfc02z21, 
            cmap='RdBu_r',  # 컬러맵을 반대로
            square=True, linewidth=0.1)
plt.show()
In [10]:
# 색상을 바꿔 보면 ?
sns.heatmap(dfc02z21, 
            cmap="YlGnBu",
            square=True)
plt.show()

scatter plot 에서 cell의 크기로 사람수를 표시해보면?

In [11]:
plt.scatter(dfc02z2.weight, dfc02z2.height, 
            s=dfc02z2.cnt_cust, color=colors1, marker='s')
plt.legend(['cust'])
plt.xlabel('weight')
plt.ylabel('height')
plt.suptitle('LADY, STARBUCKS or BOOK?')
plt.title('(red: STARKBUCKS)', size=10, color='r')
plt.show()

Summary

? Heatmap 은 EDA 탐색적 분석에서 매우 유용
? 생각보다는 heatmap 전용 기능 대신 scatter 사용하는 장점이 많음
? scatter -- 마음대로 그리는 것이 가능 - 신축적 FLEXIBLE!
? 기본기능이므로 버전, 옵션 바뀌는 걱정 적음


- 끝 - 



[유튜브영상]



dff01.csv
0.09MB