Kaggle competition: Titanic - Machine Learning from Disaster
grouped_by_user_df = train_questions_only_df.groupby('user_id')
 user_answers_df = grouped_by_user_df.agg({'answered_correctly': ['mean', 'count', 'sum']} ).copy()
user_answers_df.columns = [
'user_mean_accuracy',
'user_questions_answered',
'user_questions_correct',
]
user_answers_df.user_questions_correct = user_answers_df.user_questions_correct.astype('int32')
 user_answers_df<categorify>
train_df=pd.read_csv('.. /input/train.csv')
 gen_df=pd.read_csv('.. /input/gender_submission.csv')
 test_df=pd.read_csv('.. /input/test.csv')
 data_arr=[train_df,test_df]
user_lagtime_max_dict = grouped_by_user_df.agg({'lag_time': ['max']} ).copy()
user_lagtime_max_dict.columns = [
'user_lag_time_max',
]
user_lagtime_max_dict = user_lagtime_max_dict.to_dict('index')
 for pair in grouped_by_user_df.tail(1)[['user_id','lag_time']].values:
user_lagtime_max_dict[pair[0]]['last_lagtime'] = pair[1]
gc.collect()<groupby>
colms=[col for col in train_df.columns if train_df[col].isnull().any() ]
grouped_by_tags_df = train_questions_only_df.groupby('tags')
 tags_answers_df = grouped_by_tags_df.agg({'answered_correctly': ['mean', 'count', 'std', 'skew']} ).copy()
tags_answers_df.columns = [
'tags_mean_accuracy',
'tags_question_asked',
'tags_std_accuracy',
'tags_skew_accuracy'
]
tags_answers_df<groupby>
train_df['Cabin']=pd.Series([i[0] if pd.notnull(i)else 'X' for i in train_df['Cabin'] ])
 train_df['Cabin'].replace('T','X',inplace=True)
 test_df['Cabin']=pd.Series([i[0] if pd.notnull(i)else 'X' for i in test_df['Cabin'] ] )
grouped_by_part_df = train_questions_only_df.groupby('part')
 part_answers_df = grouped_by_part_df.agg({'answered_correctly': ['mean', 'count']} ).copy()
part_answers_df.columns = [
'part_mean_accuracy',
'part_questions_answered',
]
part_answers_df<drop_column>
print(train_df.Cabin.value_counts())
del grouped_by_user_df
del grouped_by_tags_df
del grouped_by_part_df<load_pretrained>
for data in data_arr:
data['Title']=data.Name.str.split(', ',expand=True)[1].str.split('.',expand=True)[0]
title_cnt=data.Title.value_counts() <10
data.Title=data.Title.apply(lambda x: x if title_cnt[x]==False else 'Misc')
content_answers_df = pd.read_pickle('.. /input/riiid-content-answers-df-preprocessing/content_answers_df.pkl.zip')
 content_answers_df<define_variables>
med_age=pd.DataFrame()
def fill_age(cols):
pclass=cols[0]
sex=cols[1]
age=cols[2]
title=cols[3]
if pd.isnull(age):
return med_age[(med_age['Pclass']==pclass)&(med_age['Title']==title)&(med_age['Sex']==sex)]['Age']
else:
return age
features = [
'user_mean_accuracy',
'user_questions_answered',
'user_questions_correct',
'q_mean_accuracy',
'q_question_asked',
'q_question_correct',
'community',
'num_in_bundle',
'tags_mean_accuracy',
'tags_question_asked',
'tags_std_accuracy',
'tags_skew_accuracy',
'part_mean_accuracy',
'part_questions_answered',
'prior_question_elapsed_time',
'part_1',
'part_2',
'part_3',
'part_4',
'part_5',
'part_6',
'part_7',
'type_of_concept',
'type_of_intention',
'type_of_solving_question',
'lectures_seen',
'seen_before',
'avg_q_time',
'lag_time',
]
target = 'answered_correctly'<drop_column>
train_df.drop(['PassengerId','Ticket','Name','Fare','Age','SibSp','Parch'],axis=1,inplace=True)
 test_df.drop(['Ticket','Name','Fare','Age','Parch','SibSp'],axis=1,inplace=True )
