Kaggle competition: Titanic - Machine Learning from Disaster
rfc2 = RandomForestClassifier(random_state=0, n_estimators=1000, n_jobs=-1, verbose=1)
 rfc2.fit(X_train.iloc[trainInds], Y_train.iloc[trainInds] )<predict_on_test>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 train = train_data.copy()
test = test_data.copy()
train.shape
y_train = train['Survived']
Id = pd.DataFrame(test['PassengerId'])
 train.drop(['PassengerId'], axis = 1, inplace=True)
 test.drop(['PassengerId'], axis = 1, inplace=True)
 train.drop(['Survived'], axis = 1, inplace=True)
 train.drop(['Ticket'], axis = 1, inplace=True)
 test.drop(['Ticket'], axis = 1, inplace=True )
pred_val_rfc2 = rfc2.predict_proba(X_train.iloc[validInds])
 pred_test_rfc2 = rfc2.predict_proba(df_test[trainCols])
 log_loss(Y_train.iloc[validInds], pred_val_rfc2), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_rfc2, axis = 1), average = "micro" )<train_model>
train.isnull().sum().sort_values(ascending=False)[0:20]
svc_clf = make_pipeline(StandardScaler() , SVC(gamma='auto', probability=True))
svc_clf.fit(Xnn_train.iloc[trainInds], Y_train.iloc[trainInds] )<predict_on_test>
for i in list(train.columns):
dtype = train[i].dtype
values = 0
if(dtype == float or dtype == int):
method = 'mean'
else:
method = 'mode'
if(train[i].notnull().sum() / 891 <=.5):
train.drop(i, axis = 1, inplace=True)
 elif method == 'mean':
train[i]=train[i].fillna(train[i].mean())
 else:
train[i]=train[i].fillna(train[i].mode() [0])
 for i in list(test.columns):
dtype = test[i].dtype
values = 0
if(dtype == float or dtype == int):
method = 'mean'
else:
method = 'mode'
if(test[i].notnull().sum() / 418 <=.5):
test.drop(i, axis = 1, inplace=True)
 elif method == 'mean':
test[i]=test[i].fillna(test[i].mean())
 else:
test[i]=test[i].fillna(test[i].mode() [0])
pred_val_svc = svc_clf.predict_proba(Xnn_train.iloc[validInds])
 pred_test_svc = svc_clf.predict_proba(Xnn_test )<compute_test_metric>
train_test_data = [train, test]
for dataset in train_test_data:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 1,
"Master": 0, "Dr": 1, "Rev": 0, "Col": 0, "Major": 0, "Mlle": 1,"Countess": 1,
"Ms": 1, "Lady": 1, "Jonkheer": 1, "Don": 0, "Dona" : 1, "Mme": 0,"Capt": 0,"Sir": 0 }
for dataset in train_test_data:
dataset['Title'] = dataset['Title'].map(title_mapping)
log_loss(Y_train.iloc[validInds], pred_val_svc), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_svc, axis = 1), average = "micro" )<train_model>
sex_mapping = {"male": 0, "female":1}
for dataset in train_test_data:
dataset['Sex'] = dataset['Sex'].map(sex_mapping)
svc_clf2 = make_pipeline(StandardScaler() , SVC(gamma='auto', probability=True))
svc_clf2.fit(X_train.iloc[trainInds], Y_train.iloc[trainInds] )<predict_on_test>
Pclass1 = train_data[train_data['Pclass'] == 1]['Embarked'].value_counts()
Pclass2 = train_data[train_data['Pclass'] == 2]['Embarked'].value_counts()
Pclass3 = train_data[train_data['Pclass'] == 3]['Embarked'].value_counts()
df = pd.DataFrame([Pclass1, Pclass2, Pclass3])
 df.index = ['1st class','2nd class', '3rd class']
df.plot(kind='bar',stacked=True, figsize=(10,5))
pred_val_svc2 = svc_clf2.predict_proba(X_train.iloc[validInds] )<compute_train_metric>
for data in train_test_data:
data['Embarked'] = data['Embarked'].fillna("S")
 embarked_mapping = {"S": 0, "C": 1, "Q": 2}
for dataset in train_test_data:
dataset['Embarked'] = dataset['Embarked'].map(embarked_mapping )
pred_test_svc2 = svc_clf2.predict_proba(df_test[trainCols])
 log_loss(Y_train.iloc[validInds], pred_val_svc2), f1_score(Y_train.iloc[validInds], np.argmax(pred_val_svc2, axis = 1), average = "micro" )<concatenate>
train["FamilySize"] = train['SibSp'] + train['Parch'] + 1
test["FamilySize"] = test['SibSp'] + test['Parch'] + 1
val_preds_all = np.hstack([pred_val_lgb2, pred_val_lgb, pred_val_nn, pred_val_nn1, pred_val_svc, pred_val_svc2, pred_val_rfc, pred_val_rfc2])
 test_preds_all = np.hstack([pred_test_lgb2, pred_test_lgb, pred_test_nn, pred_test_nn1, pred_test_svc, pred_test_svc2, pred_test_rfc, pred_test_rfc2] )<train_model>
train.drop(['Name'], axis = 1, inplace=True)
 test.drop(['Name'], axis = 1, inplace=True )
clf = LogisticRegression(multi_class="ovr" ).fit(val_preds_all, Y_train.iloc[validInds] )<predict_on_test>
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasClassifier
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten, Conv2D,MaxPool2D
import keras
prediction = clf.predict(test_preds_all)
 prediction<save_to_csv>
continuous = ['Age', 'Fare', 'Parch', 'Pclass', 'SibSp', 'FamilySize']
scaler = StandardScaler()
for var in continuous:
train[var] = train[var].astype('float64')
 train[var] = scaler.fit_transform(train[var].values.reshape(-1, 1))
for var in continuous:
test[var] = test[var].astype('float64')
 test[var] = scaler.fit_transform(test[var].values.reshape(-1, 1))
sub["department_id"] = prediction.astype(int)
 sub.to_csv("submission.csv", index = False)
 sub.head()<save_to_csv>
tf.keras.optimizers.Adam(
 learning_rate=0.003, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False,
name='Adam', 
)
 model = Sequential()
model.add(Flatten())
 model.add(Dense(32, input_dim=train.shape[1],kernel_initializer = 'uniform', activation='relu'))
model.add(Dense(32, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dropout(0.4))
model.add(Dense(32,kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
sub.to_csv('submission.csv' )<save_to_csv>
history = model.fit(train, y_train, epochs=20, batch_size=50, validation_split = 0.2)
 %s: %.2f%%" %('val_acc', val_acc*100))
df.to_csv("dataframe_all.csv")
 np.savetxt("val_preds_all.csv", val_preds_all, delimiter=",")
 np.savetxt("test_preds_all.csv", test_preds_all, delimiter=",")
 np.save("trainInds", trainInds)
 np.save("validInds", validInds )<save_to_csv>
scores = model.evaluate(train, y_train, batch_size=32 )
df_test.to_csv("dataframe_test.csv" )<load_from_csv>
y_pred = model.predict(test)
 y_final =(y_pred > 0.5 ).astype(int ).reshape(test.shape[0])
 output = pd.DataFrame({'PassengerId': test_data['PassengerId'], 'Survived': y_final})
 output.to_csv('prediction-ann.csv', index=False )