Kaggle competition: Titanic - Machine Learning from Disaster
gnb = GaussianNB()
gnb.fit(X_train, y_train )<predict_on_test>
%matplotlib inline
y_preds_gnb = gnb.predict(X_test)
 print(accuracy_score(y_test, y_preds_gnb))<load_from_csv>
train= pd.read_csv('/kaggle/input/titanic/train.csv' )
test_data = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
def impute_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age):
if Pclass==1:
return 37
elif Pclass == 2:
return 29
else :
return 24
else:
return Age
X_test_data = test_data.iloc[:,1:202]<predict_on_test>
train['Age']=train[['Age','Pclass']].apply(impute_age,axis = 1 )
y_preds_test_data_gnb = gnb.predict(X_test_data )<save_to_csv>
train.drop('Cabin',inplace = True,axis =1)
my_submission_gnb = pd.DataFrame({'ID_code': test_data.ID_code, 'target': y_preds_test_data_gnb})
 my_submission_gnb.to_csv('submission_gnb.csv', index=False )<import_modules>
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True )
from catboost import CatBoostClassifier<train_model>
train = pd.concat([train,sex,embark],axis=1 )
cat = CatBoostClassifier(iterations=3000, learning_rate=0.03, objective="Logloss", eval_metric='AUC')
 cat.fit(X_train, y_train )<predict_on_test>
train.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )
y_preds_cat = cat.predict(X_test)
 print(accuracy_score(y_test, y_preds_cat))<predict_on_test>
st = StandardScaler()
y_preds_test_data_cat = cat.predict(X_test_data )<save_to_csv>
feature_scale = ['Age','Fare']
train[feature_scale] = st.fit_transform(train[feature_scale])
my_submission_cat = pd.DataFrame({'ID_code': test_data.ID_code, 'target': y_preds_test_data_cat})
 my_submission_cat.to_csv('submission_cat.csv', index=False )<import_modules>
x = train.drop(['Survived'],axis=1)
 y = train['Survived']
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
import time<categorify>
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y<choose_model_class>
tree = DecisionTreeClassifier()
tree.fit(x,y)
 tree.score(x,y )
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=False, random_state=42 )<init_hyperparams>
test = pd.read_csv('/kaggle/input/titanic/test.csv' )
lgbm_params = {'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': -1
}<split>
test2 = test.copy()
prediction_lgb_new = np.zeros(len(X_test_data))
for fold_n,(train_index, valid_index)in enumerate(folds.split(train_data,y)) :
print('Fold', fold_n)
 X_training, X_validation = train_data.iloc[train_index], train_data.iloc[valid_index]
y_training, y_validation = y.iloc[train_index], y.iloc[valid_index]
X_training, y_training = augment(X_training.values, y_training.values)
 X_training = pd.DataFrame(X_training)
 training_data = lgb.Dataset(X_training, label=y_training)
 validation_data = lgb.Dataset(X_validation, label=y_validation)
 model = lgb.train(lgbm_params,training_data,num_boost_round=1000000,
valid_sets = [training_data, validation_data],verbose_eval=1000,early_stopping_rounds = 3000)
 prediction_lgb_new += model.predict(X_test_data, num_iteration=model.best_iteration)/folds.n_splits<save_to_csv>
test['Age']=test[['Age','Pclass']].apply(impute_age,axis = 1 )
my_submission_lgb_new = pd.DataFrame({'ID_code': test_data.ID_code, 'target': prediction_lgb_new})
 my_submission_lgb_new.to_csv('submission_lgb_new.csv', index=False )<save_to_csv>
test.drop('Cabin',inplace = True,axis =1 )
my_submission_cat_lgb_gnb = pd.DataFrame({'ID_code': test_data.ID_code,
'target':(y_preds_test_data_cat + y_preds_test_data_gnb)/2})
 my_submission_cat_lgb_gnb.to_csv('my_submission_cat_gnb.csv', index=False )<load_from_csv>
sex = pd.get_dummies(test['Sex'],drop_first=True)
 embark = pd.get_dummies(test['Embarked'],drop_first=True )
test = pd.read_csv(".. /input/test.csv")
 train = pd.read_csv(".. /input/train.csv" )<prepare_x_and_y>
test = pd.concat([test,sex,embark],axis=1 )
ytrain = train['target']
xtrain = train.iloc[:,2:]
xtest = test.iloc[:,1:]<train_model>
test.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )
rus = RandomUnderSampler(random_state=1, replacement=True)
 xtrain1,ytrain1 = rus.fit_sample(xtrain, ytrain)
<split>
test['Fare'].fillna(test['Fare'].mean() ,inplace=True )
param = {
'num_leaves': 2,
'learning_rate': 0.1,
'feature_fraction': 0.2,
'max_depth': -1,
'objective': 'binary',
'boosting_type': 'gbdt',
'metric': 'auc',
}
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=4590)
 oof = np.zeros(len(xtrain))
ypred = np.zeros(len(xtest))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(xtrain1,ytrain1)) :
print("fold {}".format(fold_))
rus = RandomUnderSampler(random_state=fold_, replacement=True)
 xtrain1,ytrain1 = rus.fit_sample(xtrain, ytrain)
 trn_data = lgb.Dataset(xtrain1[trn_idx], label=ytrain1[trn_idx])
 val_data = lgb.Dataset(xtrain1[val_idx], label=ytrain1[val_idx])
 num_round = 30000
model = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 300)
 oof += model.predict(xtrain)/ 5
ypred += model.predict(xtest)/ 5
roc_auc_score(ytrain,oof )<save_to_csv>
feature_scale = ['Age','Fare']
test[feature_scale] = st.fit_transform(test[feature_scale] )
df = pd.DataFrame({'ID_code':test['ID_code'],'target':ypred})
 df.to_csv('undersamping.csv',index=None )<compute_test_metric>
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
roc_auc_score(ytrain,oof )<load_from_csv>
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5 )
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )<drop_column>
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5)
train.pop("ID_code")
 test.pop("ID_code" )<count_values>
model.fit(x,y )
train["target"].value_counts()<prepare_x_and_y>
y_predicted = model.predict(test )
y=train["target"]<drop_column>
submission = pd.DataFrame({
"PassengerId":test2['PassengerId'],
"Survived":y_predicted
} )
train.pop("target" )<import_modules>
submission.to_csv('first_kaggale_titanic_submission.csv',index=False )