Kaggle competition: Titanic - Machine Learning from Disaster
submission.to_csv("Final.csv",index=False )<set_options>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
warnings.filterwarnings('ignore')
 sns.set(style="white")
 %matplotlib inline
%matplotlib inline<save_to_csv>
Col_with_missing = [col for col in train_data.columns if train_data[col].isnull().any() ]
print(Col_with_missing )
def rmse(y_test, y_pred):
return np.sqrt(((y_test - y_pred)** 2 ).mean())
 def rmse_cv(estimator, X_test, y_test):

 return - np.sqrt(((estimator.predict(X_test)- y_test)** 2 ).mean())
 def rmse_log(estimator, X_test, y_test):

 return - np.sqrt(((np.exp(estimator.predict(X_test)) - np.exp(y_test)) ** 2 ).mean())
 def submit_file(y_pred, filename):
pd.Series(y_pred, name='DelayTime' ).to_csv('{}.csv'.format(filename),
index_label='id', header=True)
 def hacking_score(pred, y_train):
zeros_submit_score = 68.79140
return pred + zeros_submit_score - np.sqrt(np.square(y_train.values ).mean() )<load_from_csv>
Col_with_missing = [col for col in test_data.columns if test_data[col].isnull().any() ]
print(Col_with_missing )
train_df = pd.read_csv('.. /input/train_features.csv')
 test_df = pd.read_csv('.. /input/test_features.csv')
 target = pd.read_csv('.. /input/train_target.csv', index_col='id')
 train_df.head()<feature_engineering>
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index)
 print("Categorical variables:")
 print(object_cols )
train_df['DepHour'] = train_df['CRSDepTime'] // 100
train_df['DepHour'].replace(to_replace=24, value=0, inplace=True)
 train_df['DepTime'] = train_df['DepHour'] + train_df['CRSDepTime'] % 100 / 60
test_df['DepHour'] = test_df['CRSDepTime'] // 100
test_df['DepHour'].replace(to_replace=24, value=0, inplace=True)
 test_df['DepTime'] = test_df['DepHour'] + test_df['CRSDepTime'] % 100 / 60
train_df['ArrHour'] = train_df['CRSArrTime'] // 100
train_df['ArrHour'].replace(to_replace=24, value=0, inplace=True)
 train_df['ArrTime'] = train_df['ArrHour'] + train_df['CRSArrTime'] % 100 / 60
test_df['ArrHour'] = test_df['CRSArrTime'] // 100
test_df['ArrHour'].replace(to_replace=24, value=0, inplace=True)
 test_df['ArrTime'] = test_df['ArrHour'] + test_df['CRSArrTime'] % 100 / 60
test_df.drop(['CRSDepTime', 'CRSArrTime', 'Year', ], axis=1, inplace=True)
 train_df.drop(['CRSDepTime', 'CRSArrTime', 'Year', ], axis=1, inplace=True)
 train_df['target'] = target<drop_column>
feature_name=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']
X=train_data[feature_name]
y=train_data["Survived"]
X_test=test_data[feature_name]
test_df.drop(['CRSElapsedTime', ], axis=1, inplace=True)
 train_df.drop(['CRSElapsedTime', ], axis=1, inplace=True )<data_type_conversions>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2 )
train_df.fillna('EVIL', inplace=True)
 test_df.fillna('EVIL', inplace=True )<data_type_conversions>
my_imputer=SimpleImputer(strategy="most_frequent")
 imputed_X_train= pd.DataFrame(my_imputer.fit_transform(X_train))
imputed_X_test=pd.DataFrame(my_imputer.transform(X_test))
imputed_X_valid=pd.DataFrame(my_imputer.transform(X_valid))
imputed_X_train.index = X_train.index
imputed_X_valid.index = X_valid.index
imputed_X_test.index = X_test.index
imputed_X_train.columns=X_train.columns
imputed_X_valid.columns=X_valid.columns
imputed_X_test.columns=X_test.columns
<categorify>
Col_with_missing_2 = [col for col in imputed_X_test.columns if imputed_X_test[col].isnull().any() ]
print(Col_with_missing_2 )
OHE = OneHotEncoder(handle_unknown='ignore')
 ohe_x_train = OHE.fit_transform(train_df.iloc[:, [0,1,2,3,4,5,6,7,9,11]])
 num_x_train = train_df.iloc[:, [8, 10, 12]]
ohe_x_test = OHE.transform(test_df.iloc[:, [0,1,2,3,4,5,6,7,9,11]])
 num_x_test = test_df.iloc[:, [8, 10, 12]]
X = hstack([ohe_x_train, num_x_train] ).tocsr()
X_test = hstack([ohe_x_test, num_x_test] ).tocsr()
y = train_df.target.values 
<find_best_model_class>
New_feature_train = imputed_X_train['Sex'] + "_" + imputed_X_train['Embarked']
New_feature_valid = imputed_X_valid['Sex'] + "_" + imputed_X_valid['Embarked']
New_feature_test = imputed_X_test['Sex'] + "_" + imputed_X_test['Embarked']
def CB_cv_scoring(X_train, y_train, X_test, nfolds=5):
predict = np.zeros(X_test.shape[0])
 oof = np.zeros(X_train.shape[0])
 folds = StratifiedKFold(n_splits=nfolds, shuffle=True, random_state=870)
 for fold_,(ind_trn, ind_valid)in enumerate(folds.split(X_train, y_train)) :
print("Fold {}".format(fold_))
clf = CatBoostRegressor(
 random_seed=42,
logging_level='Silent',
early_stopping_rounds=200, 
)
 categorical_features_indices = [0,1,2,3,4,5,6,7,9,11]
clf.fit(
 X_train.iloc[ind_trn], y_train.iloc[ind_trn],
cat_features=categorical_features_indices,
eval_set=(X_train.iloc[ind_valid], y_train.iloc[ind_valid]), 
);
oof[ind_valid] = clf.predict(X_train.iloc[ind_valid],)
 pred = clf.predict(X_test)/ nfolds
predict += pred
print("CV score: {:<8.5f}".format(rmse(y_train, oof)))
 return np.array(predict )<compute_test_metric>
imputed_X_train["Sex_Embarked"]=New_feature_train
imputed_X_valid["Sex_Embarked"]=New_feature_valid
imputed_X_test["Sex_Embarked"]=New_feature_test
lgb_pred = LGB_cv_scoring(X, y, X_test)
 submit_file(lgb_pred, 'lgb_pred' )<compute_test_metric>
Cat_cols=['Sex','Embarked','Sex_Embarked']
cat_pred = CB_cv_scoring(train_df.drop('target', axis=1), train_df.target, test_df)
 submit_file(cat_pred, 'cat_pred' )<train_model>
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
 OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(imputed_X_train[Cat_cols]))
OH_cols_valid = pd.DataFrame(OH_encoder.transform(imputed_X_valid[Cat_cols]))
OH_cols_test = pd.DataFrame(OH_encoder.transform(imputed_X_test[Cat_cols]))
OH_cols_train.index = imputed_X_train.index
OH_cols_valid.index = imputed_X_valid.index
OH_cols_test.index = imputed_X_test.index
num_X_train = imputed_X_train.drop(Cat_cols, axis =1)
 num_X_valid = imputed_X_valid.drop(Cat_cols, axis =1)
 num_X_test = imputed_X_test.drop(Cat_cols, axis =1)
 OH_X_train = pd.concat([num_X_train, OH_cols_train], axis=1)
 OH_X_valid = pd.concat([num_X_valid, OH_cols_valid], axis=1)
 OH_X_test = pd.concat([num_X_test, OH_cols_test], axis=1 )
mix_pred =(lgb_pred + cat_pred)/ 2
submit_file(mix_pred, 'mix_pred' )<compute_test_metric>
OH_X_train = OH_X_train.apply(pd.to_numeric)
 OH_X_valid = OH_X_valid.apply(pd.to_numeric)
 OH_X_test = OH_X_test.apply(pd.to_numeric)
 OH_X_train=OH_X_train.rename(columns={0:"Sex1", 1:"Sex2"})
 OH_X_train=OH_X_train.rename(columns={2:"C", 3:"Q",4:"S"})
 OH_X_valid=OH_X_valid.rename(columns={0:"Sex1", 1:"Sex2"})
 OH_X_valid=OH_X_valid.rename(columns={2:"C", 3:"Q",4:"S"})
 OH_X_test=OH_X_test.rename(columns={0:"Sex1", 1:"Sex2"})
 OH_X_test=OH_X_test.rename(columns={2:"C", 3:"Q",4:"S"} )
hack_mix_pred = hacking_score(mix_pred, y)
 submit_file(hack_mix_pred, 'hack_mix_pred' )<import_modules>
my_model = AdaBoostClassifier(n_estimators=200, learning_rate=0.05)
 my_model.fit(OH_X_train, y_train)
 y_pred5 = my_model.predict(OH_X_valid)
 print("Accuracy:",metrics.accuracy_score(y_valid, y_pred5))
<load_from_csv><EOS> 
predictions2 = my_model.predict(OH_X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions2})
 output.to_csv('my_submission_02_06.csv', index=False)
 print("Your submission was successfully saved!" )