<jupyter_start><jupyter_text>![bike-img](https://storage.googleapis.com/kaggle-competitions/kaggle/3948/logos/front_page.png)  Data Fieldsfrom [Bike Sharing Demand | Kaggle](https://www.kaggle.com/c/bike-sharing-demand/data) Bike Sharing Demand* Reference    * corazzon님의 [Github](https://github.com/corazzon/KaggleStruggle) Description* datetime - hourly date + timestamp  * season -  1 = spring, 2 = summer, 3 = fall, 4 = winter * holiday - whether the day is considered a holiday* workingday - whether the day is neither a weekend nor holiday* weather     - 1: Clear, Few clouds, Partly cloudy, Partly cloudy     - 2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist     - 3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds     - 4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog * temp - temperature in Celsius* atemp - "feels like" temperature in Celsius* humidity - relative humidity* windspeed - wind speed* casual - number of non-registered user rentals initiated* registered - number of registered user rentals initiated* count - number of total rentals EvaluationSubmissions are evaluated one the Root Mean Squared Logarithmic Error (RMSLE).<jupyter_code>import pandas as pd
import numpy as np

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

from scipy import stats

import warnings
warnings.filterwarnings('ignore')
%matplotlib inline

plt.rc('font',family='Malgun Gothic')
plt.style.use('ggplot')

mpl.rcParams['axes.unicode_minus'] = False
train = pd.read_csv('data/train.csv', parse_dates=['datetime'])
train.shape
train.info()
train.head()<jupyter_output><empty_output><jupyter_text>* windspeed가 0으로 나와 있어서 데이터를 추가로 확인함<jupyter_code>train.head(20)<jupyter_output><empty_output><jupyter_text>* windspeed 데이터가 없는 경우를 0으로 표기한 것으로 보임    이 데이터 처리 방안 필요<jupyter_code>train.temp.describe()
train.isnull().sum()
import missingno as msno

msno.matrix(train, figsize=(12,5))
train['year'] = train['datetime'].dt.year
train['month'] = train['datetime'].dt.month
train['day'] = train['datetime'].dt.day
train['hour'] = train['datetime'].dt.hour
train['minute'] = train['datetime'].dt.minute
train['second'] = train['datetime'].dt.second
train.shape
train.head()
figure, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(nrows=2, ncols=3)
figure.set_size_inches(18,12)

sns.barplot(data=train, x='year', y='count', ax=ax1)
sns.barplot(data=train, x='month', y='count', ax=ax2)
sns.barplot(data=train, x='day', y='count', ax=ax3)
sns.barplot(data=train, x='hour', y='count', ax=ax4)
sns.barplot(data=train, x='minute', y='count', ax=ax5)
sns.barplot(data=train, x='second', y='count', ax=ax6)

ax1.set(ylabel='Count', title='연도별 대여량')
ax2.set(ylabel='Month', title='월별 대여량')
ax3.set(ylabel='Day', title='일별 대여량')
ax4.set(ylabel='Hour', title='시간별 대여량')<jupyter_output><empty_output><jupyter_text>* 연도별 대여량이 2012년에 늘어남* 계절별 대여량이 차이가 남* 일별 대여량은 19일까지만 있음(이 이후는 test.csv에 있음)* 시간별로 출퇴근 시간댕 대여량이 많음(평일, 주말과 나누어서 확인 필요)* 분, 초는 의미가 없음<jupyter_code>fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_size_inches(12,12)

sns.boxplot(data=train, y='count', orient='v', ax=axes[0][0])
sns.boxplot(data=train, x='season', y='count', orient='v', ax=axes[0][1])
sns.boxplot(data=train, x='hour', y='count', orient='v', ax=axes[1][0])
sns.boxplot(data=train, x='workingday', y='count', orient='v', ax=axes[1][1])

axes[0][0].set(ylabel='Count', title='연도별 대여량')
axes[0][1].set(xlabel='Season', ylabel='Month', title='계절별 대여량')
axes[1][0].set(xlabel='Hour of the Day', ylabel='Day', title='시간별 대여량')
axes[1][1].set(xlabel='Working Day', ylabel='Hour', title='근무일 여부별 대여량')
train['dayofweek'] = train['datetime'].dt.dayofweek
train.shape
train['dayofweek'].value_counts()
fig, axes = plt.subplots(nrows=5)
fig.set_size_inches(18,25)

sns.pointplot(data=train, x='hour', y='count', ax=axes[0])
sns.pointplot(data=train, x='hour', y='count', hue='workingday', ax=axes[1])
sns.pointplot(data=train, x='hour', y='count', hue='dayofweek', ax=axes[2])
sns.pointplot(data=train, x='hour', y='count', hue='weather', ax=axes[3])
sns.pointplot(data=train, x='hour', y='count', hue='season', ax=axes[4])
corrMatt = train[["temp", "atemp", "casual", "registered", "humidity", "windspeed", "count"]]
corrMatt = corrMatt.corr()
print(corrMatt)

mask = np.array(corrMatt)
mask[np.tril_indices_from(mask)] = False
fig, ax = plt.subplots()
fig.set_size_inches(20,10)
sns.heatmap(corrMatt, mask=mask, vmax=.8, square=True,annot=True)<jupyter_output><empty_output><jupyter_text>* 등록여부가 대여량과 상관관계가 높지만 test.csv에는 이 항목이 제외됨* temp와 atemp는 서로 상관관계가 높지만 대여량과의 상관관계는 낮음<jupyter_code>fig, (ax1, ax2, ax3) = plt.subplots(ncols=3)
fig.set_size_inches(12,5)

sns.regplot(data=train, x='temp', y='count', ax=ax1)
sns.regplot(data=train, x='windspeed', y='count', ax=ax2)
sns.regplot(data=train, x='humidity', y='count', ax=ax3)
def concat_year_month(datetime):
    return "{0}-{1}".format(datetime.year, datetime.month)

train['year_month'] = train['datetime'].apply(concat_year_month)

print(train.shape)

train[['datetime', 'year_month']].head()
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
fig.set_size_inches(18,4)

sns.barplot(data=train, x='year', y='count', ax=ax1)
sns.barplot(data=train, x='month', y='count', ax=ax2)

fig, ax3 = plt.subplots()
fig.set_size_inches(18,4)

sns.barplot(data=train, x='year_month', y='count', ax=ax3)
trainWithoutOutliers = train[np.abs(train['count'] - train['count'].mean()) <= (3*train['count'].std())]

print(train.shape)
print(trainWithoutOutliers.shape)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
fig.set_size_inches(12,10)

sns.distplot(train['count'], ax=ax1)
stats.probplot(train['count'], dist='norm', fit=True, plot=ax2)
sns.distplot(np.log(trainWithoutOutliers['count']), ax=ax3)
stats.probplot(np.log1p(trainWithoutOutliers['count']), dist='norm', fit=True, plot=ax4)<jupyter_output><empty_output><jupyter_text>* 평가 방법 : RMSLE* **과대평가된 항목보다는 과소평가된 항목에 패널티를 준다.**  * 오차를 제곱해서 평균한 값의 제곱근으로 계산하여 값이 작을 수록 정밀도가 높다.    0에 가까운 값이 나올 수록 정밀도가 높다.<jupyter_code>from sklearn.metrics import make_scorer

def rmsle(predicted_values, actual_values):
    predicted_values = np.array(predicted_values)
    actual_values = np.array(actual_values)

    log_predict = np.log(predicted_values + 1)
    log_actual = np.log(actual_values + 1)

    difference = log_predict - log_actual
    difference = np.square(difference)

    mean_difference = difference.mean()

    score = np.sqrt(mean_difference)

    return score
train = pd.read_csv('data/train.csv', parse_dates=['datetime'])
test = pd.read_csv('data/test.csv', parse_dates=['datetime'])

train['year'] = train['datetime'].dt.year
train['month'] = train['datetime'].dt.month
train['day'] = train['datetime'].dt.day
train['hour'] = train['datetime'].dt.hour
train['minute'] = train['datetime'].dt.minute
train['second'] = train['datetime'].dt.second
train['dayofweek'] = train['datetime'].dt.dayofweek
display(train.shape)

test['year'] = test['datetime'].dt.year
test['month'] = test['datetime'].dt.month
test['day'] = test['datetime'].dt.day
test['hour'] = test['datetime'].dt.hour
test['minute'] = test['datetime'].dt.minute
test['second'] = test['datetime'].dt.second
test['dayofweek'] = test['datetime'].dt.dayofweek
display(test.shape)
# 풍속 시각화

fig, axes = plt.subplots(nrows=2)
fig.set_size_inches(18,15)

plt.sca(axes[0])
plt.xticks(rotation=30, ha='right')
axes[0].set(ylabel='Count', title='train windspeed')
sns.countplot(data=train, x='windspeed', ax=axes[0])

plt.sca(axes[1])
plt.xticks(rotation=30, ha='right')
axes[1].set(ylabel='Count', title='test windspeed')
sns.countplot(data=test, x='windspeed', ax=axes[1])<jupyter_output><empty_output><jupyter_text>* train 데이터나 test 데이터의 풍속이 0에 많이 치중해있다.* 측정이 되지 않은 값들을 0으로 저장한 것으로 보임<jupyter_code>from sklearn.ensemble import RandomForestClassifier

def predict_windspeed(data):
    dataWind0 = data.loc[data['windspeed']==0]
    dataWindNot0 = data.loc[data['windspeed']!=0]

    col = ['season', 'weather', 'humidity', 'month', 'temp', 'year', 'atemp']

    dataWindNot0['windspeed'] = dataWindNot0['windspeed'].astype(str)

    rfModel_wind = RandomForestClassifier()
    rfModel_wind.fit(dataWindNot0[col], dataWindNot0['windspeed'])

    wind0Values = rfModel_wind.predict(X=dataWind0[col])

    predictWind0 = dataWind0
    predictWindNot0 = dataWindNot0

    predictWind0['windspeed'] = wind0Values

    data = predictWindNot0.append(predictWind0)

    data['windspeed'] = data['windspeed'].astype(float)

    data.reset_index(drop=True, inplace=True)
    
    return data
train = predict_windspeed(train)

fig, ax1 = plt.subplots()
fig.set_size_inches(18,6)

plt.sca(ax1)
plt.xticks(rotation=30, ha='right')
ax1.set(ylabel='Count',title="train windspeed")
sns.countplot(data=train, x="windspeed", ax=ax1)<jupyter_output><empty_output><jupyter_text>Feature Selection<jupyter_code>categorical_feature_names=['season','holiday','workingday','weather',
                          'dayofweek','month','year','hour']

# one hot encoding
for var in categorical_feature_names:
    train[var] = train[var].astype('category')
    test[var] = test[var].astype('category')
    
train[categorical_feature_names].head()
feature_names = ['season','weather','temp','atemp','humidity','windspeed',
               'year','hour','dayofweek','holiday','workingday']
X_train = train[feature_names]

print(X_train.shape)
X_train.head()
X_test = test[feature_names]

print(X_test.shape)
X_test.head()
label_name = 'count'

y_train = train[label_name]

print(y_train.shape)
y_train.head()
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score

k_fold = KFold(n_splits=10, shuffle=True, random_state=0)

from sklearn.ensemble import RandomForestRegressor

max_depth_list=[]
model = RandomForestRegressor(n_estimators=100,
                             n_jobs=-1,
                             random_state=0)
model
rmsle_scorer = make_scorer(rmsle)
rmsle_scorer
rmsle
%time score = cross_val_score(model, X_train, y_train, cv=k_fold, scoring=rmsle_scorer)
score = score.mean()
print("Score={0:.5f}".format(score))<jupyter_output>Score=0.33141<jupyter_text>Train<jupyter_code>model.fit(X_train, y_train)

predictions = model.predict(X_test)

print(predictions.shape)
predictions[0:10]
fig, (ax1, ax2) = plt.subplots(ncols=2)
fig.set_size_inches(18,6)

sns.distplot(y_train, ax=ax1, bins=50)
ax1.set(title='train')

sns.distplot(predictions, ax=ax2, bins=50)
ax2.set(title='test')<jupyter_output><empty_output><jupyter_text>Submit<jupyter_code>submission = pd.read_csv('data/sampleSubmission.csv')

submission['count'] = predictions
print(submission.shape)
submission.head(10)
submission.to_csv("output/Score_{0:.5f}_submission.csv".format(score), index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17131253461325457, 'fraction_numerical': 0.01938342255861178, 'mean_word_length': 6.576923076923077, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 40, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '197184', 'license': 'MIT', 'n_tokens_mistral': 4507, 'n_tokens_neox': 4579, 'n_words': 749, 'path': 'Bike_Sharing_Demand/.ipynb_checkpoints/[Kaggle]Bike Sharing Demand-checkpoint.ipynb', 'repo_name': 'kamzzang/Kaggle', 'size': '10834'}