본문 바로가기

[중급] 가볍게 이것저것

[스마트 빌딩] 가정집 전력 소비량 알아보기_1

가정집 전력 소비 데이터셋

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
path = 'data/GREEND/dataset_2014-04-10.csv'
building = pd.read_csv(path, sep=',')
apps = ['Coffee machine', 
        'washing machine', 
        'radio', 
        'water kettle', 
        'fridge', 
        'dishwasher', 
        'kitchen lamp', 
        'TV', 
        'vacuum cleaner']

building.columns = ['time'] + apps

building['datetime'] = building['time'].astype("datetime64[s]")
building.head()
  time Coffee machine washing machine radio water kettle fridge dishwasher kitchen lamp TV vacuum cleaner datetime
0 1.397080e+09 2.274737 0.0 0.000000 NaN NaN 0.0 0.0 0.0 NaN 2014-04-09 21:52:54
1 1.397080e+09 0.000000 0.0 0.000000 NaN NaN 0.0 0.0 0.0 NaN 2014-04-09 21:52:55
2 1.397080e+09 2.274737 0.0 0.000000 NaN NaN NaN 0.0 NaN NaN 2014-04-09 21:52:56
3 1.397080e+09 0.000000 0.0 2.171969 NaN NaN NaN 0.0 NaN NaN 2014-04-09 21:52:57
4 1.397080e+09 2.274737 0.0 0.000000 NaN NaN NaN 0.0 NaN NaN 2014-04-09 21:52:58
building = building.fillna(0)
building.shape
(78052, 11)
building['Power'] = building[apps].sum(axis=1)
building.head()
  time Coffee machine washing machine radio water kettle fridge dishwasher kitchen lamp TV vacuum cleaner datetime Power
0 1.397080e+09 2.274737 0.0 0.000000 0.0 0.0 0.0 0.0 0.0 0.0 2014-04-09 21:52:54 2.274737
1 1.397080e+09 0.000000 0.0 0.000000 0.0 0.0 0.0 0.0 0.0 0.0 2014-04-09 21:52:55 0.000000
2 1.397080e+09 2.274737 0.0 0.000000 0.0 0.0 0.0 0.0 0.0 0.0 2014-04-09 21:52:56 2.274737
3 1.397080e+09 0.000000 0.0 2.171969 0.0 0.0 0.0 0.0 0.0 0.0 2014-04-09 21:52:57 2.171969
4 1.397080e+09 2.274737 0.0 0.000000 0.0 0.0 0.0 0.0 0.0 0.0 2014-04-09 21:52:58 2.274737
# building 데이터에 연, 월, 일, 시, 분, 초를 나타내는 새로운 컬럼을 생성합니다.
# 각각의 이름을 datetime-month/day/hour/minute/second라고 지정합니다.
# 이 컬럼에 날짜(datetime) 컬럼의 dt(datetime의 약자입니다) 옵션을 활용하여 월일시분을 따로 넣어줍니다.
building["datetime-month"] = building["datetime"].dt.month
building["datetime-day"] = building["datetime"].dt.day
building["datetime-hour"] = building["datetime"].dt.hour
building["datetime-minute"] = building["datetime"].dt.minute

# dayofweek는 날짜에서 요일(월~일)을 가져오는 기능입니다.
# 값은 0(월), 1(화), 2(수), 3(목), 4(금), 5(토), 6(일) 을 나타냅니다.
building["datetime-dayofweek"] = building["datetime"].dt.dayofweek
# building 변수에 할당된 데이터의 행렬 사이즈를 출력합니다.
# 출력은 (row, column) 으로 표시됩니다.
print(building.shape)

# .head()로 building 데이터의 상위 5개를 띄우되,
# datetime과 이와 연관된 나머지 다섯 개의 컬럼만을 출력합니다.
building[["datetime", "datetime-month", "datetime-day", 
       "datetime-hour", "datetime-minute", "datetime-dayofweek"]].head()
(78052, 17)
  datetime datetime-month datetime-day datetime-hour datetime-minute datetime-dayofweek
0 2014-04-09 21:52:54 4 9 21 52 2
1 2014-04-09 21:52:55 4 9 21 52 2
2 2014-04-09 21:52:56 4 9 21 52 2
3 2014-04-09 21:52:57 4 9 21 52 2
4 2014-04-09 21:52:58 4 9 21 52 2
# 이번에는 seaborn의 barplot으로 시간대별 전력 소비량을 출력합니다.
sns.barplot(data=building, x="datetime-hour", y="Power")

# 이 시각화의 전체 사이즈는 15x5로 설정합니다.
plt.gcf().set_size_inches(15, 5)

building.corrwith(building['Power']).sort_values(ascending=False)
Power                 1.000000
washing machine       0.867977
kitchen lamp          0.459486
Coffee machine        0.152683
datetime-minute       0.091577
datetime-dayofweek    0.051429
datetime-day          0.051429
TV                    0.017004
radio                 0.004354
fridge                0.000341
vacuum cleaner       -0.000742
time                 -0.012829
datetime-hour        -0.067268
water kettle               NaN
dishwasher                 NaN
datetime-month             NaN
dtype: float64
# matplotlib의 subplots를 사용합니다. 이 함수는 여러 개의 시각화를 한 화면에 띄울 수 있도록 합니다.
# 이번에는 2x2으로 총 4개의 시각화를 한 화면에 띄웁니다.
figure, ((ax1, ax2)) = plt.subplots(nrows=2, ncols=1)

# 시각화의 전체 사이즈는 18x8로 설정합니다.
figure.set_size_inches(18, 10)

# seaborn의 barplot으로 subplots의 각 구역에
# 월, 일, 시, 분 별 전력 소비량을 출력합니다.
sns.barplot(data=building, x="datetime-hour", y="washing machine", ax=ax1)
sns.barplot(data=building, x="datetime-hour", y="Coffee machine", ax=ax2)
<matplotlib.axes._subplots.AxesSubplot at 0x1a2b66f5438>