<jupyter_start><jupyter_text>Feature Engineering<jupyter_code>train['year'] = train['datetime'].dt.year
train['month'] = train['datetime'].dt.month
train['hour'] = train['datetime'].dt.hour
train['dayofweek'] = train['datetime'].dt.dayofweek
train.shape
test['year'] = test['datetime'].dt.year
test['month'] = test['datetime'].dt.month
test['hour'] = test['datetime'].dt.hour
test['dayofweek'] = test['datetime'].dt.dayofweek
test.shape
rfModel_wind = RandomForestRegressor()
f_columns = ['season','weather','humidity','month','temp','year','hour','dayofweek','atemp']
X_train = train[f_columns]
y_r_label = train['registered']
y_c_label = train['casual']
print(X_train.shape)
print(y_r_label.shape)
print(y_c_label.shape)
X_test = test[f_columns]
X_test.shape
rfModel = RandomForestRegressor()
rfModel.fit(X_train, y_r_label)
testRegisteredValues = rfModel.predict(X_test)
test['registered'] = testRegisteredValues
test['registered'].isnull().sum()
rfModel.fit(X_train, y_c_label)
testCasualValues = rfModel.predict(X_test)
test['casual'] = testCasualValues
test['casual'].isnull().sum()
fig,(ax1,ax2)= plt.subplots(nrows=2)
fig.set_size_inches(12,8)

hourTransformed = pd.melt(train[['hour','casual','registered']], id_vars=['hour'], value_vars=['casual', 'registered'])
hourAggregated = pd.DataFrame(hourTransformed.groupby(['hour','variable'],sort=True)['value'].mean()).reset_index()
sns.pointplot(x=hourAggregated['hour'], y=hourAggregated['value'],hue=hourAggregated['variable'],hue_order=['casual','registered'], data=hourAggregated, join=True, ax=ax1)


hourTransformed = pd.melt(test[['hour','casual','registered']], id_vars=['hour'], value_vars=['casual', 'registered'])
hourAggregated = pd.DataFrame(hourTransformed.groupby(['hour','variable'],sort=True)['value'].mean()).reset_index()
sns.pointplot(x=hourAggregated['hour'], y=hourAggregated['value'],hue=hourAggregated['variable'],hue_order=['casual','registered'], data=hourAggregated, join=True, ax=ax2)
# train과 test 데이터셋을 합쳐준다.
data = train.append(test)
data.reset_index(inplace=True)
data.drop('index',inplace=True,axis=1)
# 풍속이 0인 데이터들의 값을 보정해 준다.

dataWind0 = data[data['windspeed']==0]
dataWindNot0 = data[data['windspeed']!=0]
rfModel_wind = RandomForestRegressor()
windColumns = ['season','weather','humidity','month','temp','year','atemp']
rfModel_wind.fit(dataWindNot0[windColumns], dataWindNot0['windspeed'])

wind0Values = rfModel_wind.predict(X= dataWind0[windColumns])
dataWind0['windspeed'] = wind0Values
data = dataWindNot0.append(dataWind0)
data.reset_index(inplace=True)
data.drop('index',inplace=True,axis=1)
from sklearn.metrics import make_scorer

def rmsle(predicted_values, actual_values, convertExp=True):

    if convertExp:
        predicted_values = np.exp(predicted_values),
        actual_values = np.exp(actual_values)
        
    # 넘파이로 배열 형태로 바꿔준다.
    predicted_values = np.array(predicted_values)
    actual_values = np.array(actual_values)
    
    # 예측값과 실제 값에 1을 더하고 로그를 씌워준다.
    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
categoricalFeatureNames = ['season','holiday','workingday','weather','dayofweek','year','hour']
numericalFeatureNames = ['temp','humidity','windspeed','atemp','casual','registered']
dropFeatures = ['count','datetime','month']
for var in categoricalFeatureNames:
    data[var] = data[var].astype("category")
X_train = data[pd.notnull(data['count'])].sort_values(by=['datetime'])
X_test = data[~pd.notnull(data['count'])].sort_values(by=['datetime'])
datetimecol = X_test['datetime']
y_train = X_train['count']
X_train  = X_train.drop(dropFeatures,axis=1)
X_test  = X_test.drop(dropFeatures,axis=1)
rfModel = RandomForestRegressor(n_estimators=100)

y_train_log = np.log1p(y_train)
rfModel.fit(X_train, y_train_log)
preds = rfModel.predict(X_train)
score = rmsle(np.exp(y_train_log),np.exp(preds),False)
print ("RMSLE Value For Random Forest: ",score)
predsTest = rfModel.predict(X_test)
fig,(ax1,ax2)= plt.subplots(ncols=2)
fig.set_size_inches(12,5)
sns.distplot(y_train,ax=ax1,bins=50)
sns.distplot(np.exp(predsTest),ax=ax2,bins=50)
submission = pd.read_csv("data/sampleSubmission.csv")
submission

submission["count"] = np.exp(predsTest)

print(submission.shape)
submission.head()
submission.to_csv("data/Score_{0:.5f}_submission.csv".format(score), index=False)
# %ls data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17198275862068965, 'fraction_numerical': 0.009913793103448277, 'mean_word_length': 7.890804597701149, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '363471', 'license': 'MIT', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1807, 'n_words': 228, 'path': 'bike-sharing-demand/bike-sharing-demand-casual-registered.ipynb', 'repo_name': 'bradsonn/KaggleStruggle', 'size': '4640'}