Kaggle competition: Titanic - Machine Learning from Disaster
def RMSLE(y, pred):
return metrics.mean_squared_error(y, pred)**0.5
def get_nn_complete_model(train, hidden1_neurons=35, hidden2_neurons=15):
K.clear_session()
air_store_id = Input(shape=(1,), dtype='int32', name='air_store_id')
 air_store_id_emb = Embedding(len(train['air_store_id2'].unique())+ 1, 15, input_shape=(1,),
name='air_store_id_emb' )(air_store_id)
 air_store_id_emb = keras.layers.Flatten(name='air_store_id_emb_flatten' )(air_store_id_emb)
 dow = Input(shape=(1,), dtype='int32', name='dow')
 dow_emb = Embedding(8, 3, input_shape=(1,), name='dow_emb' )(dow)
 dow_emb = keras.layers.Flatten(name='dow_emb_flatten' )(dow_emb)
 month = Input(shape=(1,), dtype='int32', name='month')
 month_emb = Embedding(13, 3, input_shape=(1,), name='month_emb' )(month)
 month_emb = keras.layers.Flatten(name='month_emb_flatten' )(month_emb)
 air_area_name, air_genre_name = [], []
air_area_name_emb, air_genre_name_emb = [], []
for i in range(7):
area_name_col = 'air_area_name' + str(i)
 air_area_name.append(Input(shape=(1,), dtype='int32', name=area_name_col))
tmp = Embedding(len(train[area_name_col].unique()), 3, input_shape=(1,),
name=area_name_col + '_emb' )(air_area_name[-1])
 tmp = keras.layers.Flatten(name=area_name_col + '_emb_flatten' )(tmp)
 air_area_name_emb.append(tmp)
 if i > 4:
continue
area_genre_col = 'air_genre_name' + str(i)
 air_genre_name.append(Input(shape=(1,), dtype='int32', name=area_genre_col))
tmp = Embedding(len(train[area_genre_col].unique()), 3, input_shape=(1,),
name=area_genre_col + '_emb' )(air_genre_name[-1])
 tmp = keras.layers.Flatten(name=area_genre_col + '_emb_flatten' )(tmp)
 air_genre_name_emb.append(tmp)
 air_genre_name_emb = keras.layers.concatenate(air_genre_name_emb)
 air_genre_name_emb = Dense(4, activation='sigmoid', name='final_air_genre_emb' )(air_genre_name_emb)
 air_area_name_emb = keras.layers.concatenate(air_area_name_emb)
 air_area_name_emb = Dense(4, activation='sigmoid', name='final_air_area_emb' )(air_area_name_emb)
 air_area_code = Input(shape=(1,), dtype='int32', name='air_area_code')
 air_area_code_emb = Embedding(len(train['air_area_name'].unique()), 8, input_shape=(1,), name='air_area_code_emb' )(air_area_code)
 air_area_code_emb = keras.layers.Flatten(name='air_area_code_emb_flatten' )(air_area_code_emb)
 air_genre_code = Input(shape=(1,), dtype='int32', name='air_genre_code')
 air_genre_code_emb = Embedding(len(train['air_genre_name'].unique()), 5, input_shape=(1,),
name='air_genre_code_emb' )(air_genre_code)
 air_genre_code_emb = keras.layers.Flatten(name='air_genre_code_emb_flatten' )(air_genre_code_emb)
 holiday_flg = Input(shape=(1,), dtype='float32', name='holiday_flg')
 year = Input(shape=(1,), dtype='float32', name='year')
 min_visitors = Input(shape=(1,), dtype='float32', name='min_visitors')
 mean_visitors = Input(shape=(1,), dtype='float32', name='mean_visitors')
 median_visitors = Input(shape=(1,), dtype='float32', name='median_visitors')
 max_visitors = Input(shape=(1,), dtype='float32', name='max_visitors')
 count_observations = Input(shape=(1,), dtype='float32', name='count_observations')
 rs1_x = Input(shape=(1,), dtype='float32', name='rs1_x')
 rv1_x = Input(shape=(1,), dtype='float32', name='rv1_x')
 rs2_x = Input(shape=(1,), dtype='float32', name='rs2_x')
 rv2_x = Input(shape=(1,), dtype='float32', name='rv2_x')
 rs1_y = Input(shape=(1,), dtype='float32', name='rs1_y')
 rv1_y = Input(shape=(1,), dtype='float32', name='rv1_y')
 rs2_y = Input(shape=(1,), dtype='float32', name='rs2_y')
 rv2_y = Input(shape=(1,), dtype='float32', name='rv2_y')
 total_reserv_sum = Input(shape=(1,), dtype='float32', name='total_reserv_sum')
 total_reserv_mean = Input(shape=(1,), dtype='float32', name='total_reserv_mean')
 total_reserv_dt_diff_mean = Input(shape=(1,), dtype='float32', name='total_reserv_dt_diff_mean')
 date_int = Input(shape=(1,), dtype='float32', name='date_int')
 var_max_lat = Input(shape=(1,), dtype='float32', name='var_max_lat')
 var_max_long = Input(shape=(1,), dtype='float32', name='var_max_long')
 lon_plus_lat = Input(shape=(1,), dtype='float32', name='lon_plus_lat')
 date_emb = keras.layers.concatenate([dow_emb, month_emb, year, holiday_flg])
 date_emb = Dense(5, activation='sigmoid', name='date_merged_emb' )(date_emb)
 cat_layer = keras.layers.concatenate([holiday_flg, min_visitors, mean_visitors,
median_visitors, max_visitors, count_observations, rs1_x, rv1_x,
rs2_x, rv2_x, rs1_y, rv1_y, rs2_y, rv2_y,
total_reserv_sum, total_reserv_mean, total_reserv_dt_diff_mean,
date_int, var_max_lat, var_max_long, lon_plus_lat,
date_emb, air_area_name_emb, air_genre_name_emb,
air_area_code_emb, air_genre_code_emb, air_store_id_emb])
 m = Dense(hidden1_neurons, name='hidden1',
kernel_initializer=keras.initializers.RandomNormal(mean=0.0,
stddev=0.05, seed=None))(cat_layer)
 m = keras.layers.LeakyReLU(alpha=0.2 )(m)
 m = keras.layers.BatchNormalization()(m)
 m1 = Dense(hidden2_neurons, name='sub1' )(m)
 m1 = keras.layers.LeakyReLU(alpha=0.2 )(m1)
 m = Dense(1, activation='relu' )(m1)
 inp_ten = [
holiday_flg, min_visitors, mean_visitors, median_visitors, max_visitors, count_observations,
rs1_x, rv1_x, rs2_x, rv2_x, rs1_y, rv1_y, rs2_y, rv2_y, total_reserv_sum, total_reserv_mean,
total_reserv_dt_diff_mean, date_int, var_max_lat, var_max_long, lon_plus_lat,
dow, year, month, air_store_id, air_area_code, air_genre_code
]
inp_ten += air_area_name
inp_ten += air_genre_name
model = keras.Model(inp_ten, m)
 model.compile(loss='mse', optimizer='rmsprop', metrics=['acc'])
 return model<train_model>
%matplotlib inline
warnings.filterwarnings('ignore')
model1 = ensemble.GradientBoostingRegressor(learning_rate=0.2, random_state=3,
n_estimators=200, subsample=0.8, max_depth =10)
 model2 = neighbors.KNeighborsRegressor(n_jobs=-1, n_neighbors=4)
 model3 = XGBRegressor(learning_rate=0.2, random_state=3, n_estimators=200, subsample=0.8,
colsample_bytree=0.8, max_depth =10)
 model4 = get_nn_complete_model(train, hidden2_neurons=12)
 model1.fit(train[col], np.log1p(train['visitors'].values))
print("Model1 trained")
 model2.fit(train[col], np.log1p(train['visitors'].values))
print("Model2 trained")
 model3.fit(train[col], np.log1p(train['visitors'].values))
print("Model3 trained")
 for i in range(5):
