Kaggle competition: Titanic - Machine Learning from Disaster
def optimal_rounds(X, verbose=False):

 rounds = []
for i in range(200):
if verbose:
print("Feature ", i)
 cv_res = lgb.cv(params,
lgb.Dataset(X[['var_'+str(i)]], X['target']),
nfold=3,
num_boost_round=100000,
metrics='binary_logloss',
verbose_eval=100 if verbose else None,
early_stopping_rounds=100 
)
 rounds.append(len(cv_res['binary_logloss-mean']))
return rounds<feature_engineering>
pd.read_csv("/kaggle/input/titanic/train.csv" )
opt_rounds = optimal_rounds(train_df, verbose=True )<train_model>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_data.head()
print("Optimal number of rounds: ", opt_rounds)
 print("Optimal number of rounds for var_108: ", opt_rounds[108])
 print("Optimal number of rounds for var_30: ", opt_rounds[30] )<train_model>
train_data['Log_Fare']=np.log(train_data['Fare']+1)
 diagnostic_plots(train_data,'Log_Fare')
num_ones = np.sum(train_df['target'] == 1)
 num_zeros = np.sum(train_df['target'] == 0)
 class LGBNaiveBayes:
def fit(self,X_train, y_train, opt_rounds):
self.clfs = []
for i in range(200):
if i%20 == 0:
print("Fitting var_"+ str(i)+"...")
 params['n_estimators'] = opt_rounds[i]
lgb_clf = lgb.LGBMClassifier(**params)
 lgb_clf.fit(X_train[['var_'+str(i)]], y_train)
 self.clfs.append(lgb_clf)
 def predict_proba(self,X):
log_sum = np.zeros(( X.shape[0],2))
for i in range(200):
log_sum += np.log(self.clfs[i].predict_proba(X[['var_'+str(i)]]))
log_sum += np.array([np.log(num_ones)- np.log(200000), np.log(num_zeros)- np.log(200000)])
 log_sum -= np.array([np.log(num_ones)- np.log(200000), np.log(num_zeros)- np.log(200000)])
 log_prob_x = logsumexp(log_sum, axis=1)
 return np.exp(log_sum - np.atleast_2d(log_prob_x ).T)
<choose_model_class>
train_data['Rec'] = 1/(train_data['Fare']+1 )
clf = LGBNaiveBayes()<find_best_model_class>
train_data['Fare'] = np.log(train_data['Fare']+1 )
features = train_df.columns[2:].values
def cross_validate(nfolds):
sss = StratifiedShuffleSplit(nfolds)
 aucs = []
for train, test in sss.split(train_df[features], train_df['target']):
clf.fit(train_df.loc[train][features], train_df.loc[train]['target'], opt_rounds)
 y_true = train_df.loc[test]['target']
y_pred = clf.predict_proba(train_df.loc[test][features])[:,1]
test_auc = roc_auc_score(y_true, y_pred)
 aucs.append(test_auc)
 print("Test AUC:", test_auc)
 print("Mean test AUC: ", np.mean(aucs))
cross_validate(5 )<save_to_csv>
def inpute(col):
Age = col[0]
Pclass= col[1]
if pd.isnull(Age):
if Pclass==1:
return 37
elif Pclass == 2:
return 29
else:
return 24
else:
return Age
test_df = pd.read_csv('.. /input/test.csv')
 clf.fit(train_df[features], train_df['target'], opt_rounds)
 pred = clf.predict_proba(test_df.iloc[:][features])
 sub_df = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_df["target"] = pred[:,1]
sub_df.to_csv("submission.csv", index=False )<load_from_csv>
train_data['Age'] = train_data[['Age','Pclass']].apply(inpute,axis=1)
 test_data['Age'] = test_data[['Age','Pclass']].apply(inpute,axis=1 )
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )<sort_values>
train_data.drop('Cabin',axis=1,inplace=True )
def missing_value(data, head=False):
missing = pd.DataFrame(data.isnull().sum() ).rename(columns={0:'total'})
 if head:
return missing.sort_values('total', ascending=False ).head(10)
 else:
return missing.sort_values('total', ascending=False )<count_missing_values>
train_data.Age=train_data.Age.astype(int)
 test_data.Age = test_data.Age.astype(int )
missing_value(test_data, head=True )<import_modules>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
train_data.head()
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,cross_val_score,StratifiedKFold,GridSearchCV
from sklearn.feature_selection import RFECV
from sklearn.preprocessing import StandardScaler, normalize, MinMaxScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.pipeline import Pipeline
import imblearn as iml
from sklearn.metrics import f1_score,make_scorer, roc_auc_score,roc_curve, confusion_matrix,auc,make_scorer
from sklearn.decomposition import PCA<categorify>
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Parch",'SibSp']
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=0)
 model.fit(X, y)
 predictions = model.predict(X_test)
 acc_random_forest = round(model.score(X,y)* 100, 2)
 acc_random_forest
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )
<categorify>
pd.read_csv("/kaggle/input/titanic/train.csv" )
<concatenate>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 train_data.head()
def _add_decomposition(df, decomp, ncomp, flag):
for i in range(1, ncomp+1):
df[flag+"_"+str(i)] = decomp[:,i-1] 
<train_model>
train_data['Log_Fare']=np.log(train_data['Fare']+1)
 diagnostic_plots(train_data,'Log_Fare')
<feature_engineering>
train_data['Rec'] = 1/(train_data['Fare']+1 )
idx = features = train_data.columns.values[2:]
for df in [train_data, test_data]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurt(axis=1)
 df['med'] = df[idx].median(axis=1)
 df['var'] = df[idx].var(axis=1)
<feature_engineering>
train_data['Fare'] = np.log(train_data['Fare']+1 )
features = train_data.drop(columns=['target','ID_code'] ).columns
for feature in features:
train_data['r2_'+feature] = np.round(train_data[feature], 2)
 test_data['r2_'+feature] = np.round(test_data[feature], 2)
 train_data['r1_'+feature] = np.round(train_data[feature], 1)
 test_data['r1_'+feature] = np.round(test_data[feature], 1 )<split>
def inpute(col):
Age = col[0]
Pclass= col[1]
if pd.isnull(Age):
if Pclass==1:
return 37
elif Pclass == 2:
return 29
else:
return 24
else:
return Age
train_data=train_data.iloc[:,202:]<split>
train_data['Age'] = train_data[['Age','Pclass']].apply(inpute,axis=1)
 test_data['Age'] = test_data[['Age','Pclass']].apply(inpute,axis=1 )
test_data=test_data.iloc[:,201:]<normalization>
train_data.drop('Cabin',axis=1,inplace=True )
pipeline = Pipeline([('StanderScaler', StandardScaler())])
 train_data = pipeline.fit_transform(train_data)
 test_data = pipeline.transform(test_data )<create_dataframe>
train_data.Age=train_data.Age.astype(int)
 test_data.Age = test_data.Age.astype(int )
train_data = pd.DataFrame(data=train_data,columns=features[200:])
 test_data = pd.DataFrame(data=test_data,columns=features[200:] )<split>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
train_data.head()
<compute_test_metric><EOS> 
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Parch",'SibSp']
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=0)
 model.fit(X, y)
 predictions = model.predict(X_test)
 acc_random_forest = round(model.score(X,y)* 100, 2)
 acc_random_forest
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )