Kaggle competition: Titanic - Machine Learning from Disaster
test['premium'].value_counts()<feature_engineering>
df_train = pd.read_csv(".. /input/titanic/train.csv")
 df_test = pd.read_csv(".. /input/titanic/test.csv" )
test['premium'] = test['premium'].fillna(False )<drop_column>
X_train = df_train.copy()
X_test = df_test.copy()
train.drop(columns='isUpgradedUser', inplace=True)
 test.drop(columns='isUpgradedUser', inplace=True )<drop_column>
X_train = X_train.set_index('PassengerId')
 X_test = X_test.set_index('PassengerId' )
train.drop(columns=['random_number'], inplace=True)
 test.drop(columns=['random_number'], inplace=True )<load_from_csv>
X_train_null = X_train.isnull().sum()
X_train_null
<count_duplicates>
X_train_null/len(X_train)* 100
print(train[train.duplicated() ].shape[0])
 print(train[train['total_transaction'].isnull() ].shape[0])
 print(train[train['average_transfer_trx'] < 0].shape[0] )<drop_column>
X_test_null = X_test.isnull().sum()
X_test_null
train.drop(train[train['average_transfer_trx'] < 0].index, inplace=True )<count_duplicates>
X_test_null/len(X_test)* 100
train.drop(train[train.duplicated() ].index, inplace=True )<drop_column>
X_train['Age'] = X_train['Age'].fillna(X_train['Age'].mean())
 X_test['Age'] = X_test['Age'].fillna(X_test['Age'].mean() )
train.drop(train[train['total_transaction'].isnull() ].index, inplace=True )<count_values>
X_train['Embarked'].value_counts()
print(train[train['num_transfer_trx'] != 0]['isChurned'].value_counts())
 print(train[train['num_transfer_trx'] == 0]['isChurned'].value_counts() )<feature_engineering>
X_train['Embarked'] = X_train['Embarked'].fillna('S')
def update_transaction(x):
if(x['num_transaction'] != 0):
x['num_transaction'] -= x['num_transfer_trx']
x['total_transaction'] -=(x['average_transfer_trx'] * x['num_transfer_trx'])
 if(x['total_transaction'] > -1e-8 and x['total_transaction'] < 1e-8):
x['total_transaction'] = 0
return x
train = train.apply(update_transaction, axis=1)
 test = test.apply(update_transaction, axis=1 )<drop_column>
X_train = X_train.drop('Cabin', axis=1)
 X_test = X_test.drop('Cabin', axis=1 )
train.drop(columns=['num_transfer_trx', 'average_transfer_trx', 'min_transfer_trx', 'max_transfer_trx'], inplace=True)
 test.drop(columns=['num_transfer_trx', 'average_transfer_trx', 'min_transfer_trx', 'max_transfer_trx'], inplace=True )<feature_engineering>
X_test['Fare'] = X_test['Fare'].fillna(X_test['Fare'].mean() )
train['average_recharge_trx'] /= 15000
train['min_recharge_trx'] /= 15000
train['max_recharge_trx'] /= 15000
train['average_topup_trx'] /= 15000
train['min_topup_trx'] /= 15000
train['max_topup_trx'] /= 15000
train['total_transaction'] /= 15000<feature_engineering>
X_train_null = X_train.isnull().sum()
X_train_null
test['average_recharge_trx'] /= 15000
test['min_recharge_trx'] /= 15000
test['max_recharge_trx'] /= 15000
test['average_topup_trx'] /= 15000
test['min_topup_trx'] /= 15000
test['max_topup_trx'] /= 15000
test['total_transaction'] /= 15000<feature_engineering>
X_test_null = X_test.isnull().sum()
X_test_null
train['total_recharge'] = train['average_recharge_trx'] * train['num_recharge_trx']
train['total_topup'] = train['average_topup_trx'] * train['num_topup_trx']
test['total_recharge'] = test['average_recharge_trx'] * test['num_recharge_trx']
test['total_topup'] = test['average_topup_trx'] * test['num_topup_trx']<define_variables>
X_train = X_train.drop(['Name', 'Ticket', 'Survived'], axis = 1)
 X_train
cols = ['userId',
'date_collected',
'num_recharge_trx',
'average_recharge_trx',
'max_recharge_trx',
'min_recharge_trx',
'num_topup_trx',
'average_topup_trx',
'max_topup_trx',
'min_topup_trx',
'total_recharge',
'total_topup',
'num_transaction',
'total_transaction',
'isActive',
'isVerifiedPhone',
'isVerifiedEmail',
'blocked',
'premium',
'super',
'userLevel',
'pinEnabled',
'isChurned']
train = train[cols]
test = test[cols[:-1]]<categorify>
X_test = X_test.drop(['Name', 'Ticket'], axis = 1)
 X_test
encode = ['premium', 'super', 'pinEnabled']
lbl = LabelEncoder()
for col in encode:
train[col] = lbl.fit_transform(train[col].values)
 test[col] = lbl.transform(test[col].values )<drop_column>
X_test['family'] = X_test['SibSp'] + X_test['Parch']
X_test = X_test.drop(['SibSp','Parch'], axis=1)
 X_test
train.drop(columns=[
'userId',
'date_collected',
], inplace=True)
 test.drop(columns=[
'userId',
'date_collected',
], inplace=True )<count_duplicates>
X_train['family'] = X_train['SibSp'] + X_train['Parch']
X_train = X_train.drop(['SibSp','Parch'], axis=1)
 X_train
