Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
warnings.simplefilter(action='ignore' )
def haversine_distance(lat1, long1, lat2, long2):
data = [train, test]
for i in data:
R = 6371
phi1 = np.radians(i[lat1])
 phi2 = np.radians(i[lat2])
 delta_phi = np.radians(i[lat2]-i[lat1])
 delta_lambda = np.radians(i[long2]-i[long1])
 a = np.sin(delta_phi / 2.0)** 2 + np.cos(phi1)* np.cos(phi2)* np.sin(delta_lambda / 2.0)** 2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
d =(R * c)
 i['H_Distance'] = d
return d<concatenate>
train_df = pd.read_csv('.. /input/titanic/train.csv')
 predict_df = pd.read_csv('.. /input/titanic/test.csv' )
haversine_distance('pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude' )<feature_engineering>
df=pd.concat([train_df,predict_df], ignore_index=True, sort =False)
 df.head()
data = [train,test]
for i in data:
i['Year'] = i['pickup_datetime'].dt.year
i['Month'] = i['pickup_datetime'].dt.month
i['Date'] = i['pickup_datetime'].dt.day
i['Day of Week'] = i['pickup_datetime'].dt.dayofweek
i['Hour'] = i['pickup_datetime'].dt.hour<sort_values>
df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
train.sort_values(['H_Distance','fare_amount'], ascending=False )<compute_test_metric>
df[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
pearsonr(train['H_Distance'], train['fare_amount'] )<feature_engineering>
df['Sex'] = df['Sex'].map({'female': 1, 'male': 0} ).astype(int )
bins_0 = train.loc[(train['H_Distance'] == 0), ['H_Distance']]
bins_1 = train.loc[(train['H_Distance'] > 0)&(train['H_Distance'] <= 10),['H_Distance']]
bins_2 = train.loc[(train['H_Distance'] > 10)&(train['H_Distance'] <= 50),['H_Distance']]
bins_3 = train.loc[(train['H_Distance'] > 50)&(train['H_Distance'] <= 100),['H_Distance']]
bins_4 = train.loc[(train['H_Distance'] > 100)&(train['H_Distance'] <= 200),['H_Distance']]
bins_5 = train.loc[(train['H_Distance'] > 200)&(train['H_Distance'] <= 300),['H_Distance']]
bins_6 = train.loc[(train['H_Distance'] > 300),['H_Distance']]
bins_0['bins'] = '0'
bins_1['bins'] = '0-10'
bins_2['bins'] = '11-50'
bins_3['bins'] = '51-100'
bins_4['bins'] = '100-200'
bins_5['bins'] = '201-300'
bins_6['bins'] = '>300'
dist_bins =pd.concat([bins_0,bins_1,bins_2,bins_3,bins_4,bins_5,bins_6])
 dist_bins.columns<count_values>
df['name_title'] = df['Name'].str.extract('([A-Za-z]+)\.', expand=False )
Counter(dist_bins['bins'] )<filter>
df['name_title'].value_counts()
train.loc[(( train['pickup_latitude']==0)&(train['pickup_longitude']==0)) &(( train['dropoff_latitude']!=0)&(train['dropoff_longitude']!=0)) &(train['fare_amount']==0)]<drop_column>
def clean_name_title(val):
if val in ['Rev', 'Col', 'Mlle', 'Mme', 'Ms','Major', 'Jonkheer', 'Countess', 'Capt','Lady','Dona','Dr']:
return 'RARE'
elif val in ['Sir', 'Don']:
return 'Mr'
elif val in ['Lady', 'Dona']:
return 'Mrs'
else:
return val
df['name_title'] = df['name_title'].apply(clean_name_title)
 df['name_title'].value_counts()
train = train.drop(train.loc[(( train['pickup_latitude']==0)&(train['pickup_longitude']==0)) &(( train['dropoff_latitude']!=0)&(train['dropoff_longitude']!=0)) &(train['fare_amount']==0)].index, axis=0 )<filter>
a = df.groupby(['Sex','Pclass','name_title'] ).mean().round(2)
 a = a['Age']
test.loc[(( test['pickup_latitude']==0)&(test['pickup_longitude']==0)) &(( test['dropoff_latitude']!=0)&(test['dropoff_longitude']!=0)) ] 
<filter>
b = df.iloc[:891].groupby(['Sex','Pclass','name_title'])
 b_median = b.median()
b = b_median.reset_index() [['Sex', 'Pclass', 'name_title', 'Age']]
b.head()
train.loc[(( train['pickup_latitude']!=0)&(train['pickup_longitude']!=0)) &(( train['dropoff_latitude']==0)&(train['dropoff_longitude']==0)) &(train['fare_amount']==0)]<drop_column>
for key, value in df['Age'].iteritems() :
if pd.isna(df['Age'][key]):
for key2, value2 in b['Age'].iteritems() :
if df['Sex'][key] == b['Sex'][key2] and df['name_title'][key] == b['name_title'][key2] and df['Pclass'][key] == b['Pclass'][key2]:
df['Age'][key] = b['Age'][key2]
else:
df['Age'][key] = df['Age'][key]
train = train.drop(train.loc[(( train['pickup_latitude']!=0)&(train['pickup_longitude']!=0)) &(( train['dropoff_latitude']==0)&(train['dropoff_longitude']==0)) &(train['fare_amount']==0)].index, axis=0 )<filter>
df['has_cabin'] = 0
for Key, value in df['Cabin'].iteritems() :
if pd.isna(df['Cabin'][Key]):
df['has_cabin'][Key] = int(0)
 else:
df['has_cabin'][Key] = int(1 )
test.loc[(( test['pickup_latitude']!=0)&(test['pickup_longitude']!=0)) &(( test['dropoff_latitude']==0)&(test['dropoff_longitude']==0)) ]<filter>
df['Embarked'].fillna("S", inplace=True)
 df['Fare'].fillna(df['Fare_limited'].mean() , inplace=True)
 df['Age'].fillna(df['Age'].mean() , inplace=True )
high_distance = train.loc[(train['H_Distance']>200)&(train['fare_amount']!=0)]<feature_engineering>
le = preprocessing.LabelEncoder()
df['Embarked'] = le.fit_transform(df['Embarked'])
 df['Sex'] = le.fit_transform(df['Sex'])
 df['name_title'] = le.fit_transform(df['name_title'] )
high_distance['H_Distance'] = high_distance.apply(
 lambda row:(row['fare_amount'] - 2.50)/1.56,
axis=1
)<train_model>
train_df = df[0:891]
pred_df = df[-418::]
train.update(high_distance )<filter>
train_df['Survived'] = train_df['Survived'].astype(int )
train[train['H_Distance']==0]<filter>
features_X = train_df[['Sex','Age','Pclass','Fare_limited','alone','family_members','name_title']]
target_Y = train_df['Survived']
predict_X = pred_df[['Sex','Age','Pclass','Fare_limited','alone','family_members','name_title']]
train[(train['H_Distance']==0)&(train['fare_amount']==0)]<drop_column>
train_data, val_data, train_target, val_target = train_test_split(features_X, target_Y, test_size=0.4, random_state=42)
 train_data.shape, val_data.shape, len(train_target), len(val_target )
train = train.drop(train[(train['H_Distance']==0)&(train['fare_amount']==0)].index, axis = 0 )<define_variables>
model = RandomForestClassifier(n_estimators=1000,min_samples_split=8, min_samples_leaf=4)
 model.fit(train_data, train_target)
 val_predictions = model.predict(val_data)
 accuracy_score(val_target, val_predictions )
rush_hour = train.loc[(((train['Hour']>=6)&(train['Hour']<=20)) &(( train['Day of Week']>=1)&(train['Day of Week']<=5)) &(train['H_Distance']==0)&(train['fare_amount'] < 2.5)) ]
rush_hour<drop_column>
rmse = np.sqrt(mean_squared_error(val_target, val_predictions))
print("RMSE: %f" %(rmse))
print("MAE: " + str(mean_absolute_error(val_predictions, val_target)))
 print("R2:" + str(r2_score(val_target, val_predictions)) )
train=train.drop(rush_hour.index, axis=0 )<feature_engineering>
test_df = pd.read_csv('.. /input/titanic/test.csv')
 predict_Y = model.predict(predict_X)
 submissionRF = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": predict_Y
})
 submissionRF.to_csv('gender_submission.csv', index=False )
