Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train['id_02_bins'] = train['id_02']
train['id_02_bins'] = pd.cut(train['id_02_bins'], 10, labels=False)
 test['id_02_bins'] = test['id_02']
test['id_02_bins'] = pd.cut(test['id_02_bins'], 10, labels=False )<data_type_conversions>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 all_data = [train,test]
train['id_11_flag'] = np.where(train['id_11'].isnull() ,'F','T')
 test['id_11_flag'] = np.where(test['id_11'].isnull() ,'F','T')
 train['id_11_flag'] = train['id_11_flag'].astype('category')
 test['id_11_flag'] = test['id_11_flag'].astype('category')
 train['id_11_residual'] = train['id_11']
train['id_11_residual'] = np.where(train['id_11'] == 100.0,'100',np.where(train['id_11_residual'].isnull() ,'None','Residual'))
train['id_11_residual'] = train['id_11_residual'].astype('category')
 test['id_11_residual'] = test['id_11']
test['id_11_residual'] = np.where(test['id_11'] == 100.0,'100',np.where(test['id_11_residual'].isnull() ,'None','Residual'))
test['id_11_residual'] = test['id_11_residual'].astype('category' )<feature_engineering>
print(train[['Pclass', 'Sex']].groupby(['Sex'], as_index=False ).mean() )
train['Transaction_DOW'] = np.floor(( train['TransactionDT'] /(3600 * 24)- 1)% 7)
 test['Transaction_DOW'] = np.floor(( test['TransactionDT'] /(3600 * 24)- 1)% 7)
 train['Transaction_H'] = np.floor(train['TransactionDT'] / 3600)% 24
