Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_search_space>
import matplotlib.pyplot as plt
import sklearn.model_selection
from sklearn.preprocessing import MinMaxScaler
from sklearn.impute import KNNImputer
from sklearn.metrics import accuracy_score
import seaborn as sns
import numpy as np
import pandas as pd
lr = 1e-2<train_model>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
learn.fit_one_cycle(6, lr )<train_model>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
Then we unfreeze and train the whole model, with lower lr .<train_model>
all_data = pd.concat([train_data, test_data])
 all_data.head()
learn.unfreeze()
learn.fit_one_cycle(3, slice(1e-4, 1e-3))<save_model>
survival_rate = train_data["Survived"].mean()
print(f"Survival rate: {survival_rate}")
 print(f"Death rate: {1-survival_rate}" )
learn.save('128' )<compute_test_metric>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
p,t = learn.get_preds()
auc_score(p,t )<find_best_params>
train_data.loc[train_data["Sex"] == "male"]["Embarked"]
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()<predict_on_test>
features = ["Pclass", "Sex", "SibSp", "Parch", "Age", "Embarked"]
X_train = pd.get_dummies(train_data[features])
 imputer = KNNImputer(n_neighbors=5, weights='uniform', metric='nan_euclidean')
 X_train = imputer.fit_transform(X_train)
 scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
 y_train = np.array(train_data["Survived"])
 print(f"Shape of the training set: {X_train.shape}" )
p,t = learn.get_preds(ds_type=DatasetType.Test )<create_dataframe>
def trainClassifier(X_train, y_train, model_name, classifier, params, score, verbose=False, num_folds=10):
kf = sklearn.model_selection.StratifiedKFold(num_folds)
 train_scores = []
best_score = 0
for config in sklearn.model_selection.ParameterGrid(params):
train_scores_run = []
counts = []
for train_indices, valid_indices in kf.split(X_train, y_train):
counts.append(len(train_indices))
X_train_kf = X_train[train_indices]
y_train_kf = y_train[train_indices]
X_valid_kf = X_train[valid_indices]
y_valid_kf = y_train[valid_indices]
model = classifier(**config)
 model.fit(X_train_kf, y_train_kf)
 y_hat = model.predict(X_valid_kf)
 train_score = score(y_valid_kf, y_hat)
 train_scores_run.append(train_score)
 if np.average(train_scores_run, weights=counts)> best_score:
best_score = np.average(train_scores_run, weights=counts)
 best_config = config
if(verbose):
print("New best score obtained")
 print(f"Training with: {config}")
 print(f"Total Score obtained with cross validation: {best_score}
")
 train_scores.append(np.average(train_scores_run, weights=counts))
output_df = pd.DataFrame(data = [[model_name, best_config ,best_score]], \
columns=["model_name", "parameters", "training_score"])
 return output_df
sub = pd.DataFrame(np.stack([ids, p[:,1]], axis=1), columns=df_sample.columns )<save_to_csv>
results = pd.DataFrame()
sub.to_csv(work_dir/'sub.csv', index=False )<create_dataframe>
params = {
"n_neighbors": [1, 3, 5, 7, 9, 11, 13, 15]
}
classifier = KNeighborsClassifier
classifier_df = trainClassifier(X_train, y_train, "k-NN", classifier, params, accuracy_score)
 results = results.append(classifier_df )
def get_null(train):
columns = train.columns
values = []
features = []
for column in columns:
size = train[train[column].isnull() ].shape[0]
if(size != 0):
features.append(column)
 values.append(size)
 return pd.DataFrame({
'features': features,
'values': values
} )<count_values>
params = {
"C": [1e-3, 1e-2, 1e-1, 1],
"max_iter": [30000]
}
classifier = LinearSVC
classifier_df = trainClassifier(X_train, y_train, "LinearSVC", classifier, params, accuracy_score)
 results = results.append(classifier_df )
def range_quantile(column):
q1 = train[column].quantile(0.25)
 q2 = train[column].quantile(0.5)
 q3 = train[column].quantile(0.75)
 print(train[train[column] == q1]['isChurned'].value_counts())
 print(train[(train[column] > q1)&(train[column] <= q2)]['isChurned'].value_counts())
 print(train[(train[column] > q2)&(train[column] <= q3)]['isChurned'].value_counts())
 print(train[train[column] > q3]['isChurned'].value_counts())
 return q1, q2, q3<set_options>
params = {
"kernel" : ["rbf"],
"C": [1e-4, 1e-3, 1e-2, 1e-1, 1, 10],
"gamma": [1e-3, 1e-2, 1e-1, 1, 10]
}
classifier = SVC
classifier_df = trainClassifier(X_train, y_train, "SVC", classifier, params, accuracy_score)
 results = results.append(classifier_df )
warnings.filterwarnings('ignore')
<load_from_csv>
params = {
"C": [1e-3, 1e-2, 1e-1, 1, 10]
}
classifier = LogisticRegression
classifier_df = trainClassifier(X_train, y_train, "LogisticRegression", classifier, params, accuracy_score)
 results = results.append(classifier_df )
train = pd.read_csv('.. /input/seleksidukungaib/train.csv')
 test = pd.read_csv('.. /input/seleksidukungaib/test.csv')
 print(train.shape)
 print(test.shape )<count_values>
params = {"max_depth": [3, 5, 7, 10, None],
"n_estimators":[3, 5,10, 25, 50],
"max_features": [1, 2, "auto"]}
classifier = RandomForestClassifier
classifier_df = trainClassifier(X_train, y_train, "RandomForests", classifier, params, accuracy_score)
 results = results.append(classifier_df )
train['isChurned'].value_counts()<drop_column>
results = results.set_index("model_name")
 results
train.drop(columns=['idx'], inplace=True)
 test.drop(columns=['idx'], inplace=True )<count_duplicates>
classifier = RandomForestClassifier
best_params = results.loc["RandomForests"]["parameters"]
submission_model = classifier(**best_params)
 submission_model.fit(X_train, y_train)
 X_test = pd.get_dummies(test_data[features])
 X_test = imputer.transform(X_test)
 X_test = scaler.transform(X_test)
 y_hat = submission_model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_hat})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!")