Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<count_values>
import numpy as np
import pandas as pd
import os
train['id_11'].value_counts(dropna=False, normalize=True ).head()<categorify>
X_full = pd.read_csv('.. /input/titanic/train.csv')
 X_test_full = pd.read_csv('.. /input/titanic/test.csv')
 X_full
train['TransactionAmt_to_mean_card1'] = train['TransactionAmt'] / train.groupby(['card1'])['TransactionAmt'].transform('mean')
 train['TransactionAmt_to_mean_card4'] = train['TransactionAmt'] / train.groupby(['card4'])['TransactionAmt'].transform('mean')
 train['TransactionAmt_to_std_card1'] = train['TransactionAmt'] / train.groupby(['card1'])['TransactionAmt'].transform('std')
 train['TransactionAmt_to_std_card4'] = train['TransactionAmt'] / train.groupby(['card4'])['TransactionAmt'].transform('std')
 test['TransactionAmt_to_mean_card1'] = test['TransactionAmt'] / test.groupby(['card1'])['TransactionAmt'].transform('mean')
 test['TransactionAmt_to_mean_card4'] = test['TransactionAmt'] / test.groupby(['card4'])['TransactionAmt'].transform('mean')
 test['TransactionAmt_to_std_card1'] = test['TransactionAmt'] / test.groupby(['card1'])['TransactionAmt'].transform('std')
 test['TransactionAmt_to_std_card4'] = test['TransactionAmt'] / test.groupby(['card4'])['TransactionAmt'].transform('std')
 train['id_02_to_mean_card1'] = train['id_02'] / train.groupby(['card1'])['id_02'].transform('mean')
 train['id_02_to_mean_card4'] = train['id_02'] / train.groupby(['card4'])['id_02'].transform('mean')
 train['id_02_to_std_card1'] = train['id_02'] / train.groupby(['card1'])['id_02'].transform('std')
 train['id_02_to_std_card4'] = train['id_02'] / train.groupby(['card4'])['id_02'].transform('std')
 test['id_02_to_mean_card1'] = test['id_02'] / test.groupby(['card1'])['id_02'].transform('mean')
 test['id_02_to_mean_card4'] = test['id_02'] / test.groupby(['card4'])['id_02'].transform('mean')
 test['id_02_to_std_card1'] = test['id_02'] / test.groupby(['card1'])['id_02'].transform('std')
 test['id_02_to_std_card4'] = test['id_02'] / test.groupby(['card4'])['id_02'].transform('std')
 train['D15_to_mean_card1'] = train['D15'] / train.groupby(['card1'])['D15'].transform('mean')
 train['D15_to_mean_card4'] = train['D15'] / train.groupby(['card4'])['D15'].transform('mean')
 train['D15_to_std_card1'] = train['D15'] / train.groupby(['card1'])['D15'].transform('std')
 train['D15_to_std_card4'] = train['D15'] / train.groupby(['card4'])['D15'].transform('std')
 test['D15_to_mean_card1'] = test['D15'] / test.groupby(['card1'])['D15'].transform('mean')
 test['D15_to_mean_card4'] = test['D15'] / test.groupby(['card4'])['D15'].transform('mean')
 test['D15_to_std_card1'] = test['D15'] / test.groupby(['card1'])['D15'].transform('std')
 test['D15_to_std_card4'] = test['D15'] / test.groupby(['card4'])['D15'].transform('std')
 train['D15_to_mean_addr1'] = train['D15'] / train.groupby(['addr1'])['D15'].transform('mean')
 train['D15_to_mean_addr2'] = train['D15'] / train.groupby(['addr2'])['D15'].transform('mean')
 train['D15_to_std_addr1'] = train['D15'] / train.groupby(['addr1'])['D15'].transform('std')
 train['D15_to_std_addr2'] = train['D15'] / train.groupby(['addr2'])['D15'].transform('std')
 test['D15_to_mean_addr1'] = test['D15'] / test.groupby(['addr1'])['D15'].transform('mean')
 test['D15_to_mean_addr2'] = test['D15'] / test.groupby(['addr2'])['D15'].transform('mean')
 test['D15_to_std_addr1'] = test['D15'] / test.groupby(['addr1'])['D15'].transform('std')
 test['D15_to_std_addr2'] = test['D15'] / test.groupby(['addr2'])['D15'].transform('std' )<string_transform>
X_full.isnull().sum()
train[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = train['P_emaildomain'].str.split('.', expand=True)
 train[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = train['R_emaildomain'].str.split('.', expand=True)
 test[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = test['P_emaildomain'].str.split('.', expand=True)
 test[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = test['R_emaildomain'].str.split('.', expand=True )<define_variables>
X_full['Sex'] = X_full['Sex'].map({"male":1, "female":0})
 X_test_full['Sex'] = X_test_full['Sex'].map({"male":1, "female":0} )
many_null_cols = [col for col in train.columns if train[col].isnull().sum() / train.shape[0] > 0.9]
many_null_cols_test = [col for col in test.columns if test[col].isnull().sum() / test.shape[0] > 0.9]<define_variables>
X_full = X_full.drop(columns = "Cabin")
 X_test = X_test_full.drop(columns = "Cabin" )
big_top_value_cols = [col for col in train.columns if train[col].value_counts(dropna=False, normalize=True ).values[0] > 0.9]
big_top_value_cols_test = [col for col in test.columns if test[col].value_counts(dropna=False, normalize=True ).values[0] > 0.9]<drop_column>
X_full['TravelAlone']=np.where(( X_full["SibSp"]+X_full["Parch"])>0, 0, 1)
 X_full.drop('SibSp', axis=1, inplace=True)
 X_full.drop('Parch', axis=1, inplace=True)
 X_full
cols_to_drop = list(set(many_null_cols + many_null_cols_test + big_top_value_cols + big_top_value_cols_test + one_value_cols+ one_value_cols_test))
cols_to_drop.remove('isFraud')
 len(cols_to_drop )<drop_column>
X_test['TravelAlone']=np.where(( X_test["SibSp"]+X_test["Parch"])>0, 0, 1)
 X_test.drop('SibSp', axis=1, inplace=True)
 X_test.drop('Parch', axis=1, inplace=True)
 X_test
train = train.drop(cols_to_drop, axis=1)
 test = test.drop(cols_to_drop, axis=1 )<define_variables>
object_cols = [col for col in X_full.columns if X_full[col].dtype == "object"]
object_nunique = list(map(lambda col: X_full[col].nunique() , object_cols))
d = dict(zip(object_cols, object_nunique))
sorted(d.items() , key=lambda x: x[1] )
cat_cols = ['id_12', 'id_13', 'id_14', 'id_15', 'id_16', 'id_17', 'id_18', 'id_19', 'id_20', 'id_21', 'id_22', 'id_23', 'id_24', 'id_25', 'id_26', 'id_27', 'id_28', 'id_29',
'id_30', 'id_31', 'id_32', 'id_33', 'id_34', 'id_35', 'id_36', 'id_37', 'id_38', 'DeviceType', 'DeviceInfo', 'ProductCD', 'card4', 'card6', 'M4','P_emaildomain',
'R_emaildomain', 'card1', 'card2', 'card3', 'card5', 'addr1', 'addr2', 'M1', 'M2', 'M3', 'M5', 'M6', 'M7', 'M8', 'M9',
'P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3', 'R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']
for col in cat_cols:
if col in train.columns:
le = LabelEncoder()
le.fit(list(train[col].astype(str ).values)+ list(test[col].astype(str ).values))
train[col] = le.transform(list(train[col].astype(str ).values))
test[col] = le.transform(list(test[col].astype(str ).values))<prepare_x_and_y>
object_cols = [col for col in X_test.columns if X_test[col].dtype == "object"]
object_nunique = list(map(lambda col: X_test[col].nunique() , object_cols))
d = dict(zip(object_cols, object_nunique))
sorted(d.items() , key=lambda x: x[1] )
X = train.sort_values('TransactionDT' ).drop(['isFraud', 'TransactionDT', 'TransactionID'], axis=1)
 y = train.sort_values('TransactionDT')['isFraud']
X_test = test.drop(['TransactionDT', 'TransactionID'], axis=1)
 del train
test = test[["TransactionDT", 'TransactionID']]<categorify>
drop_X_full = X_full.drop(columns = ['Ticket', 'Name'])
 drop_X_test = X_test.drop(columns = ['Ticket', 'Name'] )
def clean_inf_nan(df):
return df.replace([np.inf, -np.inf], np.nan)
 X = clean_inf_nan(X)
 X_test = clean_inf_nan(X_test )<set_options>
OH_X = pd.get_dummies(drop_X_full, columns=["Pclass","Embarked"])
 OH_X
gc.collect()<string_transform>
OH_X_test = pd.get_dummies(drop_X_test, columns = ['Pclass', 'Embarked'])
 OH_X_test
n_fold = 5
folds = TimeSeriesSplit(n_splits=n_fold)
 folds = KFold(n_splits=5 )<init_hyperparams>
null_data = OH_X_test[OH_X_test['Fare'].isnull() ]
null_data
params = {'num_leaves': 256,
'min_child_samples': 79,
'objective': 'binary',
'max_depth': 13,
'learning_rate': 0.03,
"boosting_type": "gbdt",
"subsample_freq": 3,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': 0.3,
'reg_lambda': 0.3,
'colsample_bytree': 0.9,
}
result_dict_lgb = train_model_classification(X=X, X_test=X_test, y=y, params=params, folds=folds, model_type='lgb', eval_metric='auc', plot_feature_importance=True,
verbose=500, early_stopping_rounds=200, n_estimators=5000, averaging='usual', n_jobs=-1 )<save_to_csv>
OH_X_test["Fare"].fillna(OH_X_test["Fare"].median(skipna=True), inplace=True)
 OH_X_test["Age"].fillna(OH_X_test["Age"].median(skipna=True), inplace=True)
 OH_X_test
sub['isFraud'] = result_dict_lgb['prediction']
sub.to_csv('submission.csv', index=False )<save_to_csv>
OH_y = OH_X['Survived']
OH_X = OH_X.drop(columns = 'Survived')
 OH_X
pd.DataFrame(result_dict_lgb['oof'] ).to_csv('lgb_oof.csv', index=False )<load_from_csv>
X_train, X_valid, y_train, y_valid = train_test_split(OH_X, OH_y,
train_size=0.8, test_size=0.2,
random_state=0 )
train_identity=pd.read_csv('.. /input/train_identity.csv')
 test_identity=pd.read_csv('.. /input/test_identity.csv')
 train_transaction=pd.read_csv('.. /input/train_transaction.csv')
 test_transaction=pd.read_csv('.. /input/test_transaction.csv' )<merge>
X_train['Age'].fillna(X_train['Age'].median(skipna=True), inplace=True)
 X_train.isnull().sum()
train=pd.merge(train_transaction,train_identity,how='left',on='TransactionID')
 test=pd.merge(test_transaction,test_identity,how='left',on='TransactionID' )<drop_column>
X_valid["Age"].fillna(X_valid["Age"].median(skipna=True), inplace=True)
 X_valid.isnull().sum()
del train_identity,test_identity,train_transaction,test_transaction<count_values>
my_model = XGBClassifier(n_estimators=1000, learning_rate=0.001)
 my_model.fit(X_train, y_train,
early_stopping_rounds=10,
eval_set=[(X_valid, y_valid)],
verbose=False)
 predictions = my_model.predict(X_valid)
 print("Mean Absolute Error: " + str(mean_absolute_error(predictions, y_valid)) )
<count_unique_values><EOS> 
test_preds = my_model.predict(OH_X_test)
 submission = pd.DataFrame({"PassengerId":OH_X_test['PassengerId'], "Survived":test_preds})
 submission.to_csv('submission.csv', index=False)
 submission