model4.fit(nn_train[0], nn_train[1], epochs=3, verbose=1,
batch_size=256, shuffle=True, validation_split=0.15)
 model4.fit(nn_train[0], nn_train[1], epochs=8, verbose=0,
batch_size=256, shuffle=True)
 print("Model4 trained")
 preds1 = model1.predict(train[col])
 preds2 = model2.predict(train[col])
 preds3 = model3.predict(train[col])
 preds4 = pd.Series(model4.predict(nn_train[0] ).reshape(-1)).clip(0, 6.8 ).values
print('RMSE GradientBoostingRegressor: ', RMSLE(np.log1p(train['visitors'].values), preds1))
print('RMSE KNeighborsRegressor: ', RMSLE(np.log1p(train['visitors'].values), preds2))
print('RMSE XGBRegressor: ', RMSLE(np.log1p(train['visitors'].values), preds3))
print('RMSE NeuralNetwork: ', RMSLE(np.log1p(train['visitors'].values), preds4))
preds1 = model1.predict(test[col])
 preds2 = model2.predict(test[col])
 preds3 = model3.predict(test[col])
 preds4 = pd.Series(model4.predict(nn_test[0] ).reshape(-1)).clip(0, 6.8 ).values
test['visitors'] = 0.2*preds1+0.2*preds2+0.3*preds3+0.3*preds4
test['visitors'] = np.expm1(test['visitors'] ).clip(lower=0.)
sub1 = test[['id','visitors']].copy()
print("Model predictions done.")
<feature_engineering>
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]
dfs = { re.search('/([^/\.]*)\.csv', fn ).group(1):
pd.read_csv(fn)for fn in glob.glob('.. /input/*.csv')}
for k, v in dfs.items() : locals() [k] = v
wkend_holidays = date_info.apply(
(lambda x:(x.day_of_week=='Sunday' or x.day_of_week=='Saturday')and x.holiday_flg==1), axis=1)
 date_info.loc[wkend_holidays, 'holiday_flg'] = 0
date_info['weight'] =(( date_info.index + 1)/ len(date_info)) ** 5
visit_data = air_visit_data.merge(date_info, left_on='visit_date', right_on='calendar_date', how='left')
 visit_data.drop('calendar_date', axis=1, inplace=True)
 visit_data['visitors'] = visit_data.visitors.map(pd.np.log1p)
 wmean = lambda x:(( x.weight * x.visitors ).sum() / x.weight.sum())
 visitors = visit_data.groupby(['air_store_id', 'day_of_week', 'holiday_flg'] ).apply(wmean ).reset_index()
visitors.rename(columns={0:'visitors'}, inplace=True)
 sample_submission['air_store_id'] = sample_submission.id.map(lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
 sample_submission.drop('visitors', axis=1, inplace=True)
 sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
 sample_submission = sample_submission.merge(visitors, on=[
'air_store_id', 'day_of_week', 'holiday_flg'], how='left')
 sample_submission['visitors'] = sample_submission.visitors.map(pd.np.expm1)
 sub2 = sample_submission[['id', 'visitors']].copy()
sub2 = sub2.fillna(-1 )<save_to_csv>
train_df.groupby('Pclass')['Survived'].mean()
def final_visitors(x, alt=False):
visitors_x, visitors_y = x['visitors_x'], x['visitors_y']
if x['visitors_y'] == -1:
return visitors_x
else:
return 0.7*visitors_x + 0.3*visitors_y* 1.1
sub_merge = pd.merge(sub1, sub2, on='id', how='inner')
 sub_merge['visitors'] = sub_merge.apply(lambda x: final_visitors(x), axis=1)
 print("Done")
 sub_merge[['id', 'visitors']].to_csv('submission.csv', index=False )<feature_engineering>
train_df.groupby('Embarked')['Survived'].mean()
dfs = { re.search('/([^/\.]*)\.csv', fn ).group(1):pd.read_csv(fn)for fn in glob.glob('.. /input/*.csv')}
print('data frames read:{}'.format(list(dfs.keys())))
 print('local variables with the same names are created.')
 for k, v in dfs.items() : locals() [k] = v
print('holidays at weekends are not special, right?')
 wkend_holidays = date_info.apply(( lambda x:(x.day_of_week=='Sunday' or x.day_of_week=='Saturday')and x.holiday_flg==1), axis=1)
 date_info.loc[wkend_holidays, 'holiday_flg'] = 0
print('add decreasing weights from now')
 date_info['weight'] =(( date_info.index + 1)/ len(date_info)) ** 5<merge>
train_df.groupby('Sex')['Survived'].mean()
print('weighted mean visitors for each(air_store_id, day_of_week, holiday_flag)or(air_store_id, day_of_week)')
 visit_data = air_visit_data.merge(date_info, left_on='visit_date', right_on='calendar_date', how='left')
 visit_data.drop('calendar_date', axis=1, inplace=True )<data_type_conversions>
train_df.groupby('SibSp')['Survived'].mean().sort_values(ascending=False )
visit_data['visit_date'] = pd.to_datetime(visit_data['visit_date'] )<split>
train_df.groupby('Parch')['Survived'].mean().sort_values(ascending=False )
test_data = visit_data[visit_data['visit_date']>='2017-04-01']<filter>
train_df.drop(columns=['Cabin', 'Ticket', 'PassengerId'], inplace=True)
 test_df.drop(columns=['Cabin', 'Ticket'], inplace=True )
train_data = visit_data[visit_data['visit_date'] >='2017-03-01']<prepare_x_and_y>
for dataset in combine:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
y_train_data = visit_data.loc[visit_data['visit_date']<'2017-04-01','visitors']
y_test_data = visit_data.loc[visit_data['visit_date']>='2017-04-01','visitors'] 
<feature_engineering>
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()
train_data['visitors'] = train_data.visitors.map(pd.np.log1p)
 wmean = lambda x:(( x.weight * x.visitors ).sum() / x.weight.sum())
 visitors = train_data.groupby(['air_store_id', 'day_of_week', 'holiday_flg'] ).apply(wmean ).reset_index()
visitors.rename(columns={0:'visitors'}, inplace=True )<merge>
title_mapping = {'Mrs': 1, 'Miss': 2, 'Master': 3, 'Mr': 4, 'Rare': 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping )
test_data['visitors_predict'] = test_data.merge(visitors[visitors.holiday_flg==0], \
on=('air_store_id', 'day_of_week'), how='left')['visitors_y']<merge>
train_df.drop(columns=['Name'], inplace=True)
 test_df.drop(columns=['Name'], inplace=True )
missings = test_data.visitors_predict.isnull()
test_data.loc[missings, 'visitors_predict'] = test_data[missings].merge(
 visitors[['air_store_id', 'visitors']].groupby('air_store_id' ).\
mean().reset_index() , on='air_store_id', how='left')['visitors_y'].values 
<feature_engineering>
gender_mapping = {'female': 1, 'male': 0}
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map(gender_mapping ).astype(int )
test_data['visitors'] = test_data['visitors'].map(np.log1p )<filter>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df.groupby('FamilySize')['Survived'].mean()
test_data[test_data['visitors_predict'].isnull() ]<import_modules>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
import sklearn.metrics<compute_train_metric>
train_df['IsAlone'].value_counts()
sklearn.metrics.mean_squared_error(
 np.array([2.3]),np.array([3]))<compute_test_metric>
train_df.drop(columns=['SibSp', 'Parch', 'FamilySize'], inplace=True)
 test_df.drop(columns=['SibSp', 'Parch', 'FamilySize'], inplace=True )
sklearn.metrics.mean_squared_error(\
test_data.loc[~test_data.visitors_predict.isnull() ,'visitors']
,test_data.loc[~test_data.visitors_predict.isnull() ,'visitors_predict'] )<compute_test_metric>
train_df.groupby('Embarked')['Survived'].mean()
sklearn.metrics.mean_squared_error(\
test_data['visitors']
,test_data['visitors_predict'] )<merge>
port_mapping = {'S': 0, 'Q': 1, 'C': 2}
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map(port_mapping)
 train_df.info()
print('prepare to merge with date_info and visitors')
 sample_submission['air_store_id'] = sample_submission.id.map(lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
 sample_submission.drop('visitors', axis=1, inplace=True)
 sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
 sample_submission = sample_submission.merge(visitors, on=['air_store_id', 'day_of_week', 'holiday_flg'], how='left')
 missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
 visitors[visitors.holiday_flg==0], on=('air_store_id', 'day_of_week'), how='left')['visitors_y'].values
missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
 visitors[['air_store_id', 'visitors']].groupby('air_store_id' ).mean().reset_index() , on='air_store_id', how='left')['visitors_y'].values
sample_submission['visitors'] = sample_submission.visitors.map(pd.np.expm1)
 sample_submission[['id', 'visitors']].to_csv('dumb_result.csv', float_format='%.4f', index=None)
 print("done" )<feature_engineering>
train_df['Embarked'].value_counts()
dfs = { re.search('/([^/\.]*)\.csv', fn ).group(1):pd.read_csv(fn)for fn in glob.glob('.. /input/*.csv')}
print('data frames read:{}'.format(list(dfs.keys())))
 print('local variables with the same names are created.')
 for k, v in dfs.items() : locals() [k] = v<merge>
train_df['Embarked'].fillna(value=0, inplace=True )
print('holidays at weekends are not special, right?')
 wkend_holidays = date_info.apply(( lambda x:(x.day_of_week=='Sunday' or x.day_of_week=='Saturday')and x.holiday_flg==1), axis=1)
 date_info.loc[wkend_holidays, 'holiday_flg'] = 0
print('add decreasing weights from now')
 date_info['weight'] =(( date_info.index + 1)/ len(date_info)) ** 5
print('weighted mean visitors for each(air_store_id, day_of_week, holiday_flag)or(air_store_id, day_of_week)')
 visit_data = air_visit_data.merge(date_info, left_on='visit_date', right_on='calendar_date', how='left')
 visit_data.drop('calendar_date', axis=1, inplace=True)
 visit_data['visitors'] = visit_data.visitors.map(pd.np.log1p)
 wmean = lambda x:(( x.weight * x.visitors ).sum() / x.weight.sum())
 visitors = visit_data.groupby(['air_store_id', 'day_of_week', 'holiday_flg'] ).apply(wmean ).reset_index()
visitors.rename(columns={0:'visitors'}, inplace=True)
 print('prepare to merge with date_info and visitors')
 sample_submission['air_store_id'] = sample_submission.id.map(lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
 sample_submission.drop('visitors', axis=1, inplace=True)
 sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
 sample_submission = sample_submission.merge(visitors, on=['air_store_id', 'day_of_week', 'holiday_flg'], how='left')
<feature_engineering>
train_df['Embarked'] = train_df['Embarked'].apply(lambda x: int(x))
ora1= sample_submission
print('split date')
 ora1['year'] = ora1.calendar_date.map(lambda x:(x.split('-')[0]))
ora1['month'] = ora1.calendar_date.map(lambda x:(x.split('-')[1]))
ora1['day'] = ora1.calendar_date.map(lambda x:(x.split('-')[2]))
ora1['mhend_flg'] = ora1.day.map(lambda x: 1 if int(x)>=25 else 0)
 ora1=pd.get_dummies(ora1, columns = ['day_of_week'])
 ora1=pd.get_dummies(ora1, columns = ['air_store_id'])
 ora1.head()<prepare_x_and_y>
test_df['Fare'].fillna(value=test_df['Fare'].median() , inplace=True )
missings = ora1.visitors.isnull()
test = ora1[missings].copy()
test.drop(['visitors'], axis=1, inplace=True)
 test.drop(['id'], axis=1, inplace=True)
 test.drop(['calendar_date'], axis=1, inplace=True)
 x = ora1[-missings].copy()
y = x['visitors'].copy()
x.drop(['visitors'], axis=1, inplace=True)
 x.drop(['id'], axis=1, inplace=True)
 x.drop(['calendar_date'], axis=1, inplace=True)
 x.shape 
<train_model>
guess_ages = np.zeros(( 2,3))
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train_df.head()
model = RandomForestRegressor(max_depth = 1000,
max_features =834,
min_samples_split = 20,
n_estimators = 50,
n_jobs = -1,
random_state = 0)
 model.fit(x, y)
 output = model.predict(test)
 model.score(x, y )<save_to_csv>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
train_df.head()
ora1.loc[missings,'visitors'] =output
ora1['visitors'] = ora1.visitors.map(pd.np.expm1)
 sub = pd.DataFrame({ 'id': ora1['id'],
'visitors':ora1['visitors'] })
 sub.to_csv('random_result.csv', float_format='%.4f', index=None)
 print("done")
 ora1.head()<define_variables>
train_df.drop(columns=['AgeBand'], inplace=True )
The first is a w<feature_engineering>
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31.0), 'Fare'] = 2
dataset.loc[(dataset['Fare'] > 31.0)&(dataset['Fare'] <= 512.329), 'Fare'] = 3
train_df.head()
dfs = { re.search('/([^/\.]*)\.csv', fn ).group(1):pd.read_csv(fn)for fn in glob.glob('.. /input/*.csv')}
print('data frames read:{}'.format(list(dfs.keys())))
 print('local variables with the same names are created.')
 for k, v in dfs.items() : locals() [k] = v
print('holidays at weekends are not special, right?')
 wkend_holidays = date_info.apply(( lambda x:(x.day_of_week=='Sunday' or x.day_of_week=='Saturday')and x.holiday_flg==1), axis=1)
 date_info.loc[wkend_holidays, 'holiday_flg'] = 0
print('add decreasing weights from now')
 date_info['weight'] =(( date_info.index + 1)/ len(date_info)) ** 8<merge>
train_df.drop(columns=['FareBand'], inplace=True )
print('weighted mean visitors for each(air_store_id, day_of_week, holiday_flag)or(air_store_id, day_of_week)')
 visit_data = air_visit_data.merge(date_info, left_on='visit_date', right_on='calendar_date', how='left')
 visit_data.drop('calendar_date', axis=1, inplace=True)
 visit_data['visitors'] = visit_data.visitors.map(pd.np.log1p)
 wmean = lambda x:(( x.weight * x.visitors ).sum() / x.weight.sum())
 visitors = visit_data.groupby(['air_store_id', 'day_of_week', 'holiday_flg'] ).apply(wmean ).reset_index()
visitors.rename(columns={0:'visitors'}, inplace=True)
 print('prepare to merge with date_info and visitors')
 sample_submission['air_store_id'] = sample_submission.id.map(lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
 sample_submission.drop('visitors', axis=1, inplace=True)
 sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
 sample_submission = sample_submission.merge(visitors, on=['air_store_id', 'day_of_week', 'holiday_flg'], how='left')
 missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
 visitors[visitors.holiday_flg==0], on=('air_store_id', 'day_of_week'), how='left')['visitors_y'].values
missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
 visitors[['air_store_id', 'visitors']].groupby('air_store_id' ).mean().reset_index() , on='air_store_id', how='left')['visitors_y'].values
sample_submission['visitors'] = sample_submission.visitors.map(pd.np.expm1)
 print("done" )<create_dataframe>
for dataset in combine:
dataset['Fare'] = dataset['Fare'].astype(int )
sample_submission2 = sample_submission.copy()<load_from_csv>
for dataset in combine:
dataset['Age*Pclass'] = dataset['Age'] * dataset['Pclass']
test_df = pd.read_csv('.. /input/sample_submission.csv')
 test_df['store_id'], test_df['visit_date'] = test_df['id'].str[:20], test_df['id'].str[21:]
test_df.drop(['visitors'], axis=1, inplace=True)
 test_df['visit_date'] = pd.to_datetime(test_df['visit_date'])
 air_data = pd.read_csv('.. /input/air_visit_data.csv', parse_dates=['visit_date'])
 air_data['dow'] = air_data['visit_date'].dt.dayofweek
train = air_data[air_data['visit_date'] > '2017-01-28'].reset_index()
train['dow'] = train['visit_date'].dt.dayofweek
test_df['dow'] = test_df['visit_date'].dt.dayofweek
aggregation = {'visitors' :{'total_visitors' : 'median'}}
agg_data = train.groupby(['air_store_id', 'dow'] ).agg(aggregation ).reset_index()
agg_data.columns = ['air_store_id','dow','visitors']
agg_data['visitors'] = agg_data['visitors']
merged = pd.merge(test_df, agg_data, how='left', left_on=[
'store_id','dow'], right_on=['air_store_id','dow'])
 final = merged[['id','visitors']]
final.fillna(0, inplace=True)
 dfs = { re.search('/([^/\.]*)\.csv', fn ).group(1):pd.read_csv(
 fn)for fn in glob.glob('.. /input/*.csv')}
for k, v in dfs.items() : locals() [k] = v
weekend_hdays = date_info.apply(
(lambda x:(x.day_of_week=='Sunday' or x.day_of_week=='Saturday')
 and x.holiday_flg==1), axis=1)
 date_info.loc[weekend_hdays, 'holiday_flg'] = 0
date_info['weight'] =(date_info.index + 1)/ len(date_info)
 visit_data = air_visit_data.merge(
 date_info, left_on='visit_date', right_on='calendar_date', how='left')
 visit_data.drop('calendar_date', axis=1, inplace=True)
 visit_data['visitors'] = visit_data.visitors.map(pd.np.log1p)
 wmean = lambda x:(( x.weight * x.visitors ).sum() / x.weight.sum())
 visitors = visit_data.groupby(
 ['air_store_id', 'day_of_week', 'holiday_flg'] ).apply(wmean ).reset_index()
visitors.rename(columns={0:'visitors'}, inplace=True)
 sample_submission['air_store_id'] = sample_submission.id.map(
 lambda x: '_'.join(x.split('_')[:-1]))
sample_submission['calendar_date'] = sample_submission.id.map(lambda x: x.split('_')[2])
 sample_submission.drop('visitors', axis=1, inplace=True)
 sample_submission = sample_submission.merge(date_info, on='calendar_date', how='left')
 sample_submission = sample_submission.merge(
 visitors, on=['air_store_id', 'day_of_week', 'holiday_flg'], how='left')
 missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
 visitors[visitors.holiday_flg==0], on=(
 'air_store_id', 'day_of_week'), how='left')['visitors_y'].values
missings = sample_submission.visitors.isnull()
sample_submission.loc[missings, 'visitors'] = sample_submission[missings].merge(
 visitors[['air_store_id', 'visitors']].groupby('air_store_id' ).mean().reset_index() ,
on='air_store_id', how='left')['visitors_y'].values
sample_submission['visitors'] = sample_submission.visitors.map(pd.np.expm1)
 sample_submission = sample_submission[['id', 'visitors']]
final['visitors'][final['visitors'] ==0] = sample_submission['visitors'][final['visitors'] ==0]
sub_file = final.copy()
sub_file['visitors'] = 2/(1/final['visitors'] + 1/sample_submission['visitors'] )<create_dataframe>
y = train_df['Survived']
X = train_df.drop('Survived', axis=1)
new = sub_file.copy()<feature_engineering>
X_train, X_val, y_train, y_val = train_test_split(X, y )
new['visitors'] = sub_file['visitors']*.2 + sample_submission2['visitors']*.8<save_to_csv>
linreg = LogisticRegression(random_state=1)
 linreg.fit(X_train, y_train)
new.to_csv("four_kernel_weighted.csv", float_format='%.4f', index=None )<import_modules>
predictions = linreg.predict(X_val)
 accuracy_score(y_val, predictions)
 round(linreg.score(X_train, y_train), 3), round(linreg.score(X_val, y_val), 3 )
