Kaggle competition: Titanic - Machine Learning from Disaster
weather_train_df.groupby('site_id' ).apply(lambda group: group.isna().sum() )<data_type_conversions>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
def add_lag_feature(weather_df, window=3):
group_df = weather_df.groupby('site_id')
 cols = ['air_temperature', 'cloud_coverage', 'dew_temperature', 'precip_depth_1_hr', 'sea_level_pressure', 'wind_direction', 'wind_speed']
rolled = group_df[cols].rolling(window=window, min_periods=0)
 lag_mean = rolled.mean().reset_index().astype(np.float16)
 lag_max = rolled.max().reset_index().astype(np.float16)
 lag_min = rolled.min().reset_index().astype(np.float16)
 lag_std = rolled.std().reset_index().astype(np.float16)
 for col in cols:
weather_df[f'{col}_mean_lag{window}'] = lag_mean[col]
weather_df[f'{col}_max_lag{window}'] = lag_max[col]
weather_df[f'{col}_min_lag{window}'] = lag_min[col]
weather_df[f'{col}_std_lag{window}'] = lag_std[col]<feature_engineering>
titanic=pd.read_csv('.. /input/titanic/train.csv', header = 0, dtype={'Age': np.float64})
 titanicT=pd.read_csv('.. /input/titanic/test.csv', header = 0, dtype={'Age': np.float64})
add_lag_feature(weather_train_df, window=3)
 add_lag_feature(weather_train_df, window=72 )<feature_engineering>
titanic.Survived.value_counts(normalize=True )
year_map = building_meta_df.year_built.value_counts()
building_meta_df['year_cnt'] = building_meta_df.year_built.map(year_map)
 bid_map = train_df.building_id.value_counts()
train_df['bid_cnt'] = train_df.building_id.map(bid_map )<categorify>
datadict=pd.DataFrame(titanic.dtypes)
 datadict["MissingVals"]=titanic.isnull().sum()
datadict["UniqueVals"]=titanic.nunique()
datadict["Count"]=titanic.count()
datadict.rename(columns={0:"dtype"} )
primary_use_list = building_meta_df['primary_use'].unique()
primary_use_dict = {key: value for value, key in enumerate(primary_use_list)}
print('primary_use_dict: ', primary_use_dict)
 building_meta_df['primary_use'] = building_meta_df['primary_use'].map(primary_use_dict)
 gc.collect()<feature_engineering>
titanic['Title'] = titanic.Name.str.extract('([a-zA-Z]+)*\.',expand=False)
 titanicT["Title"]= titanicT.Name.str.extract('([a-zA-Z]+)*\.', expand=False)
 titanic['Title'] = titanic['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don',
'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 titanicT['Title'] = titanicT['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don',
'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 titanic['Title'] = titanic['Title'].replace('Mlle', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Ms', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Mme', 'Mrs')
 titanicT['Title'] = titanicT['Title'].replace('Mlle', 'Miss')
 titanicT['Title'] = titanicT['Title'].replace('Ms', 'Miss')
 titanicT['Title'] = titanicT['Title'].replace('Mme', 'Mrs')
train_df = reduce_mem_usage(train_df, use_float16=True)
 building_meta_df = reduce_mem_usage(building_meta_df, use_float16=True)
 weather_train_df = reduce_mem_usage(weather_train_df, use_float16=True )<feature_engineering>
del titanic["Name"]
del titanic["Ticket"]
del titanic["Cabin"]
del titanic["PassengerId"]
del titanicT["Name"]
del titanicT["Ticket"]
del titanicT["Cabin"]
del titanicT["PassengerId"]
def add_sg(df):
w = 11
p = 2
for si in df.site_id.unique() :
index = df.site_id == si
df.loc[index, 'air_smooth'] = sg(df[index].air_temperature, w, p)
 df.loc[index, 'dew_smooth'] = sg(df[index].dew_temperature, w, p)
 df.loc[index, 'air_diff'] = sg(df[index].air_temperature, w, p, 1)
 df.loc[index, 'dew_diff'] = sg(df[index].dew_temperature, w, p, 1)
 df.loc[index, 'air_diff2'] = sg(df[index].air_temperature, w, p, 2)
 df.loc[index, 'dew_diff2'] = sg(df[index].dew_temperature, w, p, 2 )<feature_engineering>
meanS=titanic[titanic.Survived==1].Age.mean()
titanic["age"]=np.where(titanic.Age.isnull() & titanic.Survived==1 , meanS, titanic["Age"])
 meanN=titanic[titanic.Survived==0].Age.mean()
titanic.age.fillna(meanN,inplace=True)
 meanT=titanicT.Age.mean()
titanicT["age"]=np.where(titanicT.Age.isnull() ,meanT,titanicT["Age"])
 del titanic["Age"]
del titanicT["Age"]
titanic["Age_Bin"]=pd.cut(titanic["age"],bins=[0,12,20,40,120], labels=["A","B","C","D"])
 titanicT["Age_Bin"]=pd.cut(titanicT["age"],bins=[0,12,20,40,120], labels=["A","B","C","D"])
 del titanic["age"]
del titanicT["age"]
add_sg(weather_train_df )<define_variables>
titanic["Fare"]=titanic.Fare.replace(0,np.NaN)
 FareS=(titanic[titanic["Survived"]==1].Fare.mean())
 FareN=(titanic[titanic["Survived"]==0].Fare.mean())
 FareT=(titanicT.Fare.mean())
 titanic["fare"]=np.where(titanic.Fare.isnull() & titanic.Survived==1, FareS, titanic["Fare"])
 titanic.fare.fillna(FareN, inplace=True)
 titanicT["fare"]=np.where(titanicT.Fare.isnull() , FareT, titanicT["Fare"])
 titanic["Fare_Bin"]=pd.cut(titanic["fare"],bins=[0,7.91,14.45,31,120,1000], labels=["A","B","C","D","E"])
 titanicT['Fare_bin'] = pd.cut(titanicT['fare'], bins=[0,7.91,14.45,31,120,1000], labels=["A","B","C","D","E"])
 del titanic["Fare"]
del titanicT["Fare"]
category_cols = ['building_id', 'site_id', 'primary_use', 'IsHoliday', 'groupNum_train']
feature_cols = ['square_feet', 'year_built'] + [
'hour', 'weekend',
] + [
'air_temperature', 'cloud_coverage',
'dew_temperature', 'precip_depth_1_hr',
'sea_level_pressure',
'air_temperature_mean_lag72',
'air_temperature_max_lag72', 'air_temperature_min_lag72',
'air_temperature_std_lag72', 'cloud_coverage_mean_lag72',
'dew_temperature_mean_lag72', 'precip_depth_1_hr_mean_lag72',
'sea_level_pressure_mean_lag72',
'wind_speed_mean_lag72',
'air_temperature_mean_lag3',
'air_temperature_max_lag3',
'air_temperature_min_lag3', 'cloud_coverage_mean_lag3',
'dew_temperature_mean_lag3',
'precip_depth_1_hr_mean_lag3',
'sea_level_pressure_mean_lag3',
'year_cnt', 'bid_cnt',
'dew_smooth', 'air_smooth',
'dew_diff', 'air_diff',
'dew_diff2', 'air_diff2'
] + list(df_groupNum_median.drop('timestamp',axis=1 ).columns )<merge>
titanic=pd.get_dummies(titanic, columns=["Sex","Embarked","Title","Age_Bin","Fare_Bin"],
prefix=["Sex","Embarked","Title","Age","Fare"])
 titanicT=pd.get_dummies(titanicT, columns=["Sex","Embarked","Title","Age_Bin","Fare_bin"],
prefix=["Sex","Embarked","Title","Age","Fare"] )
train_df = train_df.merge(building_meta_df, on=['building_id','meter'], how='left')
 train_df = train_df.merge(weather_train_df, on=['site_id', 'timestamp'], how='left')
 train_df = reduce_mem_usage(train_df, use_float16=True)
 del weather_train_df
gc.collect()<prepare_x_and_y>
Y_train=titanic["Survived"]
Y_train=Y_train.values
def create_X_y(train_df, groupNum_train):
target_train_df = train_df[train_df['groupNum_train'] == groupNum_train].copy()
target_train_df = target_train_df.merge(df_groupNum_median, on=['timestamp'], how='left')
 target_train_df['group_median_'+str(groupNum_train)] = np.nan
X_train = target_train_df[feature_cols + category_cols]
y_train = target_train_df['meter_reading_log1p'].values
del target_train_df
return X_train, y_train<train_model>
del titanic["Survived"]
titanic_train=titanic.values
titanic_test=titanicT.values
def fit_lgbm(train, val, devices=(-1,), seed=None, cat_features=None, num_rounds=1500, lr=0.1, bf=0.1):

 X_train, y_train = train
X_valid, y_valid = val
metric = 'l2'
params = {'num_leaves': 31,
'objective': 'regression',
'learning_rate': lr,
"boosting": "gbdt",
"bagging_freq": 5,
"bagging_fraction": bf,
"feature_fraction": 0.9,
"metric": metric,
}
device = devices[0]
if device == -1:
pass
else:
print(f'using gpu device_id {device}...')
 params.update({'device': 'gpu', 'gpu_device_id': device})
 params['seed'] = seed
early_stop = 20
verbose_eval = 50
d_train = lgb.Dataset(X_train, label=y_train, categorical_feature=cat_features)
 d_valid = lgb.Dataset(X_valid, label=y_valid, categorical_feature=cat_features)
 watchlist = [d_train, d_valid]
print('training LGB:')
 model = lgb.train(params,
train_set=d_train,
num_boost_round=num_rounds,
valid_sets=watchlist,
verbose_eval=verbose_eval,
early_stopping_rounds=early_stop)
 y_pred_valid = model.predict(X_valid, num_iteration=model.best_iteration)
 print('best_score', model.best_score)
 log = {'train/mae': model.best_score['training']['l2'],
'valid/mae': model.best_score['valid_1']['l2']}
return model, y_pred_valid, log<choose_model_class>
import tensorflow as tf
from keras import backend as K
from keras.models import load_model,Sequential
from keras.layers import Dense,LeakyReLU,BatchNormalization,Dropout
seed = 666
shuffle = False
kf = StratifiedKFold(n_splits=folds )<split>
model=Sequential()
model.add(Dense(12,input_shape=(23,)))
 model.add(LeakyReLU(alpha=0.01))
model.add(BatchNormalization(axis=1))
model.add(Dense(8))
model.add(LeakyReLU(alpha=0.01))
model.add(BatchNormalization(axis=1))
model.add(Dense(4))
model.add(LeakyReLU(alpha=0.01))
model.add(BatchNormalization(axis=1))
model.add(Dense(2))
model.add(LeakyReLU(alpha=0.01))
model.add(BatchNormalization(axis=1))
model.add(Dense(1,activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
for groupNum_train in building_meta_df['groupNum_train'].unique() :
X_train, y_train = create_X_y(train_df, groupNum_train=groupNum_train)
 y_valid_pred_total = np.zeros(X_train.shape[0])
 gc.collect()
print('groupNum_train', groupNum_train, X_train.shape)
 cat_features = [X_train.columns.get_loc(cat_col)for cat_col in category_cols]
print('cat_features', cat_features)
 exec('models' +str(groupNum_train)+ '=[]')
 train_df_site = train_df[train_df['groupNum_train']==groupNum_train].copy()
for train_idx, valid_idx in kf.split(train_df_site, train_df_site['building_id']):
train_data = X_train.iloc[train_idx,:], y_train[train_idx]
valid_data = X_train.iloc[valid_idx,:], y_train[valid_idx]
mindex = train_df_site.iloc[valid_idx,:].month.unique()
print(mindex)
 print('train', len(train_idx), 'valid', len(valid_idx))
model, y_pred_valid, log = fit_lgbm(train_data, valid_data, cat_features=category_cols,
num_rounds=num_rounds, lr=0.05, bf=0.7)
 y_valid_pred_total[valid_idx] = y_pred_valid
exec('models' +str(groupNum_train)+ '.append([mindex, model])')
 gc.collect()
if debug:
break
try:
sns.distplot(y_train)
 sns.distplot(y_valid_pred_total)
 plt.show()
except:
pass
del X_train, y_train
gc.collect()
print('-------------------------------------------------------------' )<set_options>
model.fit(x=titanic_train,y=Y_train,batch_size=32,epochs=100)
del train_df
gc.collect()<feature_engineering>
model.save("titanic" )
print('loading...')
 test_df = pd.read_feather(root/'test.feather')
 weather_test_df = pd.read_feather(root/'weather_test.feather')
 weather_test_df = weather_test_df.drop_duplicates(['timestamp', 'site_id'])
 set_local(weather_test_df)
 add_holiyday(weather_test_df)
 print('preprocessing building...')
 test_df['date'] = test_df['timestamp'].dt.date
preprocess(test_df)
 print('preprocessing weather...')
 weather_test_df = weather_test_df.groupby('site_id' ).apply(lambda group: group.interpolate(limit_direction='both'))
weather_test_df.groupby('site_id' ).apply(lambda group: group.isna().sum())
 add_sg(weather_test_df)
 add_lag_feature(weather_test_df, window=3)
 add_lag_feature(weather_test_df, window=72)
 test_df['bid_cnt'] = test_df.building_id.map(bid_map)
 test_df = test_df.merge(building_meta_df[['building_id','meter','groupNum_train']], on=['building_id','meter'], how='left')
 print('reduce mem usage...')
 test_df = reduce_mem_usage(test_df, use_float16=True)
 weather_test_df = reduce_mem_usage(weather_test_df, use_float16=True)
 gc.collect()
print(test_df.shape )<load_from_disk>
predictions=model.predict(x=titanic_test,steps=None)
 predictions.shape
sample_submission = pd.read_feather(os.path.join(root, 'sample_submission.feather'))
reduce_mem_usage(sample_submission)
 print(sample_submission.shape )<merge>
p_id=pd.read_csv('.. /input/titanic/test.csv', header = 0, dtype={'Age': np.float64})
 p_id=p_id["PassengerId"].values
predictions=predictions.reshape(418,)
 predictions=predictions>0.5
predictions=predictions.astype(np.int)
 submission=pd.DataFrame({"PassengerId":p_id,"Survived":predictions} )
<predict_on_test><EOS> 
filename = 'Titanic Predictions 1.csv'
x=submission.to_csv(filename,index=False )