non_rush_hour = train.loc[(((train['Hour']<6)|(train['Hour']>20)) &(( train['Day of Week']>=1)&(train['Day of Week']<=5)) &(train['H_Distance']==0)&(train['fare_amount'] < 3.0)) ]
non_rush_hour 
<filter>
def algorithm_pipeline(X_train_data, X_test_data, y_train_data, y_test_data,
model, param_grid, cv=10, scoring_fit='neg_mean_squared_error',
do_probabilities = False):
gs = GridSearchCV(
 estimator=model,
param_grid=param_grid,
cv=cv,
n_jobs=-1,
scoring=scoring_fit,
verbose=2 
)
 fitted_model = gs.fit(X_train_data, y_train_data)
 if do_probabilities:
pred = fitted_model.predict_proba(X_test_data)
 else:
pred = fitted_model.predict(X_test_data)
 return fitted_model, pred
weekends = train.loc[(( train['Day of Week']==0)|(train['Day of Week']==6)) &(train['H_Distance']==0)&(train['fare_amount'] < 3.0)]
weekends 
<filter>
model = RandomForestClassifier()
parameters = {
'min_samples_split': [4,8],
'min_samples_leaf': [2,4,8],
'criterion': ['gini', 'entropy'],
'n_estimators': [100]}
model, pred = algorithm_pipeline(train_data, val_data, train_target, val_target, model,
parameters, cv=5)
 print(model.best_params_)
 rmse = np.sqrt(-model.best_score_)
 print("RMSE: %f" %(rmse))