no_dup_train = train.drop(train[train.duplicated() ].index ).copy()
dup_index = no_dup_train[no_dup_train.drop(columns='isChurned' ).duplicated() ].index<filter>
Y_train = df_train['Survived']
Y_train
def get_same_row(index, data=train):
row = data.loc[index]
return train[(train['num_recharge_trx'] == row['num_recharge_trx'])& 
(train['average_recharge_trx'] == row['average_recharge_trx'])& 
(train['max_recharge_trx'] == row['max_recharge_trx'])& 
(train['min_recharge_trx'] == row['min_recharge_trx'])& 
(train['total_recharge'] == row['total_recharge'])& 
(train['num_topup_trx'] == row['num_topup_trx'])& 
(train['average_topup_trx'] == row['average_topup_trx'])& 
(train['max_topup_trx'] == row['max_topup_trx'])& 
(train['min_topup_trx'] == row['min_topup_trx'])& 
(train['total_topup'] == row['total_topup'])& 
(train['num_transaction'] == row['num_transaction'])& 
(train['total_transaction'] == row['total_transaction'])& 
(train['isActive'] == row['isActive'])& 
(train['isVerifiedPhone'] == row['isVerifiedPhone'])& 
(train['isVerifiedEmail'] == row['isVerifiedEmail'])& 
(train['blocked'] == row['blocked'])& 
(train['premium'] == row['premium'])& 
(train['super'] == row['super'])& 
(train['userLevel'] == row['userLevel'])& 
(train['pinEnabled'] == row['pinEnabled'])]<feature_engineering>
X_train_null = X_train.isnull().sum()
X_train_null
def update_row(threshold=0.3, dup_index=dup_index):
for a, i in enumerate(dup_index):
dup_row = get_same_row(i)
 index = dup_row.index
zero, one = dup_row['isChurned'].value_counts() [0], dup_row['isChurned'].value_counts() [1]
if(min(zero, one)/(zero+one)<= threshold):
mode = dup_row.mode() ['isChurned'][0]
for idx in index:
train.at[idx, 'isChurned'] = mode
if(a % 100 == 0):
print(a )<count_values>
X_test_null = X_test.isnull().sum()
X_test_null
train['isChurned'].value_counts()<count_values>
X_train = pd.get_dummies(X_train, columns=['Sex', 'Embarked'], drop_first=True)
 X_train
train['isChurned'].value_counts()<prepare_x_and_y>
X_test = pd.get_dummies(X_test, columns=['Sex', 'Embarked'], drop_first=True)
 X_test
df_x = train.drop(['isChurned'], axis=1)
 df_y = train['isChurned']
x = df_x
y = df_y<normalization>
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
scaler = StandardScaler()
x = scaler.fit_transform(x)
 new_test = scaler.transform(test )<split>
Rf = RandomForestClassifier()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42 )<import_modules>
param_grid = {"n_estimators":[20, 40, 100, 120, 150],'max_depth': np.arange(1, 20), 'max_features': ['auto', 'sqrt'],
'criterion' :['gini', 'entropy']}
from sklearn.metrics import confusion_matrix, f1_score<train_model>
RF_grid = GridSearchCV(Rf, param_grid=param_grid, cv=10, scoring='accuracy' )
def train_model(models, x=x_train, y=y_train):
for key, model in models.items() :
print(f'Currently Training {key}')
 model.fit(x, y )<predict_on_test>
RF_grid.fit(X_train, Y_train )
def preds(models, x_test=x_test, y_test=y_test, new_test=new_test):
preds = {
'models': [],
'val_f1_score': [],
'train_f1_score': [],
'confusion_matrix': [],
'prediction': []
}
for key, model in models.items() :
preds['models'].append(key)
 test_pred = model.predict(x_test)
 train_pred = model.predict(x_train)
 preds['val_f1_score'].append(f1_score(test_pred, y_test))
preds['train_f1_score'].append(f1_score(train_pred, y_train))
preds['confusion_matrix'].append(confusion_matrix(test_pred, y_test))
preds['prediction'].append(model.predict(new_test))
return pd.DataFrame(preds )<define_variables>
RF_grid.best_params_
evaluation = []<import_modules>
RF_cv_result = RF_grid.cv_results_['mean_test_score']
from sklearn.linear_model import LogisticRegression, Perceptron, RidgeClassifier, SGDClassifier<choose_model_class>
from xgboost import XGBClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import RandomizedSearchCV
linear_models = {
'LogisticRegression': LogisticRegression() ,
'Perceptron': Perceptron() ,
'RidgeClassifier': RidgeClassifier() ,
'SGDClassifier': SGDClassifier()
}<train_model>
xgb_model = XGBClassifier()
train_model(linear_models )<predict_on_test>
distributions = {"n_estimators":[20, 40, 100, 120, 150],'max_depth': np.arange(1, 20),
'criterion' :['gini', 'entropy'],
'learning_rate' : [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3]}
evaluation.append(preds(linear_models))<import_modules>
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)
 XG_grid = RandomizedSearchCV(xgb_model, distributions, scoring="neg_log_loss", n_jobs=-1, cv=kfold )
from sklearn.naive_bayes import GaussianNB<define_variables>
XG_grid.fit(X_train, Y_train )
naive_bayes = {
'GaussianNB': GaussianNB()
}<train_model>
XG_grid.best_params_
train_model(naive_bayes )<predict_on_test>
XG_grid_result = XG_grid.cv_results_['mean_test_score']
XG_grid_result
evaluation.append(preds(naive_bayes))<import_modules>
predictions = RF_grid.predict(X_test)
 predictions
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier<choose_model_class>
output = pd.DataFrame({'PassengerId': df_test.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )