Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None)
 pd.set_option('display.width', 1000)
train_df = train.copy()
test_df = test.copy()
train_df.drop(columns=["ID_code", "target"], inplace=True)
 test_df.drop(columns=["ID_code"], inplace=True)
 target = train.target<randomize_order>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 dataset = [train, test]
print('Entries in training set: ', len(train), '
Entries in testing set: ',len(test))
for df in dataset:
print(df.isna().sum())
 train_test_comb = pd.concat([train, test], axis=0 )
def augment_train(df_train, y_train):
t0 = df_train[y_train == 0].copy()
t1 = df_train[y_train == 1].copy()
i = 0
N = 3
for I in range(0):
for col in df_train.columns:
i = i + 1000
np.random.seed(i)
 np.random.shuffle(t0[col].values)
 np.random.shuffle(t1[col].values)
 df_train = pd.concat([df_train, t0.copy() ])
 df_train = pd.concat([df_train, t1.copy() ])
 y_train = pd.concat([y_train, pd.Series([0] * t0.shape[0]), pd.Series([1] * t1.shape[0])])
 return df_train, y_train<choose_model_class>
for df in dataset:
df['Familysize'] = df['SibSp']+df['Parch']
df['Title'] = df['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
title_names =(df['Title'].value_counts() > 10)
 df['Title'] = df['Title'].apply(lambda x: x if title_names.loc[x] == True else 'Misc')
 train_test_comb = pd.concat([train, test], axis=0)
 print(train_test_comb['Title'].value_counts() )
model = CatBoostClassifier(subsample=0.36,
custom_loss='Logloss',
random_strength = 0,
max_depth=3,
eval_metric="AUC",
learning_rate=0.02,
iterations=60000,
bootstrap_type='Bernoulli',
l2_leaf_reg=0.3,
task_type="GPU",
random_seed=432013,
od_type="Iter",
border_count=128
)<split>
for df in dataset:
df['Fare_cat'] = pd.qcut(df['Fare'], q=4, labels=(1,2,3,4))
df['Age_cat'] = pd.qcut(df['Age'], q=4, labels=(1,2,3,4))
df['Familysize'] = df['Familysize'].apply(lambda x: 'Alone' if x==0 else('Small' if x>0 and x<5 else('Medium' if x>=5 and x<7 else 'Large')) )
def run_cat(model, trt, tst, tar,n_splits=5, plot=False):
kf = KFold(n_splits=n_splits, random_state=432013, shuffle=True)
 oof = np.zeros(len(trt))
feature_importance_df = pd.DataFrame()
y_valid_pred = 0 * tar
y_test_pred = 0
for n_fold,(train_index, valid_index)in enumerate(kf.split(trt, tar)) :
y_train, y_valid = tar.iloc[train_index], tar.iloc[valid_index]
X_train, X_valid = trt.iloc[train_index,:], trt.iloc[valid_index,:]
X_train, y_train = augment_train(X_train, y_train)
 X_train,X_valid = generate_fe(trn=X_train,tst=X_valid)
 _train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 print("Fold ", n_fold)
 fit_model = model.fit(_train,
verbose_eval=1000,
early_stopping_rounds=1000,
eval_set=[_valid],
use_best_model=True,
plot=False, 
)
 pred = fit_model.predict_proba(X_valid)[:,1]
oof[valid_index] = pred
print("auc = ", roc_auc_score(y_valid, pred))
y_valid_pred.iloc[valid_index] = pred
y_test_pred += fit_model.predict_proba(test_fe)[:,1]
y_test_pred /= n_splits
print("average auc:", roc_auc_score(tar, oof))
return y_test_pred, oof<feature_engineering>
for df in dataset:
df['Age_cat'] = df['Age_cat'].astype(np.int32)
 df['Fare_cat'] = df['Fare_cat'].astype(np.int32)
 df.Title.replace({'Mr':1, 'Mrs':2, 'Miss':3, 'Master':4, 'Misc':5}, inplace=True)
 df.Sex.replace({'female':0, 'male': 1}, inplace=True)
 df.Embarked.replace({'S':1, 'C':2, 'Q':3}, inplace=True )
def generate_fe(trn, tst):
real,syn = detect_test(test_df[features])
 al = pd.concat([trn,tst,test_df.iloc[real]],axis=0)
 for c in features:
trn[c+"_test"]=trn[c].map(al[c].value_counts())
 trn[c+"_test"] = trn[c+"_test"]*trn[c]
tst[c+"_test"]=tst[c].map(al[c].value_counts())
 tst[c+"_test"] = tst[c+"_test"]*tst[c]
return trn, tst<define_variables>
features = ['Age_cat', 'Fare_cat', 'Pclass', 'Sex', 'Embarked', 'Title', 'Familysize']
encoded_fearures = []
for df in dataset:
for feature in features:
encoded = OneHotEncoder().fit_transform(df[feature].values.reshape(-1, 1)).toarray()
n = df[feature].nunique()
cols = [f'{feature}_{n}' for n in range(1, n + 1)]
encoded_df = pd.DataFrame(encoded, columns=cols)
 encoded_df.index = df.index
encoded_fearures.append(encoded_df)
 train_one = pd.concat([train, *encoded_fearures[:7]], axis=1)
 test_one = pd.concat([test, *encoded_fearures[7:]], axis=1)
 dataset = [train_one, test_one]
features = [c for c in train_df.columns if c not in ["ID_code","target"]]<count_unique_values>
for df in dataset:
df.drop(['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch',
'Ticket', 'Fare', 'Embarked', 'Familysize', 'Title', 'Fare_cat', 'Age_cat' ], axis=1, inplace=True )
def detect_test(test_df):
df_test=test_df.values
unique_count = np.zeros_like(df_test)
 for feature in tqdm(range(df_test.shape[1])) :
_, index_, count_ = np.unique(df_test[:, feature], return_counts=True, return_index=True)
 unique_count[index_[count_ == 1], feature] += 1
real_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)> 0)[:, 0]
synthetic_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)== 0)[:, 0]
return real_samples_indexes,synthetic_samples_indexes<feature_engineering>
features = [x for x in train_one.columns if x!='Survived']
x = train_one[features].to_numpy()
y = train_one['Survived'].to_numpy()
x_train, x_val, y_train, y_val = train_test_split(x, y, train_size = int(0.95*len(train_one)) , shuffle=False ,random_state=1400)
 print(x_train.shape, y_train.shape, x_val.shape, y_val.shape )
