Kaggle competition: Titanic - Machine Learning from Disaster
result = pd.DataFrame(y_pred_proba, columns=keys)
 result.head()<save_to_csv>
data_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 data_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 data = pd.concat([data_train, data_test], ignore_index=True, sort=False )
result.to_csv(path_or_buf="rfc_predict_4.csv",index=True, index_label = 'Id' )<load_from_csv>
pClass_1 = round(
(data_train[data_train.Pclass == 1].Survived == 1 ).value_counts() [1] /
len(data_train[data_train.Pclass == 1])* 100, 2)
 pClass_2 = round(
(data_train[data_train.Pclass == 2].Survived == 1 ).value_counts() [1] /
len(data_train[data_train.Pclass == 2])* 100, 2)
 pClass_3 = round(
(data_train[data_train.Pclass == 3].Survived == 1 ).value_counts() [1] /
len(data_train[data_train.Pclass == 3])* 100, 2)
 pClassDf = pd.DataFrame(
 {"Survived": {"Class 1": pClass_1,
"Class 2": pClass_2,
"Class 3": pClass_3},
"Not survived": {"Class 1": 100 - pClass_1,
"Class 2": 100 - pClass_2,
"Class 3": 100 - pClass_3}})
 pClassDf.plot.bar().set_title("Survived ~ Class")
 plt.show()
data_train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
<load_from_csv>
data['Family'] = data.Parch + data.SibSp
data['IsAlone'] = data.Family == 0
data['SmallFamily'] = data['Family'].map(lambda s: 1 if 1 <= s <= 3 else 0)
 data['BigFamily'] = data['Family'].map(lambda s: 1 if 4 <= s else 0 )
pd.options.display.max_columns=100
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id')
 def feature_engineering(data):
data['Date'] = pd.to_datetime(data['Dates'].dt.date)
 data['n_days'] =(data['Date'] - data['Date'].min() ).apply(lambda x: x.days)
 data['Day'] = data['Dates'].dt.day
data['DayOfWeek'] = data['Dates'].dt.weekday
data['Month'] = data['Dates'].dt.month
data['Year'] = data['Dates'].dt.year
data['Hour'] = data['Dates'].dt.hour
data['Minute'] = data['Dates'].dt.minute
data['Block'] = data['Address'].str.contains('block', case=False ).apply(lambda x: 1 if x == True else 0)
 data["X_Y"] = data["X"] - data["Y"]
data["XY"] = data["X"] + data["Y"]
data.drop(columns=['Dates','Date','Address'], inplace=True)
 return data
train = feature_engineering(train)
 test = feature_engineering(test)
 train.drop(columns=['Descript','Resolution'], inplace=True )<categorify>
data['Salutation'] = data.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 print(data.Salutation.unique())
 data.Salutation.nunique()
for i in train,test:
data = i
coord = data[['X','Y']]
pca = PCA(n_components=2)
 pca.fit(coord)
 coord_pca = pca.transform(coord)
 data['coord_pca1'] = coord_pca[:, 0]
