Kaggle competition: Bike Sharing Demand
<drop_column><EOS> 
import pandas as pd
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<define_variables>
import pandas as pd
state_code = 0
train_data['Province/State'][0] = 0
for instance in range(0, 284):
for state_index in range(0, day_number):
train_data['Province/State'][state_index +(day_number * instance)] = state_code
state_code += 1<filter>
train = pd.read_csv(".. /input/train.csv")
 print(train.shape)
 train.head()
train_data[train_data['Country/Region'] == 'Turkey']<drop_column>
test = pd.read_csv(".. /input/test.csv")
 print(test.shape)
 test.head()
train_data = train_data.drop('Country/Region', axis = 1)
 test_data = test_data.drop('Country/Region', axis = 1 )<create_dataframe>
train["datetime"] = pd.to_datetime(train["datetime"] )
CC_dataset = pd.DataFrame()
Fatal_dataset = pd.DataFrame()
target = []
for forecast_id in range(0, 284):
sample_CC_data = train_data.loc[train_data['Province/State'] == forecast_id]
sample_Fatal_data = train_data.loc[train_data['Province/State'] == forecast_id]
CC_column = sample_CC_data['ConfirmedCases']
sample_CC_data = sample_CC_data.drop(['ConfirmedCases', 'Date', 'Fatalities'], axis = 1)
 Fatal_column = sample_Fatal_data['Fatalities']
sample_Fatal_data = sample_Fatal_data.drop(['Fatalities', 'Date', 'ConfirmedCases'], axis = 1)
 sample_CC_data = sample_CC_data.head(1)
 sample_Fatal_data = sample_Fatal_data.head(1)
 CC_list = list(CC_column)
 Fatal_list = list(Fatal_column)
 for CC_index in range(0,len(CC_list)) :
sample_CC_data['CC' + str(CC_index+1)] = CC_list[CC_index]
for Fatal_index in range(0,len(Fatal_list)) :
sample_Fatal_data['F' + str(Fatal_index+1)] = Fatal_list[Fatal_index]
CC_dataset = CC_dataset.append(sample_CC_data)
 Fatal_dataset = Fatal_dataset.append(sample_Fatal_data )<create_dataframe>
print(train.shape)
 train["datetime-year"] = train["datetime"].dt.year
train["datetime-month"] = train["datetime"].dt.month
train["datetime-day"] = train["datetime"].dt.day
train["datetime-hour"] = train["datetime"].dt.hour
train["datetime-minute"] = train["datetime"].dt.minute
train["datetime-second"] = train["datetime"].dt.second
train["datetime-dayofweek"] = train["datetime"].dt.dayofweek
print(train.shape)
 train[["datetime", "datetime-year", "datetime-month", "datetime-day",
"datetime-hour", "datetime-minute", "datetime-second", "datetime-dayofweek"]].head()
CC_daily_dataset = pd.DataFrame()
Fatality_daily_dataset = pd.DataFrame()
country_number = CC_dataset.shape[0]
for state_id in range(0, country_number):
sample_case = CC_dataset.loc[CC_dataset['Province/State'] == state_id]
sample_case['Day1CC'] = sample_case['CC1']
sample_fatality = Fatal_dataset.loc[Fatal_dataset['Province/State'] == state_id]
sample_fatality['Day1F'] = sample_fatality['F1']
for case_index in range(1, day_number):
sample_case['Day' + str(case_index + 1)+ 'CC'] = sample_case['CC' + str(case_index + 1)] - sample_case['CC' + str(case_index)]
sample_case = sample_case.drop(['CC' + str(case_index)], axis = 1)
 sample_fatality['Day' + str(case_index + 1)+ 'F'] = sample_fatality['F' + str(case_index + 1)] - sample_fatality['F' + str(case_index)]
sample_fatality = sample_fatality.drop(['F' + str(case_index)], axis = 1)
 sample_case = sample_case.drop(['CC' + str(day_number)], axis = 1)
 sample_fatality = sample_fatality.drop(['F' + str(day_number)], axis = 1)
 CC_daily_dataset = CC_daily_dataset.append(sample_case)
 Fatality_daily_dataset = Fatality_daily_dataset.append(sample_fatality)
<define_variables>
test["datetime"] = pd.to_datetime(test["datetime"] )
last_day_CC = CC_dataset.iloc[:,-1].values
last_day_F = Fatal_dataset.iloc[:,-1].values<drop_column>
print(test.shape)
 test["datetime-year"] = test["datetime"].dt.year
