Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<prepare_output>
%matplotlib inline
sns.set()
train_preds2 = torch.max(train_preds.data,1)[1]
train_preds2[:3]<prepare_output>
df = pd.read_csv(".. /input/train.csv" )
p_train["preds"] = train_preds2
p_train<feature_engineering>
drop_lst = ['casual', 'registered']
df = df.drop(drop_lst, axis=1)
 df.head()
p_train["judge"] = p_train["Survived"] == p_train["preds"]
p_train<compute_test_metric>
df['count_log'] = count_log
df['count_boxcox'] = count_boxcox
accuracy_score(train_Y,train_preds2 )<compute_test_metric>
df['datetime'] = pd.to_datetime(df['datetime'] )
def calc_accuracy(x,y,model):
preds = model(x)
 preds2 = torch.max(preds.data,1)[1]
return accuracy_score(y,preds2)
<compute_test_metric>
df['dow'] = df['datetime'].dt.dayofweek
df.head()
calc_accuracy(train_X,train_Y,model )<compute_test_metric>
df['month'] = df['datetime'].dt.month
df.head()
calc_accuracy(val_X,val_Y,model )<train_on_grid>
df['week'] = df['datetime'].dt.week
df.head()
all_trainloss = []
all_valloss = []
all_trainscore = []
all_valscore = []
bestscore = None
model=Net()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters() , lr=0.01)
 for epoch in tqdm(range(1000)) :
model,trainloss = training(train_dataloader,model)
 all_trainloss.append(trainloss)
 valloss = valeval(val_dataloader,model)
 all_valloss.append(valloss)
 trainscore = calc_accuracy(train_X,train_Y,model)
 all_trainscore.append(trainscore)
 valscore = calc_accuracy(val_X,val_Y,model)
 all_valscore.append(valscore)
 if bestscore is None:
bestscore = valscore
state = {
'state_dict': model.state_dict() ,
'optimizer_dict': optimizer.state_dict() ,
"bestscore":bestscore
}
torch.save(state, "model1.pth")
 elif bestscore < valscore:
bestscore = valscore
state = {
'state_dict': model.state_dict() ,
'optimizer_dict': optimizer.state_dict() ,
"bestscore":bestscore
}
torch.save(state, "model1.pth")
 else:
pass 
<load_pretrained>
df['hour'] = df['datetime'].dt.hour
df.head()
state = torch.load("./model1.pth" )<load_from_disk>
df['year'] = df['datetime'].dt.year
df.head()
model.load_state_dict(state["state_dict"] )<load_from_csv>
df['day'] = df['datetime'].dt.day
df.head()
submission = pd.read_csv(".. /input/titanic/gender_submission.csv")
 submission<prepare_x_and_y>
df = df.drop(labels='datetime', axis=1)
 df.head()
test_X = test[FEATURES]
test_X<normalization>
spring_2011 = int(df['season'][df['season'] == 1][df['year'] == 2011].value_counts())
 summer_2011 = int(df['season'][df['season'] == 2][df['year'] == 2011].value_counts())
 fall_2011 = int(df['season'][df['season'] == 3][df['year'] == 2011].value_counts())
 winter_2011 = int(df['season'][df['season'] == 4][df['year'] == 2011].value_counts())
 spring_2012 = int(df['season'][df['season'] == 1][df['year'] == 2012].value_counts())
 summer_2012 = int(df['season'][df['season'] == 2][df['year'] == 2012].value_counts())
 fall_2012 = int(df['season'][df['season'] == 3][df['year'] == 2012].value_counts())
 winter_2012 =int(df['season'][df['season'] == 4][df['year'] == 2012].value_counts())
 print("Spring 2011: {}".format(spring_2011))
print("Summer 2011: {}".format(summer_2011))
print("Fall 2011: {}".format(fall_2011))
print("Winter 2011: {}".format(winter_2011))
print("-----------------------------------------")
 print("Spring 2012: {}".format(spring_2012))