train.loc[(train['H_Distance']!=0)&(train['fare_amount']==0)]<feature_engineering>
classifiers = [
KNeighborsClassifier() ,
SVC(probability=True),
DecisionTreeClassifier() ,
RandomForestClassifier() ,
AdaBoostClassifier() ,
GradientBoostingClassifier() ,
GaussianNB() ,
LinearDiscriminantAnalysis() ,
QuadraticDiscriminantAnalysis() ,
LogisticRegression() ]
log_cols = ["Classifier", "Accuracy"]
log 	 = pd.DataFrame(columns=log_cols)
 sss = StratifiedShuffleSplit(n_splits=8, test_size=0.5, random_state=1)
 X = features_X.values
y = target_Y.values
acc_dict = {}
for train_index, test_index in sss.split(X, y):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
for clf in classifiers:
name = clf.__class__.__name__
clf.fit(X_train, y_train)
 train_predictions = clf.predict(X_test)
 acc = accuracy_score(y_test, train_predictions)
 if name in acc_dict:
acc_dict[name] += acc
else:
acc_dict[name] = acc
for clf in acc_dict:
acc_dict[clf] = acc_dict[clf] / 10.0
log_entry = pd.DataFrame([[clf, acc_dict[clf]]], columns=log_cols)
 log = log.append(log_entry)
 log.sort_values('Accuracy', ascending=False )
scenario_3 = train.loc[(train['H_Distance']!=0)&(train['fare_amount']==0)]<sort_values>
model = GradientBoostingClassifier(learning_rate=0.005,n_estimators=1000, min_samples_split=2, min_samples_leaf=2)
 model.fit(train_data, train_target)
 val_predictions = model.predict(val_data)
 accuracy_score(val_target, val_predictions )
scenario_3.sort_values('H_Distance', ascending=False )<feature_engineering>
rmse = np.sqrt(mean_squared_error(val_target, val_predictions))
print("RMSE: %f" %(rmse))
print("MAE: " + str(mean_absolute_error(val_predictions, val_target)))
 print("R2:" + str(r2_score(val_target, val_predictions)) )
scenario_3['fare_amount'] = scenario_3.apply(
 lambda row:(( row['H_Distance'] * 1.56)+ 2.50), axis=1
)<filter>
train.loc[(train['H_Distance']==0)&(train['fare_amount']!=0)]<feature_engineering>
model = Sequential()
model.add(Dense(units = 7, kernel_initializer = 'uniform', activation = 'relu', input_dim = 7))
model.add(Dense(units = 9, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(units = 5, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
keras.optimizers.Adam(learning_rate=0.0009)
 model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
 model.fit(features_X, target_Y, batch_size = 32, epochs = 200)
scenario_4 = train.loc[(train['H_Distance']==0)&(train['fare_amount']!=0)]<filter>
y_pred = model.predict(features_X)
 y_pred =(y_pred > 0.5 ).astype(int ).reshape(features_X.shape[0])
 accuracy_score(target_Y, y_pred )
scenario_4.loc[(scenario_4['fare_amount']<=3.0)&(scenario_4['H_Distance']==0)]<filter>
y_pred = model.predict(predict_X)
 y_final =(y_pred > 0.5 ).astype(int ).reshape(predict_X.shape[0] )
<filter><EOS>