import lightgbm
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_absolute_error, mean_squared_error<categorify>
X_test = test_df.drop(columns=['PassengerId'] )
def match_normalize(match):
match = match.groupby('groupId' ).mean()
for head in match.columns.values:
if head != 'winPlacePerc':
match[head] /= match[head].max()
match = match.drop(columns=['maxPlace', 'numGroups'])
 return match
def rearrange(t_order, p_orders):
flag = True
while flag:
flag = False
for p_order in p_orders:
for i in range(len(p_order)-1):
if t_order.index(p_order[i])> t_order.index(p_order[i+1]):
t_order.remove(p_order[i])
 t_order.insert(t_order.index(p_order[i+1]), p_order[i])
 flag = True
return t_order<load_from_csv>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
 D_train = xgb.DMatrix(X_train, label=y_train)
 D_test = xgb.DMatrix(X_val, label=y_val)
 clf = xgb.XGBClassifier()
parameters = {
"eta" : [0.05, 0.10, 0.16, 0.13, 0.14, 0.15,] ,
"max_depth" : [ 3, 4, 5, 6, 8, 10, 12, 15],
"min_child_weight" : [ 3, 5, 7],
"gamma" : [ 0.05, 0.1, 0.15 , 0.2, 0.5],
"colsample_bytree" : [ 0.8, 0.9, 0.6 , 0.7 ],
}
grid = GridSearchCV(clf, parameters, n_jobs=-1, cv=3)
 grid.fit(X_train, y_train)
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv' ).dropna()<groupby>
grid.best_params_
train = train.drop(['swimDistance', 'headshotKills', 'killPoints', 'matchDuration', 'rankPoints', 'teamKills', 'winPoints'], axis=1)
 train = train.groupby('matchId')
 train_groupwise = train.apply(match_normalize )<prepare_x_and_y>
param_3 = {'colsample_bytree': 0.7,
'eta': 0.14,
'gamma': 0.15,
'max_depth': 6,
'min_child_weight': 5}
train_matchId = list(train.groups.keys())
 split = train_groupwise.index.get_loc(train_matchId[round(len(train_matchId)* 0.8)] ).start
train_X = train_groupwise.drop('winPlacePerc', axis=1 ).iloc[:split]
train_Y = train_groupwise['winPlacePerc'].iloc[:split]
valid_X = train_groupwise.drop('winPlacePerc', axis=1 ).iloc[split:]
valid_Y = train_groupwise['winPlacePerc'].iloc[split:]<train_model>
D_train = xgb.DMatrix(X_train, label=y_train)
 D_test = xgb.DMatrix(X_val, label=y_val)
 param = {
'eta': 0.1,
'max_depth': 3,
'objective': 'multi:softprob',
'num_class': 3}
steps = 20
model = xgb.train(param, D_train, steps)
 preds = model.predict(D_test)
 best_preds = np.asarray([np.argmax(line)for line in preds])
 print("Precision = {}".format(precision_score(y_val, best_preds, average='macro')))
 print("Recall = {}".format(recall_score(y_val, best_preds, average='macro')))
 print("Accuracy = {}".format(accuracy_score(y_val, best_preds)))
model = lightgbm.LGBMRegressor()
model.fit(train_X, train_Y, eval_set=(valid_X, valid_Y), eval_metric='l1' )<load_from_csv>
D_test = xgb.DMatrix(X_test)
 param = {
'eta': 0.1,
'max_depth': 3,
'objective': 'multi:softprob',
'num_class': 3}
steps = 20
model = xgb.train(param, D_train, steps)
 preds = model.predict(D_test )
test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv' )<groupby>
predictions = []
for pred in preds:
result = max(pred)
 if result > 0.5:
result = 1
else:
result = 0
predictions.append(result )
test = test.drop(['swimDistance', 'headshotKills', 'killPoints', 'matchDuration', 'rankPoints', 'teamKills', 'winPoints'], axis=1)
 test = test.groupby('matchId')
 test_groupwise = test.apply(match_normalize )<predict_on_test>
output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False )