print("Summer 2012: {}".format(summer_2012))
print("Fall 2012: {}".format(fall_2012))
print("Winter 2012: {}".format(winter_2012))
test_X = np.array(test[FEATURES])
 test_X = scaler.transform(test_X)
 test_X = torch.from_numpy(test_X ).float()<prepare_output>
no_holiday_2011 = int(df['holiday'][df['holiday'] == 0][df['year'] == 2011].value_counts())
 holiday_2011 = int(df['holiday'][df['holiday'] == 1][df['year'] == 2011].value_counts())
 no_holiday_2012 = int(df['holiday'][df['holiday'] == 0][df['year'] == 2012].value_counts())
 holiday_2012 = int(df['holiday'][df['holiday'] == 1][df['year'] == 2012].value_counts())
 print("No Holidays 2011: {}".format(no_holiday_2011))
print("No Holidays 2012: {}".format(no_holiday_2012))
print("Holidays 2011: {}".format(holiday_2011))
print("Holidays 2012: {}".format(holiday_2012))
print('----------------')
 total_2011 = no_holiday_2011 + holiday_2011
total_2012 = no_holiday_2012 + holiday_2012
print('No Holidays 2011: {:.0f}%'.format(no_holiday_2011 / total_2011 * 100))
print('No Holidays 2012: {:.0f}%'.format(no_holiday_2012 / total_2012 * 100))
preds = model(test_X )<prepare_output>
no_workingday_2011 = int(df['workingday'][df['workingday'] == 0][df['year'] == 2011].value_counts())
 workingday_2011 = int(df['workingday'][df['workingday'] == 1][df['year'] == 2011].value_counts())
 no_workingday_2012 = int(df['workingday'][df['workingday'] == 0][df['year'] == 2012].value_counts())
 workingday_2012 = int(df['workingday'][df['workingday'] == 1][df['year'] == 2012].value_counts())
 print("No working day 2011: {}".format(no_workingday_2011))
print("working day 2011: {}".format(workingday_2011))
print("No working day 2012: {}".format(no_workingday_2012))
print("working day 2012: {}".format(workingday_2012))
print('----------------')
 total_2011 = no_workingday_2011 + workingday_2011
total_2012 = no_workingday_2012 + workingday_2012
print('No working day 2011: {:.0f}%'.format(no_workingday_2011 / total_2011 * 100))
print('No working day 2012: {:.0f}%'.format(no_workingday_2012 / total_2012 * 100))
preds2 = torch.max(preds.data,1)[1]<prepare_output>
weather_2011_1 = df['weather'][df['weather'] == 1][df['year'] == 2011].value_counts()
weather_2011_2 = df['weather'][df['weather'] == 2][df['year'] == 2011].value_counts()
weather_2011_3 = df['weather'][df['weather'] == 3][df['year'] == 2011].value_counts()
weather_2012_1 = df['weather'][df['weather'] == 1][df['year'] == 2012].value_counts()
weather_2012_2 = df['weather'][df['weather'] == 2][df['year'] == 2012].value_counts()
weather_2012_3 = df['weather'][df['weather'] == 3][df['year'] == 2012].value_counts()
weather_2012_4 = df['weather'][df['weather'] == 4][df['year'] == 2012].value_counts()
print('weather_1 in 2011: {}'.format(int(weather_2011_1)))
 print('weather_2 in 2011: {}'.format(int(weather_2011_2)))
 print('weather_3 in 2011: {}'.format(int(weather_2011_3)))
 print('--------------')
 print('weather_1 in 2012: {}'.format(int(weather_2012_1)))
 print('weather_2 in 2012: {}'.format(int(weather_2012_2)))
 print('weather_3 in 2012: {}'.format(int(weather_2012_3)))
 print('weather_4 in 2012: {}'.format(int(weather_2012_4)))
 print('---------------')
 total_2011 = int(weather_2011_1)+ int(weather_2011_2)+ int(weather_2011_3)
 total_2012 = int(weather_2012_1)+ int(weather_2012_2)+ int(weather_2012_3)+ int(weather_2012_4)
 print('weather_1 in 2011: {:.0f}%'.format(int(weather_2011_1)/ int(total_2011)* 100))
print('weather_2 in 2011: {:.0f}%'.format(int(weather_2011_2)/ int(total_2011)* 100))
print('weather_3 in 2011: {:.0f}%'.format(int(weather_2011_3)/ int(total_2011)* 100))
print('--------------')
 print('weather_1 in 2012: {:.0f}%'.format(int(weather_2012_1)/ int(total_2012)* 100))
print('weather_2 in 2012: {:.0f}%'.format(int(weather_2012_2)/ int(total_2012)* 100))
print('weather_3 in 2012: {:.0f}%'.format(int(weather_2012_3)/ int(total_2012)* 100))
print('weather_4 in 2012: {:.0f}%'.format(int(weather_2012_4)/ int(total_2012)* 100))
submission["Survived"] = preds2<save_to_csv>
count_2011 = df['year'][df['year'] == 2011].count()
count_2012 = df['year'][df['year'] == 2012].count()
print('2011: {}'.format(count_2011))
print('2012: {}'.format(count_2012))
submission.to_csv("submission1.csv",index = False )<choose_model_class>
_, _, r_value, _, _ = stats.linregress(df['count'], df['temp'])
 r_square = r_value ** 2
r_square.round(2 )
bestscores = []
for fold in range(5):
trn_idx = folds[folds['fold'] != fold].index
val_idx = folds[folds['fold'] == fold].index
train_folds = folds.loc[trn_idx].reset_index(drop=True)
 valid_folds = folds.loc[val_idx].reset_index(drop=True)
 model=Net()
p_train = train_folds.copy()
p_val = valid_folds.copy()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters() , lr=0.01)
 train_X = np.array(p_train[FEATURES])
 train_Y = np.array(p_train[TARGET])
 val_X = np.array(p_val[FEATURES])
 val_Y = np.array(p_val[TARGET])
 scaler = StandardScaler()
scaler.fit(train_X)
 train_X = scaler.transform(train_X)
 val_X = scaler.transform(val_X)
 train_X = torch.from_numpy(train_X ).float()
train_Y = torch.from_numpy(train_Y ).long()
val_X = torch.from_numpy(val_X ).float()
val_Y = torch.from_numpy(val_Y ).long()
train_dataset = TensorDataset(train_X,train_Y)
 val_dataset = TensorDataset(val_X,val_Y)
 train_dataloader = DataLoader(train_dataset,batch_size=128,shuffle = True)
 val_dataloader = DataLoader(val_dataset,batch_size=128,shuffle = False)
 all_trainloss = []
all_valloss = []
all_trainscore = []
all_valscore = []
bestscore = None
model=Net()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters() , lr=0.01)
 for epoch in tqdm(range(1000)) :
model,trainloss = training(train_dataloader,model)
 all_trainloss.append(trainloss)
 valloss = valeval(val_dataloader,model)
 all_valloss.append(valloss)
 trainscore = calc_accuracy(train_X,train_Y,model)
 all_trainscore.append(trainscore)
 valscore = calc_accuracy(val_X,val_Y,model)
 all_valscore.append(valscore)
 if bestscore is None:
bestscore = valscore
state = {
'state_dict': model.state_dict() ,
'optimizer_dict': optimizer.state_dict() ,
"bestscore":bestscore
}
torch.save(state, "model"+str(fold)+ ".pth")
 elif bestscore < valscore:
bestscore = valscore
state = {
'state_dict': model.state_dict() ,
'optimizer_dict': optimizer.state_dict() ,
"bestscore":bestscore
}
torch.save(state, "model"+str(fold)+ ".pth")
 else:
pass
bestscores.append(bestscore)
<load_pretrained>
_, _, r_value, _, _ = stats.linregress(df['count'], df['atemp'])
 r_square = r_value ** 2
r_square.round(2 )
states = [torch.load("model" + str(s)+ ".pth")for s in range(5)]<feature_engineering>
_, _, r_value, _, _ = stats.linregress(df['temp'], df['atemp'])
 r_square = r_value ** 2
r_square.round(2 )
soft_values = []
for state in states:
model.load_state_dict(state["state_dict"])
 model.eval()
