Kaggle competition: Titanic - Machine Learning from Disaster
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 acc_linear_svc<compute_train_metric>
train = pd.read_csv(".. /input/titanic/train.csv")
 y_train = train["Survived"]
train.drop("Survived",axis=1,inplace=True)
 test = pd.read_csv(".. /input/titanic/test.csv")
 sample_sub = pd.read_csv(".. /input/titanic/gender_submission.csv" )
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(sgd.score(X_train, Y_train)* 100, 2)
 acc_sgd<choose_model_class>
def search_title(name):
match = re.search("([A-Za-z]+)\.",name)
 if match: return match.group(1)
 else: return ""
train.Name = train.Name.apply(search_title)
 test.Name = test.Name.apply(search_title )
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_predt = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 acc_decision_tree<compute_train_metric>
train["Age"].fillna(train["Age"].median() , inplace=True)
 test ["Age"].fillna(train["Age"].median() , inplace=True)
 train["Embarked"].fillna(train["Embarked"].mode() [0], inplace=True)
 test ["Embarked"].fillna(train["Embarked"].mode() [0], inplace=True)
 train["Cabin"] = train["Cabin"].apply(( lambda x: 0 if type(x)== float else 1))
test ["Cabin"] = test ["Cabin"].apply(( lambda x: 0 if type(x)== float else 1))
train["TotalFamily"] = train["SibSp"] + train["Parch"]
test ["TotalFamily"] = test ["SibSp"] + test ["Parch"]
test ["Fare"].fillna(train["Fare"].median() , inplace=True )
ex = ExtraTreesClassifier(random_state = 6, bootstrap=True, oob_score=True)
 ex.fit(X_train, Y_train)
 y_predERT = ex.predict(X_test)
 ex.score(X_train, Y_train)
 score = round(ex.score(X_train, Y_train)* 100, 2)
 print('Extremely Randomized Trees', score )<import_modules>
oneHot = OneHotEncoder()
oneHotName = OneHotEncoder(handle_unknown="ignore")
 ordinal = OrdinalEncoder()
train = pd.concat([train,
pd.DataFrame(oneHot.fit_transform(train[["Embarked"]] ).toarray()),
pd.DataFrame(ordinal.fit_transform(train[["Sex"]])) ,
pd.DataFrame(oneHotName.fit_transform(train[["Name"]] ).toarray())], axis=1)
 train.drop(columns=["Sex","Embarked","Name","Ticket","PassengerId"],inplace=True)
 test = pd.concat([test,
pd.DataFrame(oneHot.transform(test[["Embarked"]] ).toarray()),
pd.DataFrame(ordinal.transform(test[["Sex"]])) ,
pd.DataFrame(oneHotName.transform(test[["Name"]] ).toarray())], axis=1)
 test.drop(columns=["Sex","Embarked","Name","Ticket","PassengerId"],inplace=True )