data['coord_pca2'] = coord_pca[:, 1]<categorify>
dataset_title = [i.split(",")[1].split(".")[0].strip() for i in data["Name"]]
data['Title']=data.Name.apply(lambda x: x.split('.')[0].split(',')[1].strip())
 newtitles={
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"}
data['Title']=data.Title.map(newtitles)
 data['Mother'] =(data['Title'] == 'Mrs')&(data['Parch'] > 0)
 data['Mother'] = data['Mother'].astype(int )
<categorify>
grp = data.groupby(['Sex', 'Pclass', 'Title'])
 data.Age = grp.Age.apply(lambda x_: x_.fillna(x_.median()))
data.Age.fillna(data.Age.median, inplace=True)
 data['AgeRange'] = pd.cut(data['Age'].astype(int), 5 )
le1 = LabelEncoder()
train['PdDistrict'] = le1.fit_transform(train['PdDistrict'])
 test['PdDistrict'] = le1.transform(test['PdDistrict'])
 le2 = LabelEncoder()
X = train.drop(columns=['Category'])
 y= le2.fit_transform(train['Category'] )<choose_model_class>
data['Ticket_Lett'] = data['Ticket'].apply(lambda x: str(x)[0])
 data['Ticket_Lett'] = data['Ticket_Lett'].apply(lambda x: str(x))
data['Ticket_Lett'] = np.where(( data['Ticket_Lett'] ).isin(['1', '2', '3', 'S', 'P', 'C', 'A']), data['Ticket_Lett'],
np.where(( data['Ticket_Lett'] ).isin(['W', '4', '7', '6', 'L', '5', '8']),
'Low_ticket', 'Other_ticket'))
data['Ticket_Len'] = data['Ticket'].apply(lambda x: len(x))
<define_variables>
data.Fare.fillna(data.Fare.mean() , inplace = True)
<train_model>
data.Embarked.fillna(data.Embarked.mode() [0], inplace = True )
train_data = lgb.Dataset(X, label=y, categorical_feature=['PdDistrict', ])
 params = {'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 20,
'learning_rate': 0.29,
'max_bin': 501,
'num_leaves': 41,
'verbose' : 1}
bst = lgb.train(params, train_data, 120)
 predictions_lgb = bst.predict(test)
<train_model>
data.Cabin.fillna('NA', inplace=True)
 data['Cabin'] = data['Cabin'].map(lambda s: s[0] )
train_data = lgb.Dataset(X, label=y, categorical_feature=['PdDistrict', ])
 params = {'boosting':'gbdt',
'objective':'multiclass',
'num_class':39,
'max_delta_step':0.9,
'min_data_in_leaf': 4,
'learning_rate': 0.29,
'max_bin': 501,
'num_leaves': 41,
'verbose' : 1}
bst = lgb.train(params, train_data, 120)
 predictions_lgb1 = bst.predict(test)
<compute_test_metric>
data['Title'] = LabelEncoder().fit_transform(data['Title'])
 data = pd.concat([data,
pd.get_dummies(data.Cabin, prefix="Cabin"),
pd.get_dummies(data.AgeRange, prefix="AgeRange"),
pd.get_dummies(data.Embarked, prefix="Embarked", drop_first = True),
pd.get_dummies(data.Title, prefix="Title", drop_first = True),
pd.get_dummies(data.FareCategory, prefix="Fare", drop_first = True),
pd.get_dummies(data.Ticket_Lett, prefix="Ticket", drop_first = True),
pd.get_dummies(data.Pclass, prefix="Class", drop_first = True)], axis=1)
 data['IsAlone'] = LabelEncoder().fit_transform(data['IsAlone'])
 data['Sex'] = LabelEncoder().fit_transform(data['Sex'])
con2 =(predictions_lgb + predictions_lgb1)/2
con2<prepare_x_and_y>
data.drop(['Pclass', 'Fare','Cabin', 'FareCategory','Name','Salutation', 'Ticket_Lett', 'Ticket','Embarked', 'AgeRange', 'SibSp', 'Parch', 'Age'], axis=1, inplace=True )
<train_model>
X_pred = data[data.Survived.isnull() ].drop(['Survived'], axis=1)
 train_data = data.dropna()
X = train_data.drop(['Survived'], axis=1)
 y = train_data['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
<save_to_csv>
clf = GradientBoostingClassifier(learning_rate=0.1,
n_estimators=700,
max_depth=2)
 clf.fit(X_train, np.ravel(y_train))
print("RF Accuracy: " + repr(round(clf.score(X_test, y_test)* 100, 2)) + "%")
 result_rf = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
 print('The cross validated score for Random forest is:', round(result_rf.mean() * 100, 2))
y_pred = cross_val_predict(clf, X_train, y_train, cv=5 )
submission = pd.DataFrame(con2, columns=le2.inverse_transform(np.linspace(0, 38, 39, dtype='int16')) ,
index=test.index)
 submission.to_csv('submission.csv', index='Id' )<load_from_csv>
clf = RandomForestClassifier(criterion='entropy',
n_estimators=700,
min_samples_split=5,
min_samples_leaf=1,
max_features = "auto",
oob_score=True,
random_state=0,
n_jobs=-1)
 clf.fit(X_train, np.ravel(y_train))
print("RF Accuracy: " + repr(round(clf.score(X_test, y_test)* 100, 2)) + "%")
 result_rf = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
 print('The cross validated score for Random forest is:', round(result_rf.mean() * 100, 2))
y_pred = cross_val_predict(clf, X_train, y_train, cv=5)
train = pd.read_csv('.. /input/train.csv', parse_dates=['Dates'])
 test = pd.read_csv('.. /input/test.csv', parse_dates=['Dates'], index_col='Id' )<count_missing_values>
result = clf.predict(X_pred)
 submission = pd.DataFrame({'PassengerId':X_pred.PassengerId,'Survived':result})
 submission.Survived = submission.Survived.astype(int)
 print(submission.shape)
 filename = 'TitanicPredictions4.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )