<jupyter_start><jupyter_text>Data & Library<jupyter_code>!pip install pytimekr
!pip install xlrd
!pip install openpyxl
!pip install seaborn
import pandas as pd
import numpy as np
import glob
from pytimekr import pytimekr<jupyter_output><empty_output><jupyter_text>날씨<jupyter_code># 날씨데이터 불러오기 (2016 ~ 2019)
weather = pd.DataFrame()

for i in glob.glob('./seoul_data/weather/**.csv'):
    temp = pd.read_csv(i,encoding='euc-kr')
    weather = pd.concat([weather,temp], sort = False)
    
weather['일시'] = pd.to_datetime(weather['일시'])
weather = weather.loc[:,['일시', '평균기온(°C)', '최저기온(°C)', '최고기온(°C)', '일강수량(mm)',
                         '평균 풍속(m/s)', '평균 상대습도(%)', '가조시간(hr)', '평균 전운량(1/10)',
                         '일 최심신적설(cm)', '일 최심적설(cm)', '안개 계속시간(hr)']]
weather.columns.values[0] = '대여일자'
weather = weather.fillna(0)
weather.head(5)
# 미세먼지 데이터 불러오기 (2016 ~ 2019)
dust = pd.DataFrame()

for i in glob.glob('./seoul_data/dust/**.xlsx'):
    temp = pd.read_excel(i)
    temp = temp.groupby(['측정일자']).mean()
    dust = pd.concat([dust,temp], sort=False)
    
dust = dust.reset_index()
dust['측정일자'] = pd.to_datetime(dust['측정일자'], format='%Y%m%d')
dust = dust.drop(['권역코드','측정소코드'],axis=1)
dust.columns.values[0] = '대여일자'

dust.head(5)
# 자전거 데이터 불러오기 (2016 ~ 2019)
bike = pd.DataFrame()

for i in glob.glob('./seoul_data/bike-share/**.xlsx'):
    temp = pd.read_excel(i)
    temp = temp.groupby(['대여일자']).sum()
    temp = temp.reset_index()
    bike = pd.concat([bike,temp], sort = False)
    
bike
data = pd.merge(weather,dust,on='대여일자').merge(bike,on='대여일자')
data<jupyter_output><empty_output><jupyter_text>요일 계산 및 주말 처리<jupyter_code>def season(x):
    date = x.month*100 + x.day
    if date >= 204 and date < 505: # spring
        return 1
    elif date >= 505 and date < 807: # summer
        return 2
    elif date >= 807 and date < 1107: # fall
        return 3
    elif date >= 1107 or date < 204: # winter
        return 4
    
def year(x):
    year = x.year
    if year == 2016:
        return 0
    elif year == 2017:
        return 1
    elif year == 2018:
        return 2
    else:
        return 3
    
def month(x):
    month = x.month
    return month

def h(x):
    if x == 5 or x == 6:
        return 1
    else:
        return 0
    
data['weekday'] = data['대여일자'].dt.weekday    
data['season'] = data['대여일자'].apply(season).astype('category')   
data['year'] = data['대여일자'].apply(year).astype('category')
data['month'] = data['대여일자'].apply(month).astype('category')
data['holiday'] = data['weekday'].apply(h).astype('category')

data.index = data['대여일자'].values
data.drop(['대여일자'], axis = 1, inplace=True)
data_columns = ['temp', 'min_temp', 'max_temp', 'precipitation', 'windspeed', 'hum', 'sunshine', 'cloud', 
                'max_snowcover', 'snowcover', 'mist', 'PM', 'finePM', 
                'O3', 'NO2', 'CO', 'SO2', 'cnt', 'weekday','season' ,'year' ,'month', 'holiday']
data.columns = data_columns<jupyter_output><empty_output><jupyter_text>연휴<jupyter_code>#!pip install pytimekr
#!pip install xlrd
year = [2016, 2017, 2018, 2019]

holidays = []
for yr in year:
    holidays.extend(pytimekr.holidays(yr))
holidays
for d in holidays:
    if d in data.index and data.loc[d, 'holiday'] == 0:
        data.loc[d, 'holiday'] = 1
    else:
        continue

data = data[['season', 'year', 'month', 'weekday', 'holiday',
              'temp', 'min_temp', 'max_temp', 'precipitation', 'windspeed', 'hum',
              'sunshine', 'cloud', 'max_snowcover', 'snowcover', 'mist',
              'PM', 'finePM', 'O3', 'NO2', 'CO', 'SO2', 'cnt']]<jupyter_output><empty_output><jupyter_text>|속성|설명||-|-||**season**|계절 1 : 봄, 2 : 여름, 3 : 가을, 4 : 겨울||**year**|연 0 : 2016년, 1 : 2017년, 2 : 2018년, 3 : 2019년||**month**|월||**weekday**|요일 0 : 월요일, 1 : 화요일, 2 : 수요일, 3 : 목요일, 4 : 금요일, 5 : 토요일, 6 : 일요일||**holiday**|주말 및 공휴일을 포함한 휴일(대체공휴일, 선거일 미포함)||**temp**|평균기온(°C)||**min_temp**|최저기온(°C)||**max_temp**|최고기온(°C)||**precipitation**|일강수량(mm)||**windspeed**|평균 풍속(m/s)||**hum**|평균 상대습도(%)||**sunshine**|가조시간(hr)||**cloud**|평균 전운량(1/10)||**max_snowcover**|일 최심신적설(cm)||**snowcover**|일 최심적설(cm)||**mist**|안개 계속시간(hr)||**PM**|미세먼지(㎍/㎥)||**finePM**|초미세먼지(㎍/㎥)||**O3**|오존(ppm)||**NO2**|이산화질소농도(ppm)||**CO**|일산화탄소농도(ppm)||**SO2**|아황산가스농도(ppm)||**cnt**|대여건수(대)|<jupyter_code>data.columns<jupyter_output><empty_output><jupyter_text>시계열분해<jupyter_code>!pip install statsmodels --upgrade
data1=data.sort_index()
data1
from statsmodels.tsa.seasonal import seasonal_decompose
import matplotlib.pyplot as plt

decomposition = seasonal_decompose(data1.cnt,period=365)

plt.figure(figsize=(100,80))

fig = decomposition.plot()
plt.show()
fig.savefig('decomposition.png')
data2=data1.reset_index().rename(columns={'index':'rdate'})
new_data=data2.reset_index().rename(columns={'index':'T'})
new_data
new_data.to_csv('seoul_data_full.csv',index = False)
##구간으로 나눠서 실행 
train = new_data[new_data.rdate<'2019-07-01']
test = new_data[new_data.rdate>='2019-07-01']

train.to_csv('seoul_data_train.csv',index = False)
test.to_csv('seoul_data_test.csv',index = False)

X_train = train.drop(['cnt','rdate'],axis=1)
y_train = train['cnt']
X_test = test.drop(['cnt','rdate'],axis=1)
y_test = test['cnt']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20743865030674846, 'fraction_numerical': 0.03450920245398773, 'mean_word_length': 4.378350515463918, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '589633', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2660, 'n_words': 392, 'path': 'Linear Regression/seoul_bike_pre_v0.2.ipynb', 'repo_name': 'jeewoolee-jasmine/TIL', 'size': '5216'}