def generate_fe_test(tst):
re,sy = detect_test(tst[features])
 al = pd.concat([train_df,test_df.iloc[re]],axis=0)
 for c in features:
tst[c+"_test"]=tst[c].map(al[c].value_counts())
 tst[c+"_test"] = tst[c+"_test"]*tst[c]
return tst
test_fe = generate_fe_test(test_df[features] )<concatenate>
clf = RandomForestClassifier(criterion='gini',
n_estimators=300,
max_depth=4,
min_samples_split=4,
min_samples_leaf=7,
max_features='auto',
oob_score=True,
random_state=1400,
n_jobs=-1)
 clf.fit(x_train, y_train)
 y_pred = clf.predict(x_val)
 cm = confusion_matrix(y_val, y_pred)
 print(cm)
 print(classification_report(y_val, y_pred))
y_test_pred, oof = run_cat(model,train_df, test_df, target )<save_to_csv>
test_data = test_one[features].to_numpy()
prediction_clf = clf.predict(test_data)
 print(len(prediction_clf))
output = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': prediction_clf})
 output.to_csv('/kaggle/working/my_submission.csv', index=False )
submission = pd.read_csv(root.joinpath("sample_submission.csv"))
submission['target'] = y_test_pred
pd.Series(oof ).to_csv("Cat_oof.csv", index = False)
 submission.to_csv('submission_cb_light_0.8999.csv', index=False )<load_from_csv>
seed = 1400
tf.random.set_seed(seed)
 my_init = keras.initializers.glorot_uniform(seed=seed)
 model = keras.models.Sequential()
model.add(keras.layers.Input(shape=(x_train.shape[1],)))
 model.add(keras.layers.Dense(360, activation='selu', kernel_initializer=my_init))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(360, activation='selu', kernel_initializer=my_init))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(360, activation='selu', kernel_initializer=my_init))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam', loss = keras.losses.BinaryCrossentropy() , metrics=['accuracy'])
 early_stopping = keras.callbacks.EarlyStopping(monitor='accuracy', patience=3, mode='max', restore_best_weights=True)
 reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='accuracy', factor=0.1, patience=3, mode='max', min_lr=0)
 model.fit(x_train, y_train, epochs = 50, batch_size = 2, callbacks=[reduce_lr, early_stopping], verbose = 1)
 val_loss, val_acc = model.evaluate(x_val, y_val, verbose=1)
 print('
Validation accuracy:', val_acc )
d_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 d_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<set_options>
target_col =[]
test_data = test_one[features].to_numpy()
prediction_nn = model.predict(test_data)
 for i in prediction_nn:
target_col.append(int(round(i[0])))
 output = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': target_col})
 output.to_csv('my_submission.csv', index=False )