with torch.no_grad() :
y_pred = model(test_X)
 soft_values.append(y_pred.softmax(1 ).to("cpu" ).numpy())
 soft_values2 = np.mean(soft_values,axis=0 )<define_variables>
df = df.drop(labels='atemp', axis=1 )
preds2 = [soft_values2[s].argmax() for s in range(len(soft_values2)) ]<prepare_output>
df = df.drop(labels='count_log', axis=1 )
submission["Survived"] = preds2<save_to_csv>
df = df.drop(labels='count_boxcox', axis=1 )
submission.to_csv("submission2.csv",index=False )<import_modules>
df = pd.get_dummies(df, columns=['weather'])
 df.head()
warnings.filterwarnings('ignore' )<load_from_csv>
df = df.drop(labels='weather_4', axis=1)
 df.head()
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 combine = [train_df, test_df] 
<drop_column>
df['temp_weath_1'] = df['temp'] * df['weather_1']
df['temp_weath_2'] = df['temp'] * df['weather_2']
df['temp_weath_3'] = df['temp'] * df['weather_3']
for dataset in combine:
dataset = dataset.drop(['Ticket', 'Cabin'], axis=1, inplace=True )<feature_engineering>
df['temp_weath_1'] = df['temp_weath_1'].astype(int)
 df['temp_weath_2'] = df['temp_weath_2'].astype(int)
 df['temp_weath_3'] = df['temp_weath_3'].astype(int )
for dataset in combine:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False )<feature_engineering>
X = df.loc[:, df.columns != 'count']
y = np.log(df['count'] )
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
from sklearn.model_selection import train_test_split
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
<drop_column>
from sklearn.model_selection import train_test_split
for dataset in combine:
dataset.drop(['Name'], axis=1, inplace=True )<drop_column>
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2 )
train_df.drop(['PassengerId'], axis=1, inplace=True )<data_type_conversions>
from sklearn.linear_model import LinearRegression, Lasso, ElasticNet, Ridge
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
from sklearn.svm import SVR
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler, MaxAbsScaler, MinMaxScaler, Normalizer, minmax_scale, QuantileTransformer, RobustScaler, PolynomialFeatures
from sklearn.model_selection import KFold, cross_val_score
from xgboost import XGBRegressor
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int )<data_type_conversions>
df_test = pd.read_csv(".. /input/test.csv" )
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna('S' )<groupby>
df_test['datetime'] = pd.to_datetime(df_test['datetime'] )
train_df[['Embarked', 'Survived']].groupby('Embarked' ).mean()<data_type_conversions>
df_test['dow'] = df_test['datetime'].dt.dayofweek
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'Q': 1, 'C': 2} ).astype(int )<groupby>
df_test['month'] = df_test['datetime'].dt.month
def mean_age_for_class(dataset):
mean_age_1class = dataset[dataset['Pclass'] == 1]['Age'].mean()
mean_age_2class = dataset[dataset['Pclass'] == 2]['Age'].mean()
mean_age_3class = dataset[dataset['Pclass'] == 3]['Age'].mean()
mean_age_list =(mean_age_1class, mean_age_2class, mean_age_3class)
 return mean_age_list
for dataset in combine:
mean_age_list = mean_age_for_class(dataset)
 print(mean_age_list)