test["datetime-month"] = test["datetime"].dt.month
test["datetime-day"] = test["datetime"].dt.day
test["datetime-hour"] = test["datetime"].dt.hour
test["datetime-minute"] = test["datetime"].dt.minute
test["datetime-second"] = test["datetime"].dt.second
test["datetime-dayofweek"] = test["datetime"].dt.dayofweek
print(test.shape)
 test[["datetime", "datetime-year", "datetime-month", "datetime-day",
"datetime-hour", "datetime-minute", "datetime-second", "datetime-dayofweek"]].head()
CC_daily_dataset = CC_daily_dataset.drop(['Id', 'Province/State'], axis = 1)
 Fatality_daily_dataset = Fatality_daily_dataset.drop(['Id', 'Province/State'], axis = 1 )<prepare_x_and_y>
%matplotlib inline
features_CC = CC_dataset
train_features_CC = CC_dataset.iloc[:,:-10]
target_CC = CC_dataset.iloc[:,-10:]
features_F = Fatal_dataset
train_features_F = Fatal_dataset.iloc[:,:-10]
target_F = Fatal_dataset.iloc[:,-10:]
train_test_CC_daily_dataset = CC_daily_dataset.iloc[:,:-28]
target_CC_daily_dataset = CC_daily_dataset.iloc[:,-28:]
train_test_fatal_daily_dataset = Fatality_daily_dataset.iloc[:,:-28]
target_fatal_daily_dataset = Fatality_daily_dataset.iloc[:,-28:]<define_variables>
train["datetime-year_month"] = train["datetime-year"].astype(str)+ "-" + train["datetime-month"].astype(str)
 print(train.shape)
 train.info()
features_CC = np.array(features_CC)
 features_F = np.array(features_F)
 train_features_CC = np.array(train_features_CC)
 train_features_F = np.array(train_features_F)
 train_test_CC_daily = np.array(train_test_CC_daily_dataset)
 train_test_F_daily = np.array(train_test_fatal_daily_dataset)
 target_CC_daily = np.array(target_CC_daily_dataset)
 target_F_daily = np.array(target_fatal_daily_dataset)
<split>
feature_names = ["season", "holiday", "workingday", "weather", "temp", "atemp", "humidity", "windspeed",
"datetime-year", "datetime-hour", "datetime-dayofweek"]
feature_names
train_CC_daily, test_CC_daily, train_target_CC_daily, test_target_CC_daily = train_test_split(train_test_CC_daily,
target_CC_daily,
test_size = 0.2)
 train_F_daily, test_F_daily, train_target_F_daily, test_target_F_daily = train_test_split(train_test_F_daily,
target_F_daily,
test_size = 0.2 )<categorify>
label_name = "count"
y_train = train[label_name]
print(y_train.shape)
 y_train.head()
<normalization>
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import normalize 
<categorify>
model = RandomForestRegressor()
model
features_CC = np.expand_dims(features_CC, axis = 2)
 train_features_CC = np.expand_dims(train_features_CC, axis = 2)
 train_features_CC = np.expand_dims(train_features_CC, axis = 2)
 train_features_F = np.expand_dims(train_features_F, axis = 2)
 train_CC_daily = np.expand_dims(train_CC_daily, axis = 2)
 train_F_daily = np.expand_dims(train_F_daily, axis = 2)
 test_CC_daily = np.expand_dims(test_CC_daily, axis = 2)
 test_F_daily = np.expand_dims(test_F_daily, axis = 2 )<import_modules>
y_predict = cross_val_predict(model, x_train, y_train, cv=20)
 print(y_predict.shape)
 y_predict
from keras.layers import LSTM, Dense, Dropout, Flatten, BatchNormalization, Conv1D, MaxPooling1D
from keras.models import Sequential
from keras.regularizers import l1, l2, l1_l2<choose_model_class>
score = abs(y_train - y_predict ).mean()
f"score(Mean Absolute Error)={score:.6f}"
model = Sequential()<choose_model_class>
model.fit(x_train, y_train )
model.add(LSTM(512, input_shape =(train_CC_daily.shape[1], 1),
return_sequences=True, activation='tanh'))
model.add(LSTM(256, return_sequences=True, activation='relu'))
model.add(LSTM(512, return_sequences=False, activation='tanh'))<choose_model_class>
predictions = model.predict(x_test)
 predictions
model.add(Dense(28, activation = 'linear'))<choose_model_class>
submit["count"] = predictions
print(submit.shape)
 submit.head()
model.compile(optimizer = 'Adam', loss = 'mean_squared_logarithmic_error', metrics=['accuracy'] )<train_model>
predictions = model.predict(x_test)
 predictions
rnn = model.fit(train_CC_daily, train_target_CC_daily, batch_size=64, epochs=20,
validation_data=(test_CC_daily, test_target_CC_daily))<predict_on_test>
submit.to_csv("baseline-script.csv", index=False)
 pd.read_csv("baseline-script.csv" ).head()