Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<rename_columns>
warnings.filterwarnings('ignore' )
content_explation_agg=content_explation_agg.unstack()
content_explation_agg=content_explation_agg.reset_index()
content_explation_agg.columns = ['content_id', 'content_explation_false_mean','content_explation_true_mean']<data_type_conversions>
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv' )
content_explation_agg.content_id=content_explation_agg.content_id.astype('int16')
 content_explation_agg.content_explation_false_mean=content_explation_agg.content_explation_false_mean.astype('float16')
 content_explation_agg.content_explation_true_mean=content_explation_agg.content_explation_true_mean.astype('float16' )<train_model>
train_data.drop(['Cabin'], axis=1, inplace=True)
 test_data.drop(['Cabin'], axis=1, inplace=True )
print('start handle attempt_no...' )<data_type_conversions>
whole_data = train_data.append(test_data)
 whole_data['Title'] = whole_data.Name.str.extract(r'([A-Za-z]+)\.', expand=False)
 whole_data.Title.value_counts()
train_df["attempt_no"] = 1
train_df.attempt_no=train_df.attempt_no.astype('int8')
 attempt_no_agg=train_df.groupby(["user_id","content_id"])["attempt_no"].agg(['sum'] ).astype('int8')
 train_df["attempt_no"] = train_df[["user_id","content_id",'attempt_no']].groupby(["user_id","content_id"])["attempt_no"].cumsum()<data_type_conversions>
Common_Title = ['Mr', 'Miss', 'Mrs', 'Master']
whole_data['Title'].replace(['Ms', 'Mlle', 'Mme'], 'Miss', inplace=True)
 whole_data['Title'].replace(['Lady'], 'Mrs', inplace=True)
 whole_data['Title'].replace(['Sir', 'Rev'], 'Mr', inplace=True)
 whole_data['Title'][~whole_data.Title.isin(Common_Title)] = 'Others'
print('start handle timestamp...')
 prior_question_elapsed_time_mean=train_df['prior_question_elapsed_time'].mean()
train_df['prior_question_elapsed_time'].fillna(prior_question_elapsed_time_mean, inplace=True )<data_type_conversions>
AgeMedian_by_titles = train_data.groupby('Title')['Age'].median()
AgeMedian_by_titles
max_timestamp_u = train_df[['user_id','timestamp']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u.columns = ['user_id', 'max_time_stamp']
max_timestamp_u.user_id=max_timestamp_u.user_id.astype('int32' )<data_type_conversions>
for title in AgeMedian_by_titles.index:
train_data['Age'][(train_data.Age.isnull())&(train_data.Title == title)] = AgeMedian_by_titles[title]
test_data['Age'][(test_data.Age.isnull())&(test_data.Title == title)] = AgeMedian_by_titles[title]
train_df['lagtime'] = train_df.groupby('user_id')['timestamp'].shift()
max_timestamp_u2 = train_df[['user_id','lagtime']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u2.columns = ['user_id', 'max_time_stamp2']
max_timestamp_u2.user_id=max_timestamp_u2.user_id.astype('int32' )<feature_engineering>
train_data['Embarked'].fillna(train_data.Embarked.mode() [0], inplace=True )
train_df['lagtime']=train_df['timestamp']-train_df['lagtime']
lagtime_mean=train_df['lagtime'].mean()
train_df['lagtime'].fillna(lagtime_mean, inplace=True )<data_type_conversions>
test_data['Fare'].fillna(test_data['Fare'].median() , inplace=True )
train_df['lagtime']=train_df['lagtime']/(1000*3600)
 train_df.lagtime=train_df.lagtime.astype('float32' )<data_type_conversions>
train_data.loc[train_data.Fare>512, 'Fare'] = 263
train_data.Fare.sort_values(ascending=False ).head(5 )
<data_type_conversions>
train_data['Sex_Code'] = train_data['Sex'].map({'female':1, 'male':0} ).astype('int')
 test_data['Sex_Code'] = test_data['Sex'].map({'female':1, 'male':0} ).astype('int' )
train_df['lagtime2'] = train_df.groupby('user_id')['timestamp'].shift(2)
 max_timestamp_u3 = train_df[['user_id','lagtime2']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u3.columns = ['user_id', 'max_time_stamp3']
max_timestamp_u3.user_id=max_timestamp_u3.user_id.astype('int32')
 train_df['lagtime2']=train_df['timestamp']-train_df['lagtime2']
lagtime_mean2=train_df['lagtime2'].mean()
train_df['lagtime2'].fillna(lagtime_mean2, inplace=True)
<data_type_conversions>
train_data['Embarked_Code'] = train_data['Embarked'].map({'S':0, 'C':1, 'Q':2} ).astype('int')
 test_data['Embarked_Code'] = test_data['Embarked'].map({'S':0, 'C':1, 'Q':2} ).astype('int' )
train_df['lagtime2']=train_df['lagtime2']/(1000*3600)
 train_df.lagtime2=train_df.lagtime2.astype('float32' )<data_type_conversions>
train_data['FareBin_5'] = pd.qcut(train_data['Fare'], 5)
 test_data['FareBin_5'] = pd.qcut(test_data['Fare'], 5 )
train_df['lagtime3'] = train_df.groupby('user_id')['timestamp'].shift(3)
 train_df['lagtime3']=train_df['timestamp']-train_df['lagtime3']
lagtime_mean3=train_df['lagtime3'].mean()
train_df['lagtime3'].fillna(lagtime_mean3, inplace=True)
 train_df['lagtime3']=train_df['lagtime3']/(1000*3600)
 train_df.lagtime3=train_df.lagtime3.astype('float32' )<data_type_conversions>
label = LabelEncoder()
train_data['AgeBin_Code_5'] = label.fit_transform(train_data['AgeBin_5'])
 test_data['AgeBin_Code_5'] = label.fit_transform(test_data['AgeBin_5'])
 label = LabelEncoder()
train_data['FareBin_Code_5'] = label.fit_transform(train_data['FareBin_5'])
 test_data['FareBin_Code_5'] = label.fit_transform(test_data['FareBin_5'] )
<data_type_conversions>
train_data['Title_Code'] = train_data.Title.map({'Mr':0, 'Miss':1, 'Mrs':2, 'Master':3, 'Others':4} ).astype('int')
 test_data['Title_Code'] = test_data.Title.map({'Mr':0, 'Miss':1, 'Mrs':2, 'Master':3, 'Others':4} ).astype('int' )
train_df['timestamp']=train_df['timestamp']/(1000*3600)
 train_df.timestamp=train_df.timestamp.astype('float16' )<feature_engineering>
whole_data = train_data.append(test_data)
 whole_data['Surname'] = whole_data.Name.str.extract(r'([A-Za-z]+),', expand=False)
 whole_data['TixPref'] = whole_data.Ticket.str.extract(r' (.*\d)', expand=False)
 whole_data['SurTix'] = whole_data['Surname'] + whole_data['TixPref']
whole_data['IsFamily'] = whole_data.SurTix.duplicated(keep=False)*1
sns.countplot(whole_data.IsFamily)
 plt.show()
train_df['delta_prior_question_elapsed_time'] = train_df.groupby('user_id')['prior_question_elapsed_time'].shift()
train_df['delta_prior_question_elapsed_time']=train_df['prior_question_elapsed_time']-train_df['delta_prior_question_elapsed_time']<data_type_conversions>
whole_data['Child'] = whole_data.Age.map(lambda x: 1 if x <=16 else 0)
 FamilyWithChild = whole_data[(whole_data.IsFamily==1)&(whole_data.Child==1)]['SurTix'].unique()
len(FamilyWithChild )
delta_prior_question_elapsed_time_mean=train_df['delta_prior_question_elapsed_time'].mean()
train_df['delta_prior_question_elapsed_time'].fillna(delta_prior_question_elapsed_time_mean, inplace=True)
 train_df.delta_prior_question_elapsed_time=train_df.delta_prior_question_elapsed_time.astype('int32' )<data_type_conversions>
whole_data['FamilyId'] = 0
x = 1
for tix in FamilyWithChild:
whole_data.loc[whole_data.SurTix==tix, ['FamilyId']] = x
x += 1
train_df['lag'] = train_df.groupby('user_id')[target].shift()
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 user_agg = train_df.groupby('user_id')['lag'].agg(['sum', 'count'] ).astype('int16')
 cum['cumsum'].fillna(0, inplace=True)
 train_df['user_correctness'] = cum['cumsum'] / cum['cumcount']
train_df['user_correct_count'] = cum['cumsum']
train_df['user_uncorrect_count'] = cum['cumcount']-cum['cumsum']
train_df.drop(columns=['lag'], inplace=True)
 train_df['user_correctness'].fillna(0.67, inplace=True)
 train_df.user_correctness=train_df.user_correctness.astype('float16')
 train_df.user_correct_count=train_df.user_correct_count.astype('int16')
 train_df.user_uncorrect_count=train_df.user_uncorrect_count.astype('int16')
<set_options>
X_train = train_data.drop(['Age', 'Embarked', 'Fare', 'Name', 'Parch', 'PassengerId', 'Sex', 'SibSp', 'Survived', 'Ticket', 'Title', 'AgeBin_5', 'FareBin_5', 'FamilySize', 'Surname', 'TixPref', 'SurTix', 'IsFamily', 'Child', 'FamilyId'], axis=1)
 y_train = train_data['Survived']
del cum
gc.collect()<data_type_conversions>
model = RandomForestClassifier(n_estimators=200, random_state=2 )
<data_type_conversions>
final = ['Title_Code', 'Sex_Code', 'ConnectedSurvival', 'Pclass', 'FareBin_Code_5']
train_df.prior_question_had_explanation=train_df.prior_question_had_explanation.astype('int8')
 explanation_agg = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['sum', 'count'])
 explanation_agg=explanation_agg.astype('int16')
<data_type_conversions>
grid_param = {
'n_estimators': [100, 200, 300],
'criterion':['gini', 'entropy'],
'min_samples_split': [2, 10, 20],
'min_samples_leaf': [1, 5],
'bootstrap': [True, False],
}
gd_sr = GridSearchCV(estimator=model,
param_grid=grid_param,
scoring='accuracy',
cv=5,
n_jobs=-1)
 gd_sr.fit(X_train[final], y_train)
 best_parameters = gd_sr.best_params_
print(best_parameters )
cum = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['cumsum', 'cumcount'])
 cum['cumcount']=cum['cumcount']+1
train_df['explanation_mean'] = cum['cumsum'] / cum['cumcount']
train_df['explanation_true_count'] = cum['cumsum']
train_df['explanation_false_count'] = cum['cumcount']-cum['cumsum']
train_df.explanation_mean=train_df.explanation_mean.astype('float16')
 train_df.explanation_true_count=train_df.explanation_true_count.astype('int16')
 train_df.explanation_false_count=train_df.explanation_false_count.astype('int16' )<set_options>
model1 = RandomForestClassifier(n_estimators=300, bootstrap=True, criterion= 'entropy', min_samples_leaf=5, min_samples_split=2, random_state=2 )
del cum
gc.collect()<categorify>
all_accuracies = cross_val_score(estimator=model1, X=X_train, y=y_train, cv=5)
 all_accuracies
all_accuracies.mean()
content_agg = train_df.groupby('content_id')[target].agg(['sum', 'count','var'])
 task_container_agg = train_df.groupby('task_container_id')[target].agg(['sum', 'count','var'])
 content_agg=content_agg.astype('float32')
 task_container_agg=task_container_agg.astype('float32' )<data_type_conversions>
X_test = test_data[final]
model.fit(X_train[final],y_train)
 prediction = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': prediction.astype(int)})
 output.to_csv('my_submission.csv', index=False )