test['Transaction_H'] = np.floor(test['TransactionDT'] / 3600)% 24<set_options>
print(train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean() )
warnings.filterwarnings("ignore" )<define_variables>
women = train.loc[train.Sex == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('The rate of survival for women is:', rate_women )
cat_types = ['ProductCD','card1','card2','card3','card4','card5','card6','addr1','addr2','P_emaildomain','R_emaildomain',
'M1','M2','M3','M4','M5','M6','M7','M8','M9','DeviceType','DeviceInfo','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','id_02_bins','id_11_flag','id_11_residual']
train[cat_types] = train[cat_types].astype('object')
 test[cat_types] = test[cat_types].astype('object' )<categorify>
men = train.loc[train.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('The rate of survival for men is:', rate_men )
train = train.fillna(-999 )<categorify>
for dataset in all_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
for f in train.columns:
if train[f].dtype=='object':
lbl = LabelEncoder()
lbl.fit(list(train[f].values)+ list(test[f].values))
train[f] = lbl.transform(list(train[f].values))
test[f] = lbl.transform(list(test[f].values))
train = train.reset_index()
test = test.reset_index() 
<drop_column>
for dataset in all_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
features = list(train)
 features.remove('isFraud')
 target = 'isFraud'<split>
for dataset in all_data:
dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 train.head()
bayesian_tr_idx, bayesian_val_idx = train_test_split(train, test_size = 0.3, random_state = 42, stratify = train[target])
 bayesian_tr_idx = bayesian_tr_idx.index
bayesian_val_idx = bayesian_val_idx.index<choose_model_class>
for dataset in all_data:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train['Title'], train['Sex'] )
def LGB_bayesian(
 num_leaves,
bagging_fraction,
feature_fraction,
min_child_weight,
min_data_in_leaf,
max_depth,
reg_alpha,
reg_lambda 
):
num_leaves = int(num_leaves)
 min_data_in_leaf = int(min_data_in_leaf)
 max_depth = int(max_depth)
 assert type(num_leaves)== int
assert type(min_data_in_leaf)== int
assert type(max_depth)== int
param = {
'num_leaves': num_leaves,
'min_data_in_leaf': min_data_in_leaf,
'min_child_weight': min_child_weight,
'bagging_fraction' : bagging_fraction,
'feature_fraction' : feature_fraction,
'max_depth': max_depth,
'reg_alpha': reg_alpha,
'reg_lambda': reg_lambda,
'objective': 'binary',
'save_binary': True,
'seed': 11,
'feature_fraction_seed': 11,
'bagging_seed': 11,
'drop_seed': 11,
'data_random_seed': 11,
'boosting_type': 'gbdt',
'verbose': 1,
'is_unbalance': False,
'boost_from_average': True,
'metric':'auc'}
oof = np.zeros(len(train))
trn_data= lgb.Dataset(train.iloc[bayesian_tr_idx][features].values, label=train.iloc[bayesian_tr_idx][target].values)
 val_data= lgb.Dataset(train.iloc[bayesian_val_idx][features].values, label=train.iloc[bayesian_val_idx][target].values)
 clf = lgb.train(param, trn_data, num_boost_round=50, valid_sets = [trn_data, val_data], verbose_eval=0, early_stopping_rounds = 50)
 oof[bayesian_val_idx] = clf.predict(train.iloc[bayesian_val_idx][features].values, num_iteration=clf.best_iteration)
 score = roc_auc_score(train.iloc[bayesian_val_idx][target].values, oof[bayesian_val_idx])
 return score<init_hyperparams>
for dataset in all_data:
dataset['Title'] = dataset['Title'].replace(['Capt', 'Col', 'Countess', 'Don', 'Dr', \
'Jonkheer', 'Major', 'Rev', 'Sir'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Ms', 'Mlle'], 'Miss')
 dataset['Title'] = dataset['Title'].replace(['Mme','Lady'], 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
bounds_LGB = {
'num_leaves':(31, 500),
'min_data_in_leaf':(20, 200),
'bagging_fraction' :(0.1, 0.9),
'feature_fraction' :(0.1, 0.9),
'min_child_weight':(0.00001, 0.01),
'reg_alpha':(1, 2),
'reg_lambda':(1, 2),
'max_depth':(-1,50),
}<choose_model_class>
title_mapping = {'Mr': 1,'Mrs': 2, 'Miss': 3, 'Master': 4, 'Rare': 5}
for dataset in all_data:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()
LGB_BO = BayesianOptimization(LGB_bayesian, bounds_LGB, random_state=42 )<define_variables>
title_mapping = {"S": 1, "C": 2, "Q": 3}
for dataset in all_data:
dataset['Embarked'] = dataset['Embarked'].map(title_mapping)
 dataset['Embarked'] = dataset['Embarked'].fillna(0)
 train.head()
init_points = 10
n_iter = 15<find_best_params>
for dataset in all_data:
dataset['Fare'] = dataset['Fare'].fillna(train['Fare'].median() )
with warnings.catch_warnings() :
warnings.filterwarnings('ignore')
 LGB_BO.maximize(init_points=init_points, n_iter=n_iter, acq='ucb', xi=0.0, alpha=1e-6 )<load_from_csv>
train = train.drop(['Ticket', 'Cabin', 'Name', 'PassengerId'], axis=1)
 test = test.drop(['Ticket', 'Cabin', 'Name'], axis=1)
 all_data = [train, test]
train_identity = pd.read_csv('.. /input/train_identity.csv')
 train_transaction = pd.read_csv('.. /input/train_transaction.csv')
 train = train_transaction.merge(train_identity, on='TransactionID', how='left')
 del train_identity, train_transaction
gc.collect()
test_identity = pd.read_csv('.. /input/test_identity.csv')
 test_transaction = pd.read_csv('.. /input/test_transaction.csv')
 test = test_transaction.merge(test_identity, on='TransactionID', how='left')
 del test_identity, test_transaction
gc.collect()
train['TransactionDecimal'] =(( train['TransactionAmt'] - train['TransactionAmt'].astype(int)) * 1000 ).astype(int)
 test['TransactionDecimal'] =(( test['TransactionAmt'] - test['TransactionAmt'].astype(int)) * 1000 ).astype(int)
 train['id_02_bins'] = train['id_02']
train['id_02_bins'] = pd.cut(train['id_02_bins'], 10, labels=False)
 test['id_02_bins'] = test['id_02']
test['id_02_bins'] = pd.cut(test['id_02_bins'], 10, labels=False)
 train['id_11_flag'] = np.where(train['id_11'].isnull() ,'F','T')
 test['id_11_flag'] = np.where(test['id_11'].isnull() ,'F','T')
 train['id_11_flag'] = train['id_11_flag'].astype('category')
 test['id_11_flag'] = test['id_11_flag'].astype('category')
 train['id_11_residual'] = train['id_11']
train['id_11_residual'] = np.where(train['id_11'] == 100.0,'100',np.where(train['id_11_residual'].isnull() ,'None','Residual'))
train['id_11_residual'] = train['id_11_residual'].astype('category')
 test['id_11_residual'] = test['id_11']
test['id_11_residual'] = np.where(test['id_11'] == 100.0,'100',np.where(test['id_11_residual'].isnull() ,'None','Residual'))
test['id_11_residual'] = test['id_11_residual'].astype('category')
 train['Transaction_DOW'] = np.floor(( train['TransactionDT'] /(3600 * 24)- 1)% 7)
 test['Transaction_DOW'] = np.floor(( test['TransactionDT'] /(3600 * 24)- 1)% 7)
 train['Transaction_H'] = np.floor(train['TransactionDT'] / 3600)% 24
test['Transaction_H'] = np.floor(test['TransactionDT'] / 3600)% 24<feature_engineering>
y = train['Survived']
features = ['Pclass', 'Sex', 'Embarked', 'Title', 'Parch']
X = pd.get_dummies(train[features])
 X_test = pd.get_dummies(test[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print('Your submission was successfully saved!' )
<prepare_x_and_y><EOS> 
np.count_nonzero(predictions==1)/np.count_nonzero(predictions==0 )