<categorify>
df_test['week'] = df_test['datetime'].dt.week
def fill_age_train(columns):
Age = columns[0]
Pclass = columns[1]
if pd.isnull(Age):
if(Pclass == 1):
return 38
elif(Pclass == 2):
return 30
elif(Pclass == 3):
return 25
else:
return Age 
<feature_engineering>
df_test['hour'] = df_test['datetime'].dt.hour
train_df['Age'] = train_df[['Age', 'Pclass']].apply(fill_age_train, axis=1 )<categorify>
df_test['year'] = df_test['datetime'].dt.year
def fill_age_test(columns):
Age = columns[0]
Pclass = columns[1]
if pd.isnull(Age):
if(Pclass == 1):
return 41
elif(Pclass == 2):
return 29
elif(Pclass == 3):
return 24
else:
return Age<feature_engineering>
df_test['day'] = df_test['datetime'].dt.day
test_df['Age'] = test_df[['Age', 'Pclass']].apply(fill_age_test, axis=1 )<data_type_conversions>
df_test = df_test.set_index(df_test['datetime'] )
test_df['Fare'].fillna(test_df['Fare'].mean() , inplace=True )<feature_engineering>
df_test = df_test.drop(labels='datetime', axis=1 )
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch']<feature_engineering>
df_test = df_test.drop(labels='atemp', axis=1 )
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 0, 'IsAlone'] = 1<drop_column>
df_test = pd.get_dummies(df_test, columns=['weather'] )
for dataset in combine:
dataset = dataset.drop(['Parch', 'SibSp'], axis=1, inplace=True )<groupby>
df_test = df_test.drop(labels='weather_4', axis=1 )
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'] ).mean()<data_type_conversions>
df_test['temp_weath_1'] = df_test['temp'] * df_test['weather_1']
df_test['temp_weath_2'] = df_test['temp'] * df_test['weather_2']
df_test['temp_weath_3'] = df_test['temp'] * df_test['weather_3']
age_bin_labels = [1, 2, 3, 4]
fare_bin_labels = [1, 2, 3, 4, 5]
for dataset in combine:
dataset['Age'] = pd.qcut(dataset['Age'], q=4, labels=age_bin_labels ).astype(int)
 dataset['Fare'] = pd.qcut(dataset['Fare'], q=5, labels=fare_bin_labels ).astype(int )<prepare_x_and_y>
df_test['temp_weath_1'] = df_test['temp_weath_1'].astype(int)
 df_test['temp_weath_2'] = df_test['temp_weath_2'].astype(int)
 df_test['temp_weath_3'] = df_test['temp_weath_3'].astype(int )
X = train_df.drop('Survived', axis=1)
 Y = train_df['Survived']
X_test = test_df.drop('PassengerId', axis=1 )<split>
standardscaler = StandardScaler()
model = XGBRegressor(colsample_bytree=0.7, learning_rate=0.05, max_depth=7, min_child_weight=4, subsample=0.7, random_state=42 )
X_train, X_val, y_train, y_val = train_test_split(X, Y, random_state=42 )<train_on_grid>
model.fit(X_train, y_train )
parameters = {'n_estimators': [49, 50, 51],
'max_depth': [4, 5, 6],
'max_features': [3, 4, 5],
'min_samples_leaf': [9, 10,11, 12, 13]}
rf = GridSearchCV(RandomForestClassifier(random_state=1, n_jobs=-1), parameters)
 rf.fit(X_train, y_train)
 rf_predict = rf.predict(X_val)
 rf.score(X_val, y_val )<find_best_params>
model.fit(X_train, y_train )
rf.best_estimator_<train_on_grid>
model.predict(df_test )
parameters = {'max_iter': [50, 100, 200, 300],
'alpha': [0.09, 0.1, 0.2, 0.3, 0.4]}
sgd = GridSearchCV(SGDClassifier(random_state=1, n_jobs=-1), parameters)
 sgd.fit(X_train, y_train)
 sgd_predict = sgd.predict(X_val)
 sgd.score(X_val, y_val )<find_best_params>
pipe = Pipeline([('poly', PolynomialFeatures()),('StandardScaler', standardscaler),('XGBR', model)])
 pipe.fit(X_train, y_train)
 y_pred = np.exp(pipe.predict(df_test))
y_pred
sgd.best_params_<find_best_params>
df_test['count'] = y_pred
parameters = {'max_iter': [25, 30, 35, 50, 100, 200, 300],
'C': [0.09, 0.1, 0.2, 0,5,0.9,1, 2, 3, 4]}
log_reg = GridSearchCV(LogisticRegression(random_state=1, n_jobs=-1), parameters)
 log_reg.fit(X_train, y_train)
 log_reg_predict = log_reg.predict(X_val)
 log_reg.score(X_val, y_val )<find_best_params>
df_test[['count']].to_csv('submission.csv', index=True )