Kaggle competition: Titanic - Machine Learning from Disaster
model.predict(df_test )<predict_on_test>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
pipe = Pipeline([('poly', PolynomialFeatures()),('StandardScaler', standardscaler),('XGBR', model)])
 pipe.fit(X_train, y_train)
 y_pred = np.exp(pipe.predict(df_test))
y_pred<feature_engineering>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
df_test['count'] = y_pred<save_to_csv>
missing_values_perc = train_data.isnull().sum() /train_data.isnull().count() *100
print(missing_values_perc)
 missing_values_per = test_data.isnull().sum() /test_data.isnull().count() *100
print(missing_values_per )
df_test[['count']].to_csv('submission.csv', index=True )<load_from_csv>
data = [train_data, test_data]
for dataset in data:
dataset['Relatives'] = dataset['SibSp'] + dataset['Parch']
dataset.loc[dataset['Relatives'] > 0, 'not_alone'] = 0
dataset.loc[dataset['Relatives'] == 0, 'not_alone'] = 1
dataset['not_alone'] = dataset['not_alone'].astype(int)
 train_data['not_alone'].value_counts()
relativesplot = sns.factorplot('Relatives','Survived', data=train_data, aspect = 2.5, )
%%time
train = pd.read_csv('.. /input/cat-in-the-dat/train.csv')
 test = pd.read_csv('.. /input/cat-in-the-dat/test.csv')
 print(train.shape)
 print(test.shape )<drop_column>
train_data = train_data.drop(['Cabin'], axis=1)
 test_data = test_data.drop(['Cabin'], axis=1)
 train_data = train_data.drop(['Name'], axis=1)
 test_data = test_data.drop(['Name'], axis=1)
 train_data = train_data.drop(['Ticket'], axis=1)
 test_data = test_data.drop(['Ticket'], axis=1)
 train_data = train_data.drop(['PassengerId'], axis=1)
 train_data.head()
target = train['target']
train_id = train['id']
test_id = test['id']
train.drop(['target', 'id'], axis=1, inplace=True)
 test.drop('id', axis=1, inplace=True)
 print(train.shape)
 print(test.shape )<categorify>
mean_value = 29
data = [train_data, test_data]
for dataset in data:
dataset['Age'] = dataset['Age'].fillna(mean_value)
 print(train_data['Age'].isnull().sum())
 print(test_data['Age'].isnull().sum() )
%%time
traintest = pd.concat([train, test])
 dummies = pd.get_dummies(traintest, columns=traintest.columns, drop_first=True, sparse=True)
 train_ohe = dummies.iloc[:train.shape[0], :]
test_ohe = dummies.iloc[train.shape[0]:, :]
print(train_ohe.shape)
 print(test_ohe.shape )<categorify>
data = [train_data, test_data]
for dataset in data:
dataset['Age'] = dataset['Age'].astype(int)
 dataset.loc[ dataset['Age'] <= 11, 'Age'] = 0
dataset.loc[(dataset['Age'] > 11)&(dataset['Age'] <= 18), 'Age'] = 1
dataset.loc[(dataset['Age'] > 18)&(dataset['Age'] <= 22), 'Age'] = 2
dataset.loc[(dataset['Age'] > 22)&(dataset['Age'] <= 27), 'Age'] = 3
dataset.loc[(dataset['Age'] > 27)&(dataset['Age'] <= 33), 'Age'] = 4
dataset.loc[(dataset['Age'] > 33)&(dataset['Age'] <= 40), 'Age'] = 5
dataset.loc[(dataset['Age'] > 40)&(dataset['Age'] <= 66), 'Age'] = 6
dataset.loc[ dataset['Age'] > 66, 'Age'] = 6
train_data['Age'].value_counts()
%%time
train_ohe = train_ohe.sparse.to_coo().tocsr()
test_ohe = test_ohe.sparse.to_coo().tocsr()<split>
top_value = 'S'
data = [train_data, test_data]
for dataset in data:
dataset['Embarked'] = dataset['Embarked'].fillna(top_value)
 print(train_data['Embarked'].isnull().sum())
 print(test_data['Embarked'].isnull().sum() )
%%time
def run_cv_model(train, test, target, model_fn, params={}, eval_fn=None, label='model'):
kf = KFold(n_splits=5)
 fold_splits = kf.split(train, target)
 cv_scores = []
pred_full_test = 0
pred_train = np.zeros(( train.shape[0]))
i = 1
for dev_index, val_index in fold_splits:
print('Started ' + label + ' fold ' + str(i)+ '/5')
 dev_X, val_X = train[dev_index], train[val_index]
dev_y, val_y = target[dev_index], target[val_index]
params2 = params.copy()
pred_val_y, pred_test_y = model_fn(dev_X, dev_y, val_X, val_y, test, params2)
 pred_full_test = pred_full_test + pred_test_y
pred_train[val_index] = pred_val_y
if eval_fn is not None:
cv_score = eval_fn(val_y, pred_val_y)
 cv_scores.append(cv_score)
 print(label + ' cv score {}: {}'.format(i, cv_score))
i += 1
print('{} cv scores : {}'.format(label, cv_scores))
print('{} cv mean score : {}'.format(label, np.mean(cv_scores)))
 print('{} cv std score : {}'.format(label, np.std(cv_scores)))
 pred_full_test = pred_full_test / 5.0
results = {'label': label,
'train': pred_train, 'test': pred_full_test,
'cv': cv_scores}
return results
def runLR(train_X, train_y, test_X, test_y, test_X2, params):
print('Train LR')
 model = LogisticRegression(**params)
 model.fit(train_X, train_y)
 print('Predict 1/2')
 pred_test_y = model.predict_proba(test_X)[:, 1]
print('Predict 2/2')
 pred_test_y2 = model.predict_proba(test_X2)[:, 1]
return pred_test_y, pred_test_y2
lr_params = {'solver': 'lbfgs', 'C': 0.1}
results = run_cv_model(train_ohe, test_ohe, target, runLR, lr_params, auc, 'lr' )<save_to_csv>
ports = {"S": 0, "C": 1, "Q": 2}
data = [train_data, test_data]
for dataset in data:
dataset['Embarked'] = dataset['Embarked'].map(ports )
%%time
submission = pd.DataFrame({'id': test_id, 'target': results['test']})
 submission.to_csv('submission.csv', index=False )<set_options>
mean_value = 32
data = [train_data, test_data]
for dataset in data:
dataset['Fare'] = dataset['Fare'].fillna(mean_value)
 print(train_data['Fare'].isnull().sum())
 print(test_data['Fare'].isnull().sum() )
__seed = 0
__n_folds = 5
__nrows = None
%matplotlib inline
plt.style.use('ggplot')
 pd.set_option('max_colwidth', 500)
 pd.set_option('max_columns', 500)
 pd.set_option('max_rows', 500)
 folds1 = StratifiedKFold(n_splits=__n_folds, shuffle=True, random_state=__seed)
 folds2 = StratifiedKFold(n_splits=__n_folds, shuffle=True, random_state=__seed+2)
 folds3 = StratifiedKFold(n_splits=__n_folds, shuffle=True, random_state=__seed+4 )<load_from_csv>
data = [train_data, test_data]
for dataset in data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[(dataset['Fare'] > 31)&(dataset['Fare'] <= 99), 'Fare'] = 3
dataset.loc[(dataset['Fare'] > 99)&(dataset['Fare'] <= 250), 'Fare'] = 4
dataset.loc[ dataset['Fare'] > 250, 'Fare'] = 5
dataset['Fare'] = dataset['Fare'].astype(int)
 train_data['Fare'].value_counts()
train = pd.read_csv("/kaggle/input/cat-in-the-dat/train.csv", index_col = "id", nrows = __nrows)
 test = pd.read_csv("/kaggle/input/cat-in-the-dat/test.csv", index_col = "id", nrows = __nrows)
 train.head()<count_values>
genders = {"male": 0, "female": 1}
data = [train_data, test_data]
for dataset in data:
dataset['Sex'] = dataset['Sex'].map(genders )
def fit_describe_infos(train, test, __featToExcl = [], target_for_vcramer = None):

 stats = []
__featToAnalyze = [v for v in list(train.columns)if v not in __featToExcl]
for col in tqdm_notebook(__featToAnalyze):
dtrain = dict(train[col].value_counts())
 dtest = dict(test[col].value_counts())
 set_train_not_in_test = set(dtest.keys())- set(dtrain.keys())
 set_test_not_in_train = set(dtrain.keys())- set(dtest.keys())
 dict_train_not_in_test = {key:value for key, value in dtest.items() if key in set_train_not_in_test}
dict_test_not_in_train = {key:value for key, value in dtrain.items() if key in set_test_not_in_train}
nb_moda_test, nb_var_test = len(dtest), pd.Series(dtest ).sum()
nb_moda_abs, nb_var_abs = len(dict_train_not_in_test), pd.Series(dict_train_not_in_test ).sum()
nb_moda_train, nb_var_train = len(dtrain), pd.Series(dtrain ).sum()
nb_moda_abs_2, nb_var_abs_2 = len(dict_test_not_in_train), pd.Series(dict_test_not_in_train ).sum()
if not target_for_vcramer is None:
vc = coef_vcramer(pd.crosstab(train[target_for_vcramer], train[col].fillna(-1)))
 else:
vc = 0
stats.append(( col, round(vc, 3), train[col].nunique()
, str(nb_moda_abs)+ '(' + str(round(100 * nb_moda_abs / nb_moda_test, 1)) +'%)'
, str(nb_moda_abs_2)+'(' + str(round(100 * nb_moda_abs_2 / nb_moda_train, 1)) +'%)'
, str(train[col].isnull().sum())+'(' + str(round(100 * train[col].isnull().sum() / train.shape[0], 1)) +'%)'
, str(test[col].isnull().sum())+'(' + str(round(100 * test[col].isnull().sum() / test.shape[0], 1)) +'%)'
, str(round(100 * train[col].value_counts(normalize = True, dropna = False ).values[0], 1))
, train[col].dtype))
df_stats = pd.DataFrame(stats, columns=['Feature', "Target Cramer's V"
, 'Unique values(train)', "Unique values in test not in train(and %)"
, "Unique values in train not in test(and %)"
, 'NaN in train(and %)', 'NaN in test(and %)', '% in the biggest cat.( train)'
, 'dtype'])
 if target_for_vcramer is None:
df_stats.drop("Target Cramer's V", axis=1, inplace=True)
 return df_stats, dict_train_not_in_test, dict_test_not_in_train<categorify>
X_train = train_data.drop("Survived", axis=1)
 Y_train = train_data["Survived"]
X_test = test_data.drop("PassengerId", axis=1 ).copy()
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 decision_tree.score(X_train, Y_train )
lr_params = {'penalty': 'l2', 'solver': 'lbfgs', 'C': 0.123456789, 'max_iter':500}
ohe1 =(OneHotEncoder(categories = 'auto', sparse = True, dtype = 'uint8', handle_unknown="ignore"),
[f for f in train.columns if not f in ["target", "bin_0"]])
 pipe = make_pipeline(make_column_transformer(ohe1), LogisticRegression(**lr_params))<compute_train_metric>
prediction = decision_tree.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': prediction})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was saved." )