del train_questions_only_df<categorify>
train_df=pd.get_dummies(train_df,columns=['Sex','Embarked','Pclass','Title','AgeBin','FareBin','Cabin'],drop_first=True)
 test_df=pd.get_dummies(test_df,columns=['Sex','Embarked','Pclass','Title','AgeBin','FareBin','Cabin'],drop_first=True )
def add_and_update_user_lects(user_lecture_stats_part, lectures_df, train_df):
lect_dict = lectures_df.to_dict('index')
 lect_stats_part_dict = user_lecture_stats_part.to_dict('index')
 part_1_list = []
part_2_list = []
part_3_list = []
part_4_list = []
part_5_list = []
part_6_list = []
part_7_list = []
type_of_concept_list = []
type_of_intention_list = []
type_of_solving_question_list = []
type_of_starter_list = []
lectures_seen_list = []
for pair in tqdm(train_df[['content_id','user_id','answered_correctly']].values):
if pair[1] in lect_stats_part_dict:
if pair[2]!=-1:
part_1_list.append(lect_stats_part_dict[pair[1]]['part_1'])
 part_2_list.append(lect_stats_part_dict[pair[1]]['part_2'])
 part_3_list.append(lect_stats_part_dict[pair[1]]['part_3'])
 part_4_list.append(lect_stats_part_dict[pair[1]]['part_4'])
 part_5_list.append(lect_stats_part_dict[pair[1]]['part_5'])
 part_6_list.append(lect_stats_part_dict[pair[1]]['part_6'])
 part_7_list.append(lect_stats_part_dict[pair[1]]['part_7'])
 type_of_concept_list.append(lect_stats_part_dict[pair[1]]['type_of_concept'])
 type_of_intention_list.append(lect_stats_part_dict[pair[1]]['type_of_intention'])
 type_of_solving_question_list.append(lect_stats_part_dict[pair[1]]['type_of_solving_question'])
 type_of_starter_list.append(lect_stats_part_dict[pair[1]]['type_of_starter'])
 lectures_seen_list.append(lect_stats_part_dict[pair[1]]['lectures_seen'])
 else:
part_1_list.append(lect_stats_part_dict[pair[1]]['part_1'])
 part_2_list.append(lect_stats_part_dict[pair[1]]['part_2'])
 part_3_list.append(lect_stats_part_dict[pair[1]]['part_3'])
 part_4_list.append(lect_stats_part_dict[pair[1]]['part_4'])
 part_5_list.append(lect_stats_part_dict[pair[1]]['part_5'])
 part_6_list.append(lect_stats_part_dict[pair[1]]['part_6'])
 part_7_list.append(lect_stats_part_dict[pair[1]]['part_7'])
 type_of_concept_list.append(lect_stats_part_dict[pair[1]]['type_of_concept'])
 type_of_intention_list.append(lect_stats_part_dict[pair[1]]['type_of_intention'])
 type_of_solving_question_list.append(lect_stats_part_dict[pair[1]]['type_of_solving_question'])
 type_of_starter_list.append(lect_stats_part_dict[pair[1]]['type_of_starter'])
 lectures_seen_list.append(lect_stats_part_dict[pair[1]]['lectures_seen'])
 lect_stats_part_dict[pair[1]]['part_1'] += lect_dict[pair[0]]['part_1']
lect_stats_part_dict[pair[1]]['part_2'] += lect_dict[pair[0]]['part_2']
lect_stats_part_dict[pair[1]]['part_3'] += lect_dict[pair[0]]['part_3']
lect_stats_part_dict[pair[1]]['part_4'] += lect_dict[pair[0]]['part_4']
lect_stats_part_dict[pair[1]]['part_5'] += lect_dict[pair[0]]['part_5']
lect_stats_part_dict[pair[1]]['part_6'] += lect_dict[pair[0]]['part_6']
lect_stats_part_dict[pair[1]]['part_7'] += lect_dict[pair[0]]['part_7']
lect_stats_part_dict[pair[1]]['type_of_concept'] += lect_dict[pair[0]]['type_of_concept']
lect_stats_part_dict[pair[1]]['type_of_intention'] += lect_dict[pair[0]]['type_of_intention']
lect_stats_part_dict[pair[1]]['type_of_solving_question'] += lect_dict[pair[0]]['type_of_solving_question']
lect_stats_part_dict[pair[1]]['type_of_starter'] += lect_dict[pair[0]]['type_of_starter']
lect_stats_part_dict[pair[1]]['lectures_seen'] += 1
else:
part_1_list.append(0)
 part_2_list.append(0)
 part_3_list.append(0)
 part_4_list.append(0)
 part_5_list.append(0)
 part_6_list.append(0)
 part_7_list.append(0)
 type_of_concept_list.append(0)
 type_of_intention_list.append(0)
 type_of_solving_question_list.append(0)
 type_of_starter_list.append(0)
 lectures_seen_list.append(0)
 if pair[2]==-1:
lect_stats_part_dict[pair[1]]={}
lect_stats_part_dict[pair[1]]['part_1'] = lect_dict[pair[0]]['part_1']
lect_stats_part_dict[pair[1]]['part_2'] = lect_dict[pair[0]]['part_2']
lect_stats_part_dict[pair[1]]['part_3'] = lect_dict[pair[0]]['part_3']
lect_stats_part_dict[pair[1]]['part_4'] = lect_dict[pair[0]]['part_4']
lect_stats_part_dict[pair[1]]['part_5'] = lect_dict[pair[0]]['part_5']
lect_stats_part_dict[pair[1]]['part_6'] = lect_dict[pair[0]]['part_6']
lect_stats_part_dict[pair[1]]['part_7'] = lect_dict[pair[0]]['part_7']
lect_stats_part_dict[pair[1]]['type_of_concept'] = lect_dict[pair[0]]['type_of_concept']
lect_stats_part_dict[pair[1]]['type_of_intention'] = lect_dict[pair[0]]['type_of_intention']
lect_stats_part_dict[pair[1]]['type_of_solving_question'] = lect_dict[pair[0]]['type_of_solving_question']
lect_stats_part_dict[pair[1]]['type_of_starter'] = lect_dict[pair[0]]['type_of_starter']
lect_stats_part_dict[pair[1]]['lectures_seen'] = 1
train_df['part_1'] = part_1_list
train_df['part_2'] = part_2_list
train_df['part_3'] = part_3_list
train_df['part_4'] = part_4_list
train_df['part_5'] = part_5_list
train_df['part_6'] = part_6_list
train_df['part_7'] = part_7_list
train_df['type_of_concept'] = type_of_concept_list
train_df['type_of_intention'] = type_of_intention_list
train_df['type_of_solving_question'] = type_of_solving_question_list
train_df['type_of_starter'] = type_of_starter_list
train_df['lectures_seen'] = lectures_seen_list
lect_stats_part = pd.DataFrame.from_dict(lect_stats_part_dict, orient='index')
 return(lect_stats_part, train_df )<split>
y=train_df['Survived']
X=train_df.iloc[:,1:]
PassengerId=test_df['PassengerId']
test_df.drop(labels=['PassengerId'],inplace=True,axis=1 )
def add_and_update_user_stats(user_answers_df, train_df):
my_dict=user_answers_df.to_dict('index')
 user_acc_list=[]
user_answered_list=[]
user_correct_list=[]
for pair in tqdm(train_df[['user_id','answered_correctly']].values):
if pair[0] in my_dict:
user_acc_list.append(my_dict[pair[0]]['user_mean_accuracy'])
 user_answered_list.append(my_dict[pair[0]]['user_questions_answered'])
 user_correct_list.append(my_dict[pair[0]]['user_questions_correct'])
 my_dict[pair[0]]['user_questions_answered']+=1
my_dict[pair[0]]['user_questions_correct']+=pair[1]
my_dict[pair[0]]['user_mean_accuracy'] = my_dict[pair[0]]['user_questions_correct']/my_dict[pair[0]]['user_questions_answered']
else:
my_dict[pair[0]]={'user_mean_accuracy':0.645,'user_questions_answered': 1, 'user_questions_correct': pair[1]}
user_acc_list.append(0.645)
 user_answered_list.append(0)
 user_correct_list.append(0)
 train_df['user_mean_accuracy']=user_acc_list
train_df['user_questions_answered']=user_answered_list
train_df['user_questions_correct']=user_correct_list
user_answers_df = pd.DataFrame.from_dict(my_dict, orient='index')
 return(user_answers_df, train_df )<groupby>
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.20,random_state=1 )
def add_and_update_content_stats(content_answers_df, train_df):
my_dict=content_answers_df.to_dict('index')
 q_mean_accuracy_list=[]
q_question_asked=[]
q_question_correct=[]
community_list=[]
num_in_bundle_list=[]
avg_q_time_list=[]
for pair in tqdm(train_df[['content_id','answered_correctly']].values):
q_mean_accuracy_list.append(my_dict[pair[0]]['q_mean_accuracy'])
 q_question_asked.append(my_dict[pair[0]]['q_question_asked'])
 q_question_correct.append(my_dict[pair[0]]['q_question_correct'])
 community_list.append(my_dict[pair[0]]['community'])
 num_in_bundle_list.append(my_dict[pair[0]]['num_in_bundle'])
 avg_q_time_list.append(my_dict[pair[0]]['avg_q_time'])
 my_dict[pair[0]]['q_question_asked']+=1
my_dict[pair[0]]['q_question_correct']+=pair[1]
my_dict[pair[0]]['q_mean_accuracy'] = my_dict[pair[0]]['q_question_correct']/my_dict[pair[0]]['q_question_asked']
train_df['q_mean_accuracy'] = q_mean_accuracy_list
train_df['q_question_asked'] = q_question_asked
train_df['q_question_correct'] = q_question_correct
train_df['community'] = community_list
train_df['num_in_bundle'] = num_in_bundle_list
train_df['avg_q_time'] = avg_q_time_list
content_answers_df = pd.DataFrame.from_dict(my_dict, orient='index')
 return(content_answers_df, train_df )<feature_engineering>
