공유자전거 수요량 예측
1. 기초 데이터 분석 / 시각화단계 - ing
PassionPython
2020. 4. 2. 12:48
train_baseline.csv
0.36MB
'''
이 자료에서 사용한 데이터 셋은 kaggle에서 받은 것을
datetime, season, registered, casual, workingday만 선택해 시작합니다.
acknowledgement:
아래 공개 데이터 셋을 kaggle에서 전처리 후 train/test로 나누어 제공합니다.
http://archive.ics.uci.edu/ml/datasets/Bike+Sharing+Dataset
kaggle 링크:
https://www.kaggle.com/c/bike-sharing-demand
(kaggle에서 받으려면 회원가입이 필요합니다.)
이 데이터 셋으로 나온 논문:
Fanaee-T, Hadi, and Gama, Joao,
Event labeling combining ensemble detectors and background knowledge,
Progress in Artificial Intelligence (2013):
pp. 1-15, Springer Berlin Heidelberg.
'''
from glob import glob
import pandas as pd
# datetime 시간에 registered(등록회원), casual(비등록회원), count(앞 두 개 합한 것) 자전거 대여 대수를 나타냅니다.
# 이 때의 계절을 1(겨울), workingday(일하는 날인지?) 로 표현합니다.
baseline = pd.read_csv(glob('*train_baseline.csv')[0])
baseline.head()
datetime | season | workingday | casual | registered | count | |
---|---|---|---|---|---|---|
0 | 2011-01-01 00:00:00 | 1 | 0 | 3 | 13 | 16 |
1 | 2011-01-01 01:00:00 | 1 | 0 | 8 | 32 | 40 |
2 | 2011-01-01 02:00:00 | 1 | 0 | 5 | 27 | 32 |
3 | 2011-01-01 03:00:00 | 1 | 0 | 3 | 10 | 13 |
4 | 2011-01-01 04:00:00 | 1 | 0 | 0 | 1 | 1 |
# datetime은 년/월/일/시간 정보가 들어있습니다.
# pd.DatetimeIndex() 기능을 이용해 이를 시간 정보로 변환 후
# .year, .month, .day 등으로 각 날짜 정보를 뽑아냅니다.
# dayofweek은 월 ~ 일요일을 0 ~ 6 으로 나타낸 것입니다.
dt_idx = pd.DatetimeIndex(baseline['datetime'])
baseline['year'] = dt_idx.year
baseline['month'] = dt_idx.month
baseline['day'] = dt_idx.day
baseline['dayofweek'] = dt_idx.dayofweek
baseline['hour'] = dt_idx.hour
baseline['minute'] = dt_idx.minute
baseline['second'] = dt_idx.second
print(baseline.shape)
baseline.head()
(10886, 13)
datetime | season | workingday | casual | registered | count | year | month | day | dayofweek | hour | minute | second | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2011-01-01 00:00:00 | 1 | 0 | 3 | 13 | 16 | 2011 | 1 | 1 | 5 | 0 | 0 | 0 |
1 | 2011-01-01 01:00:00 | 1 | 0 | 8 | 32 | 40 | 2011 | 1 | 1 | 5 | 1 | 0 | 0 |
2 | 2011-01-01 02:00:00 | 1 | 0 | 5 | 27 | 32 | 2011 | 1 | 1 | 5 | 2 | 0 | 0 |
3 | 2011-01-01 03:00:00 | 1 | 0 | 3 | 10 | 13 | 2011 | 1 | 1 | 5 | 3 | 0 | 0 |
4 | 2011-01-01 04:00:00 | 1 | 0 | 0 | 1 | 1 | 2011 | 1 | 1 | 5 | 4 | 0 | 0 |
# dayofweek으로부터 주말에 대한 정보를 뽑아줍니다.
# dayofweek가 5 이상이면 토, 일요일이므로 weekend 컬럼에 1을 할당합니다.
# 맨 첫 줄에 weekend 컬럼에 0을 넣은 것은 기본값으로 0을 넣어준 것입니다.
baseline['weekend'] = 0
baseline.loc[baseline['dayofweek'] >= 5, 'weekend'] = 1
baseline.head()
datetime | season | workingday | casual | registered | count | year | month | day | dayofweek | hour | minute | second | weekend | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2011-01-01 00:00:00 | 1 | 0 | 3 | 13 | 16 | 2011 | 1 | 1 | 5 | 0 | 0 | 0 | 1 |
1 | 2011-01-01 01:00:00 | 1 | 0 | 8 | 32 | 40 | 2011 | 1 | 1 | 5 | 1 | 0 | 0 | 1 |
2 | 2011-01-01 02:00:00 | 1 | 0 | 5 | 27 | 32 | 2011 | 1 | 1 | 5 | 2 | 0 | 0 | 1 |
3 | 2011-01-01 03:00:00 | 1 | 0 | 3 | 10 | 13 | 2011 | 1 | 1 | 5 | 3 | 0 | 0 | 1 |
4 | 2011-01-01 04:00:00 | 1 | 0 | 0 | 1 | 1 | 2011 | 1 | 1 | 5 | 4 | 0 | 0 | 1 |
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.pyplot as plt
figure, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(nrows=2, ncols=3)
figure.set_size_inches(18, 8)
sns.barplot(data=baseline, x="year", y="count", ax=ax1)
sns.barplot(data=baseline, x="month", y="count", ax=ax2)
sns.barplot(data=baseline, x="day", y="count", ax=ax3)
sns.barplot(data=baseline, x="hour", y="count", ax=ax4)
sns.barplot(data=baseline, x="minute", y="count", ax=ax5)
sns.barplot(data=baseline, x="second", y="count", ax=ax6)
<matplotlib.axes._subplots.AxesSubplot at 0x21db1b86d88>

baseline["year_month"] = baseline["year"].astype('str') + '-' + baseline["month"].astype('str')
baseline.head()
datetime | season | workingday | casual | registered | count | year | month | day | dayofweek | hour | minute | second | weekend | year_month | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2011-01-01 00:00:00 | 1 | 0 | 3 | 13 | 16 | 2011 | 1 | 1 | 5 | 0 | 0 | 0 | 1 | 2011-1 |
1 | 2011-01-01 01:00:00 | 1 | 0 | 8 | 32 | 40 | 2011 | 1 | 1 | 5 | 1 | 0 | 0 | 1 | 2011-1 |
2 | 2011-01-01 02:00:00 | 1 | 0 | 5 | 27 | 32 | 2011 | 1 | 1 | 5 | 2 | 0 | 0 | 1 | 2011-1 |
3 | 2011-01-01 03:00:00 | 1 | 0 | 3 | 10 | 13 | 2011 | 1 | 1 | 5 | 3 | 0 | 0 | 1 | 2011-1 |
4 | 2011-01-01 04:00:00 | 1 | 0 | 0 | 1 | 1 | 2011 | 1 | 1 | 5 | 4 | 0 | 0 | 1 | 2011-1 |
figure, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
figure.set_size_inches(18, 4)
sns.barplot(data=baseline, x="year", y="count", ax=ax1)
sns.barplot(data=baseline, x="month", y="count", ax=ax2)
figure, ax3 = plt.subplots(nrows=1, ncols=1)
figure.set_size_inches(18, 4)
sns.barplot(data=baseline, x="year_month", y="count", ax=ax3)
<matplotlib.axes._subplots.AxesSubplot at 0x21db1ddf088>


figure, ax1 = plt.subplots(nrows=1, ncols=1)
figure.set_size_inches(18, 4)
sns.pointplot(data=baseline, x="hour", y="count", hue="dayofweek", ax=ax1)
figure, ax2 = plt.subplots(nrows=1, ncols=1)
figure.set_size_inches(18, 4)
sns.pointplot(data=baseline, x="hour", y="count", hue="weekend", ax=ax2)
<matplotlib.axes._subplots.AxesSubplot at 0x21db28afcc8>


figure, ax1 = plt.subplots(nrows=1, ncols=1)
figure.set_size_inches(18, 4)
target = baseline.loc[baseline['weekend'] == 0]
sns.pointplot(data=target, x="hour", y="count", hue="year", ax=ax1)
<matplotlib.axes._subplots.AxesSubplot at 0x21db2d41088>

target = baseline.loc[baseline['weekend'] == 0]
figure, ax1 = plt.subplots(nrows=1, ncols=1)
figure.set_size_inches(18, 4)
sns.pointplot(data=target, x="hour", y="count", hue="season", ax=ax1)
<matplotlib.axes._subplots.AxesSubplot at 0x21db2c5e088>

baseline = baseline.drop(columns=['minute', 'second'])
baseline.to_csv('eda_df.csv', index=False)