from sklearn.model_selection import GridSearchCV<import_modules>
forest = RandomForestClassifier(min_samples_leaf=3, random_state=42)
 boost = GradientBoostingClassifier(min_samples_leaf=3, random_state=42)
 forest.fit(train, y_train)
 boost.fit(train, y_train)
 train_preds = forest.predict(train)
 train_preds_boost = boost.predict(train)
 print("Train Accuracy
RandomForest: {}
GBDT: {}".format(round(precision_score(y_train,train_preds),3),round(precision_score(y_train,train_preds_boost),3)) )
from sklearn.model_selection import GridSearchCV<define_search_space>
scaler = StandardScaler()
train_scaled = np.hstack([scaler.fit_transform(train.iloc[:,[1,4]]), train.iloc[:,[0,2,3] + [i for i in range(5,len(train.columns)) ]].values])
 test_scaled = np.hstack([scaler.transform(test.iloc[:,[1,4]]), test.iloc[:,[0,2,3] + [i for i in range(5,len(test.columns)) ]].values])
n_estimators = [int(x)for x in np.linspace(start = 10, stop = 80, num = 10)]
max_features = ['auto', 'sqrt']
max_depth = [2,4]
min_samples_split = [2, 5]
min_samples_leaf = [1, 2]
bootstrap = [True, False]
param_grid = {'n_estimators': n_estimators,
'max_features': max_features,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'bootstrap': bootstrap}
rf_Model = RandomForestClassifier()
rf_Grid = GridSearchCV(estimator = rf_Model, param_grid = param_grid, cv = 3, verbose=2, n_jobs = 4)
 rf_Grid.fit(X_train, Y_train)
 rf_Grid.best_params_ 
<predict_on_test>
svc = SVC(random_state=42)
 svc.fit(train_scaled, y_train)
 train_preds_svc = svc.predict(train_scaled)
 log_reg = LogisticRegression(random_state=True,n_jobs=-1)
 log_reg.fit(train_scaled, y_train)
 train_preds_log = log_reg.predict(train_scaled)
 print("Train Accuracy
LogisticRegression: {}
SVC: {}".format(round(precision_score(y_train,train_preds_log),3),round(precision_score(y_train,train_preds_svc),3)))
Y_pred = rf_Grid.predict(X_test)
 rf_Grid.score(X_train, Y_train)
 acc_random_forest = round(rf_Grid.score(X_train, Y_train)* 100, 2)
 acc_random_forest<train_on_grid>
model = None
model = tf.keras.Sequential()
model.add(layers.Input(( len(train.columns),)))
 model.add(layers.Dense(64,activation=tf.nn.elu))
model.add(layers.Dense(32,activation=tf.nn.elu))
model.add(layers.Dense(1))
model.compile(loss="binary_crossentropy",metrics=["accuracy"],optimizer="adam")
 model.summary()
C=[0.05,0.1,0.2,0.3,0.25,0.4,0.5,0.6,0.7,0.8,0.9,1]
gamma=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
kernel=['rbf','linear']
hyper={'kernel':kernel,'C':C,'gamma':gamma}
gd=GridSearchCV(SVC() ,param_grid=hyper,verbose=True)
 gd.fit(X_train, Y_train)
 Y_preds = gd.predict(X_test)
 acc_svc = round(gd.score(X_train, Y_train)* 100, 2)
 acc_svc<create_dataframe>
es = EarlyStopping(monitor="accuracy",patience=5,restore_best_weights=True)
 val = int(len(train)* 0.1)
 history = model.fit(train_scaled[:-val], y_train[:-val].values, validation_data=(train_scaled[-val:], y_train[-val:].values), callbacks=[es], epochs=40, verbose=0 )
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [acc_svc, acc_knn, acc_log,
acc_random_forest, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree]})
 sorted_model=models.sort_values(by='Score', ascending=False)
 sorted_model<save_to_csv>
train_preds_ann = model.predict(train_scaled)
 train_preds_ann = [int(round(i[0])) for i in train_preds_ann]
print("Train Accuracy
ANN: {}".format(round(( np.array(train_preds_ann)== y_train ).sum() / len(train),3)) )
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submission2.csv', index=False )<set_options>
X_preds = np.hstack([forest.predict(test ).reshape(-1,1),
log_reg.predict(test_scaled ).reshape(-1,1),
boost.predict(test ).reshape(-1,1),
svc.predict(test_scaled ).reshape(-1,1),
np.array([int(round(i[0])) for i in model.predict(test_scaled)] ).reshape(-1,1)] )
%matplotlib inline<load_from_csv>
preds = []
for row in X_preds:
pred = row[0]
if np.array([row[1:] != pred] ).sum() >= 4:
pred = 0 if pred == 1 else 1
preds.append(pred )
train_data= pd.read_csv(".. /input/titanic/train.csv")
 test_data = pd.read_csv(".. /input/titanic/test.csv" )<drop_column>
sample_sub["Survived"] = preds
sample_sub.to_csv("submission.csv",index=False )
<count_duplicates><EOS> 
print(""Predicted survival rate": {}".format(round(( sample_sub["Survived"] == 1 ).sum() / len(test),3)))
 print("Train survival rate: {}".format(round(((y_train == 1 ).sum() / len(train)) ,3)))