modelxgb=XGBClassifier(n_estimators=300,learning_rate=0.001,max_depth=4,n_jobs=4,)
 modelxgb.fit(X_train,y_train)
 ypred=modelxgb.predict(X_test)
 print(modelxgb.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
def update_user_max_timestamp(user_lagtime_max_dict, train_df):
for pair in train_df[['user_id','timestamp','lag_time']].values:
if pair[0] in user_lagtime_max_dict:
user_lagtime_max_dict[pair[0]]['user_lag_time_max'] = pair[1]
user_lagtime_max_dict[pair[0]]['last_lagtime'] = pair[2]
else:
user_lagtime_max_dict[pair[0]] = {}
user_lagtime_max_dict[pair[0]]['user_lag_time_max'] = pair[1]
user_lagtime_max_dict[pair[0]]['last_lagtime'] = pair[2]
return(user_lagtime_max_dict )<merge>
logmodel=LogisticRegression(max_iter=100)
 logmodel.fit(X_train,y_train)
 ypred=logmodel.predict(X_test)
 print(logmodel.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
%%time
lect_stats_part, train_df = add_and_update_user_lects(user_lecture_stats_part, lectures_df, train_df)
 train_df = train_df[train_df[target] != -1]
user_lagtime_max_dict = update_user_max_timestamp(user_lagtime_max_dict, train_df)
 user_answers_df, train_df = add_and_update_user_stats(user_answers_df, train_df)
 content_answers_df, train_df = add_and_update_content_stats(content_answers_df, train_df)
 train_df = pd.merge(train_df, questions[['part','tags']], left_on='content_id', right_index=True, how = 'left')
 train_df = train_df.merge(part_answers_df, how='left', left_on='part', right_index=True)
 train_df = train_df.merge(tags_answers_df, how='left', left_on='tags', right_index=True)
 train_df['part_1'].fillna(0, inplace = True)
 train_df['part_2'].fillna(0, inplace = True)
 train_df['part_3'].fillna(0, inplace = True)
 train_df['part_4'].fillna(0, inplace = True)
 train_df['part_5'].fillna(0, inplace = True)
 train_df['part_6'].fillna(0, inplace = True)
 train_df['part_7'].fillna(0, inplace = True)
 train_df['type_of_concept'].fillna(0, inplace = True)
 train_df['type_of_intention'].fillna(0, inplace = True)
 train_df['type_of_solving_question'].fillna(0, inplace = True)
 train_df['type_of_starter'].fillna(0, inplace = True)
 train_df['lectures_seen'].fillna(0, inplace = True)
 train_df['prior_question_elapsed_time'].fillna(25423, inplace = True)
 train_df['avg_q_time'].fillna(25423, inplace = True)
 train_df['lag_time'].fillna(26161758, inplace = True)
 train_df[types_of_lectures_columns + part_lectures_columns] = train_df[types_of_lectures_columns + part_lectures_columns].astype(int)
 train_df['lectures_seen'] = train_df['lectures_seen'].astype(int)
 train_df['prior_question_had_explanation'] = train_df['prior_question_had_explanation'].fillna(value=False ).astype(bool)
 train_df = train_df.fillna(value=0.5)
 train_df = train_df[features + [target]]
train_df = train_df.replace([np.inf, -np.inf], np.nan)
 train_df['prior_question_elapsed_time'].fillna(25423, inplace = True)
 train_df['avg_q_time'].fillna(25423, inplace = True)
 train_df['lag_time'].fillna(26161758, inplace = True)
 train_df = train_df.fillna(0 )<choose_model_class>
modelsvc=SVC(probability=True,gamma='auto')
 modelsvc.fit(X_train,y_train)
 ypred=modelsvc.predict(X_test)
 print(modelsvc.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
sampler = TPESampler(seed=314)
 def create_model(trial):
num_leaves = trial.suggest_int("num_leaves", 20, 40)
 n_estimators = trial.suggest_int("n_estimators", 50, 400)
 max_depth = trial.suggest_int('max_depth', 3, 8)
 min_child_samples = trial.suggest_int('min_child_samples', 100, 1200)
 learning_rate = trial.suggest_uniform('learning_rate', 0.0001, 0.30)
 min_data_in_leaf = trial.suggest_int('min_data_in_leaf', 5, 90)
 bagging_fraction = trial.suggest_uniform('bagging_fraction', 0.50, 1.0)
 feature_fraction = trial.suggest_uniform('feature_fraction', 0.50, 1.0)
 model = LGBMClassifier(
 num_leaves=num_leaves,
n_estimators=n_estimators,
max_depth=max_depth,
min_child_samples=min_child_samples,
min_data_in_leaf=min_data_in_leaf,
learning_rate=learning_rate,
feature_fraction=feature_fraction,
random_state=314 
)
 return model
def objective(trial):
model = create_model(trial)
 model.fit(train_df[features], train_df[target])
 score = roc_auc_score(test_df[target].values, model.predict_proba(test_df[features])[:,1])
 return score<train_on_grid>
dmodel=DecisionTreeClassifier()
dmodel.fit(X_train,y_train)
 ypred=dmodel.predict(X_test)
 print(dmodel.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
params = {'num_leaves': 30,
'n_estimators': 300,
'max_depth': 5,
'min_child_samples': 371,
'learning_rate': 0.28285171125399805,
'min_data_in_leaf': 23,
'bagging_fraction': 0.8057106694835638,
'feature_fraction': 0.5688885590495344,
}
model = LGBMClassifier(**params)
 model.fit(train_df[features], train_df[target])
<create_dataframe>
rmodel=RandomForestClassifier(n_estimators=50)
 rmodel.fit(X_train,y_train)
 ypred=rmodel.predict(X_test)
 print(rmodel.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
print(model.feature_importances_)
 print(train_df.columns[:-1])
 pd.DataFrame({'col_name': model.feature_importances_},
index=train_df.columns[:-1] ).sort_values(by='col_name', ascending=False )<load_pretrained>
amodel=AdaBoostClassifier(n_estimators=100)
 amodel.fit(X_train,y_train)
 ypred=amodel.predict(X_test)
 print(amodel.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
del train_df
all_data = pd.read_pickle('.. /input/riiid-train-df/train_df.pkl.gzip')
 all_data = all_data[all_data[target] != -1]<feature_engineering>
gmodel=GradientBoostingClassifier(n_estimators=100)
 gmodel.fit(X_train,y_train)
 ypred=gmodel.predict(X_test)
 print(gmodel.score(X_train,y_train))
print(confusion_matrix(y_test,ypred))
print(classification_report(y_test,ypred))
def get_me_all_seen_befores(all_data):
state = dict()
for user_id in all_data['user_id'].unique() :
state[user_id] = {}
total = len(state.keys())
 user_content = all_data.groupby('user_id')['content_id'].apply(np.array ).apply(np.sort ).apply(np.unique)
 user_attempts = all_data.groupby(['user_id', 'content_id'])['content_id'].count().astype(np.uint8 ).groupby('user_id' ).apply(np.array ).values
for user_id, content, attempt in tqdm(zip(state.keys() , user_content, user_attempts),total=total):
state[user_id] = dict(zip(content, attempt))
del user_content, user_attempts, all_data
return(state)
 state = get_me_all_seen_befores(all_data )<feature_engineering>
voting=VotingClassifier(estimators=[('logi',logmodel),('svc',modelsvc),('dtc',dmodel),('abc',amodel)],voting='soft',n_jobs=4 )
def update_content_stats(content_answers_df, previous_test_df):
for row in previous_test_df[['content_id','answered_correctly','content_type_id']].values:
if row[2] == 0:
content_answers_df.at[row[0],'q_question_correct'] += row[1]
content_answers_df.at[row[0],'q_question_asked'] += 1
content_answers_df['q_mean_accuracy']= content_answers_df['q_question_correct']/content_answers_df['q_question_asked']<feature_engineering>
voting=voting.fit(X_train,y_train )
def update_user_stats(user_answers_df, previous_test_df):
for row in previous_test_df[['user_id','answered_correctly','content_type_id']].values:
if row[2] == 0:
try:
user_answers_df.at[row[0],'user_questions_correct'] += row[1]
user_answers_df.at[row[0],'user_questions_answered'] += 1
except:
user_answers_df.at[row[0]]=(0,1,row[1])
 user_answers_df['user_mean_accuracy']= user_answers_df['user_questions_correct']/user_answers_df['user_questions_answered']<prepare_x_and_y>
pred1=voting.predict(test_df)
 print(confusion_matrix(gen_df.Survived,pred1))
print(classification_report(gen_df.Survived,pred1))
def update_lect_stats(user_lecture_stats_part, lectures_df, previous_test_df):
for row in previous_test_df[['user_id','content_type_id', 'content_id']].values:
if row[1] == 1:
y = lectures_df.query('lecture_id == {}'.format(row[2])).drop('tag', 1 ).reset_index(drop=True)
 y = y.loc[:,(y != 0 ).any(axis=0)]
if row[0] in user_lecture_stats_part.index:
for i in y.columns:
user_lecture_stats_part.at[row[0], i] +=1
user_lecture_stats_part.at[row[0], 'lectures_seen'] +=1
else:
user_lecture_stats_part.loc[row[0]] = 0
for i in y.columns:
user_lecture_stats_part.at[row[0], i] +=1
user_lecture_stats_part.at[row[0], 'lectures_seen'] +=1<feature_engineering>
prediction=modelsvc.predict(test_df )
def add_and_update_seen_before(train_df, state):
big_list=[]
for pair in train_df[['user_id','content_id','content_type_id']].values:
if pair[2] == 0:
if pair[0] in state:
if pair[1] in state[pair[0]]:
big_list.append(state[pair[0]][pair[1]])
 state[pair[0]][pair[1]]+=1
else:
big_list.append(0)
 state[pair[0]][pair[1]]=1
else:
big_list.append(0)
 state[pair[0]]={pair[1]:1}
else:
big_list.append(0)
 train_df['seen_before']= big_list
train_df.seen_before = train_df.seen_before.clip(upper=5)
 return(train_df, state )<feature_engineering>
print(confusion_matrix(gen_df.Survived,prediction))
print(classification_report(gen_df.Survived,prediction))
def add_and_update_lag_time(test_df, user_lagtime_max_dict):
lag_time_list = []
for pair in test_df[['user_id','timestamp']].values:
if pair[0] in user_lagtime_max_dict:
if pair[1] != user_lagtime_max_dict[pair[0]]['user_lag_time_max']:
lag_time_list.append(pair[1] -(user_lagtime_max_dict[pair[0]]['user_lag_time_max']))
user_lagtime_max_dict[pair[0]]['last_lagtime'] = pair[1] -(user_lagtime_max_dict[pair[0]]['user_lag_time_max'])
 user_lagtime_max_dict[pair[0]]['user_lag_time_max'] = pair[1]
else:
lag_time_list.append(user_lagtime_max_dict[pair[0]]['last_lagtime'])
 else:
lag_time_list.append(0)
 user_lagtime_max_dict[pair[0]] = {}
user_lagtime_max_dict[pair[0]]['user_lag_time_max'] = pair[1]
user_lagtime_max_dict[pair[0]]['last_lagtime'] = 0
test_df['lag_time'] = lag_time_list
return(test_df, user_lagtime_max_dict )<split>
sub=pd.DataFrame({'PassengerId':PassengerId,'Survived':prediction} )
<merge><EOS> 
sub.to_csv('Submission.csv',index=False )