Kaggle competition: Titanic - Machine Learning from Disaster
warnings.filterwarnings('ignore' )<load_from_csv>
df_trainval=pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test=pd.read_csv("/kaggle/input/titanic/test.csv" )
flight = pd.read_csv(".. /input/datavidia2019/flight.csv")
 test = pd.read_csv(".. /input/datavidia2019/test.csv")
 hotel = pd.read_csv(".. /input/datavidia2019/hotel.csv" )<feature_engineering>
df_trainval_C=df_trainval.copy()
def preprocess_city_trx(df):
df["v_city_proc"] = df.visited_city.map(lambda x: ast.literal_eval(x.strip("''")))
 df["log_trx_proc"] = df.log_transaction.map(lambda x: ast.literal_eval(x.strip("''")))
 return df
preprocess_city_trx(test)
 preprocess_city_trx(flight ).head()<create_dataframe>
print("% Of Males Survived:",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)).astype(int)).sum())/(df_trainval["Sex"]=="male" ).astype(int ).sum())*100,"%")
 print("% Of Females Survived:",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)).astype(int)).sum())/(df_trainval["Sex"]=="female" ).astype(int ).sum())*100,"%")
 fig, ax = plt.subplots(figsize=(8, 5))
sns.countplot(df_trainval['Survived'],hue=df_trainval["Sex"])
 plt.title('Survived labels values')
 plt.show()
missing_data = pd.DataFrame({'total_missing': flight.isnull().sum() , 'perc_missing':(flight.isnull().sum() /len(flight.index)) *100})
 missing_data<create_dataframe>
print("% Of Males Survived from class 1:",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)&(df_trainval["Pclass"]==1)).astype(int)).sum())/(((df_trainval["Sex"]=="male")&(df_trainval["Pclass"]==1)).astype(int)).sum())*100,"%")
 print("% Of Males Survived from class 2:",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)&(df_trainval["Pclass"]==2)).astype(int)).sum())/(((df_trainval["Sex"]=="male")&(df_trainval["Pclass"]==2)).astype(int)).sum())*100,"%")
 print("% Of Males Survived from class 3:",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)&(df_trainval["Pclass"]==3)).astype(int)).sum())/(((df_trainval["Sex"]=="male")&(df_trainval["Pclass"]==3)).astype(int)).sum())*100,"%")
 print("% Of Females Survived from class 1:",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)&(df_trainval["Pclass"]==1)).astype(int)).sum())/(((df_trainval["Sex"]=="female")&(df_trainval["Pclass"]==1)).astype(int)).sum())*100,"%")
 print("% Of Females Survived from class 2:",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)&(df_trainval["Pclass"]==2)).astype(int)).sum())/(((df_trainval["Sex"]=="female")&(df_trainval["Pclass"]==2)).astype(int)).sum())*100,"%")
 print("% Of Females Survived from class 3:",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)&(df_trainval["Pclass"]==3)).astype(int)).sum())/(((df_trainval["Sex"]=="female")&(df_trainval["Pclass"]==3)).astype(int)).sum())*100,"%")
 sns.catplot(x='Survived',hue="Pclass",col='Sex',kind="count",data=df_trainval)
 plt.show()
missing_data2 = pd.DataFrame({'total_missing': test.isnull().sum() , 'perc_missing':(test.isnull().sum() /len(test.index)) *100})
 missing_data2<feature_engineering>
df_trainval["Parch"].value_counts()
def get_is_cross_sell(df):
df["is_cross_sell"] = df.hotel_id.map(lambda x: 0 if x == "None" else 1)
 return df
get_is_cross_sell(flight ).is_cross_sell.value_counts()<count_values>
print("% of people survived with having 0 Parch: ",(((((df_trainval["Parch"]==0)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==0 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 1 Parch: ",(((((df_trainval["Parch"]==1)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==1 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 2 Parch: ",(((((df_trainval["Parch"]==2)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==2 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 3 Parch: ",(((((df_trainval["Parch"]==3)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==3 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 4 Parch: ",(((((df_trainval["Parch"]==4)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==4 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 5 Parch: ",(((((df_trainval["Parch"]==5)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==5 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 6 Parch: ",(((((df_trainval["Parch"]==6)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["Parch"]==6 ).astype(int ).sum())) *100," %")
 sns.catplot(x='Survived',hue="Parch",kind="count",data=df_trainval )
flight["route"].value_counts()<count_unique_values>
df_trainval["SibSp"].value_counts()
flight['order_id'].nunique() , flight.shape[0]<count_values>
print("% of people survived with having 0 Siblings: ",(((((df_trainval["SibSp"]==0)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["SibSp"]==0 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 1 Siblings: ",(((((df_trainval["SibSp"]==1)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["SibSp"]==1 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 2 Siblings: ",(((((df_trainval["SibSp"]==2)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["SibSp"]==2 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 3 Siblings: ",(((((df_trainval["SibSp"]==3)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["SibSp"]==3 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 4 Siblings: ",(((((df_trainval["SibSp"]==4)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["SibSp"]==4 ).astype(int ).sum())) *100," %")
 print("% of people survived with having 5 Siblings: ",(((((df_trainval["SibSp"]==5)&(df_trainval["Survived"]==1)).astype(int ).sum())/(df_trainval["SibSp"]==5 ).astype(int ).sum())) *100," %")
 sns.catplot(x='Survived',hue="SibSp",kind="count",data=df_trainval )
flight.visited_city.value_counts()<feature_engineering>
print("% Of male Survived with boarding at Southampton :",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)&(df_trainval["Embarked"]=="S")).astype(int)).sum())/(((df_trainval["Sex"]=="male")&(df_trainval["Embarked"]=="S")).astype(int)).sum())*100,"%")
 print("% Of female Survived with boarding at Southampton :",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)&(df_trainval["Embarked"]=="S")).astype(int)).sum())/(((df_trainval["Sex"]=="female")&(df_trainval["Embarked"]=="S")).astype(int)).sum())*100,"%")
 print("% Of male Survived with boarding at Cherbourg :",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)&(df_trainval["Embarked"]=="C")).astype(int)).sum())/(((df_trainval["Sex"]=="male")&(df_trainval["Embarked"]=="C")).astype(int)).sum())*100,"%")
 print("% Of female Survived with boarding at Cherbourg :",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)&(df_trainval["Embarked"]=="C")).astype(int)).sum())/(((df_trainval["Sex"]=="female")&(df_trainval["Embarked"]=="C")).astype(int)).sum())*100,"%")
 print("% Of female Survived with boarding at Queenstown :",(((((df_trainval["Sex"]=="male")&(df_trainval["Survived"]==1)&(df_trainval["Embarked"]=="Q")).astype(int)).sum())/(((df_trainval["Sex"]=="male")&(df_trainval["Embarked"]=="Q")).astype(int)).sum())*100,"%")
 print("% Of female Survived with boarding at Queenstown :",(((((df_trainval["Sex"]=="female")&(df_trainval["Survived"]==1)&(df_trainval["Embarked"]=="Q")).astype(int)).sum())/(((df_trainval["Sex"]=="female")&(df_trainval["Embarked"]=="Q")).astype(int)).sum())*100,"%")
 sns.catplot(x='Survived',col="Embarked",hue="Sex",kind="count",data=df_trainval)
 plt.show()
y = flight.copy()
y["n_trx"] = y.log_trx_proc.map(len)
 y[y.n_trx == 2][y.trip == "trip"].visited_city.value_counts()<define_variables>
df_trainval["Sex"].value_counts()
num_columns = ["sum_transaction", "num_transaction", "price", "member_duration_days", "no_of_seats"]
cat_columns = ["trip", "is_tx_promo", "airlines_name", "visited_city", "account_id", "service_class", "gender"]<feature_engineering>
df_trainval["Sex_labels"],Sex_uniques=pd.factorize(df_trainval["Sex"])
 df_trainval.head()
num_columns = ["avg_transaction", "price", "no_of_seats", "member_duration_days"]
cat_columns = ["trip", "is_tx_promo", "airlines_name", "visited_city", "account_id", "service_class", "gender", "num_transaction", "member_registered_quarter"]
def process_data2(df):
f = df.copy()
f["num_transaction"] = f.log_trx_proc.map(len)
 f["avg_transaction"] = f.log_trx_proc.map(sum)/ f["num_transaction"]
f["num_transaction"] = f["num_transaction"].astype(np.int32 ).map(lambda x: x if x <= 14 else 15)
 f.avg_transaction = f.avg_transaction / 10**4
f.price = f.price / 10**4
f["member_registered_quarter"] =(( f.member_duration_days - 329)// 90 ).astype("int32")
 f = f.drop(columns=["order_id", "hotel_id", "route", "log_transaction", "v_city_proc", "log_trx_proc", "is_cross_sell"])
 return f<data_type_conversions>
df_trainval["Pclass"].value_counts()
def cast_columns(f):
for c in num_columns:
f[c] = f[c].astype(np.int32)
 for c in cat_columns:
f[c] = f[c].astype("category")
 return f<prepare_x_and_y>
Age_mean=df_trainval["Age"].mean()
df_trainval["Age"].fillna(Age_mean,inplace=True )
ty = flight["hotel_id"].map(lambda x: 0 if x == "None" else 1 )<concatenate>
a,age_bins=pd.qcut(df_trainval["Age"],7,retbins=True)
 df_trainval["Age_label"],Age_unique=pd.factorize(a)
 df_trainval.head()
train_split = flight.shape[0]
full_df = pd.concat(( flight, test), sort=True)
 p = cast_columns(process_data2(full_df))
tx, tex = p.iloc[:train_split], p.iloc[train_split:]
tx.shape, ty.shape, tex.shape<define_variables>
df_trainval["Age_label"].value_counts()
categ_feat_idx = np.where(tx.dtypes == "category")[0]<split>
age_cat=[]
for i in range(7):
i1=age_bins[i]
i2=age_bins[i+1]
age_cat.append(str(i)+" : "+i1.astype(str)+"-"+i2.astype(str))
train_X, valid_X, train_y, valid_y = train_test_split(tx, ty, test_size=0.25, stratify=ty, random_state=17 )<choose_model_class>
df_trainval["Parch"].value_counts()
parameters = {
"random_seed": 17,
"eval_metric": "F1",
"custom_metric": ["F1", "Precision", "Recall"],
"loss_function": "Logloss",
"task_type": "GPU",
"random_strength": 3,
"od_type": "Iter",
"verbose":100,
"cat_features":categ_feat_idx
}
ctb = CatBoostClassifier(**parameters )<train_model>
df_trainval["(Sex_labels+1)/Parch"]=(df_trainval["Sex_labels"]+1)/df_trainval["Pclass"]
df_trainval["Parch+SibSp+1"]=df_trainval["SibSp"]+df_trainval["Parch"]+1
df_trainval["Pclass/Parch"]=df_trainval["Parch"]/df_trainval["Pclass"]
df_trainval["Parch+SibSp+sex_label"]=df_trainval["SibSp"]+df_trainval["Parch"]+df_trainval["Sex_labels"]
df_trainval.head()
ctb.fit(train_X, train_y )<compute_test_metric>
df_trainval["Embarked"].value_counts()
get_metrics(valid_X,valid_y, e=ctb )<feature_engineering>
df_trainval["Embarked_label"],Embarked_unique=pd.factorize(df_trainval["Embarked"])
 df_trainval.head()
ctb.get_feature_importance(prettified=True )<install_modules>
b,Fare_bins=pd.qcut(df_trainval["Fare"],5,retbins=True)
 df_trainval["Fare_labels"],Fare_unique=pd.factorize(b)
 df_trainval.head()
!pip uninstall scikit-optimize --yes
!pip install git+https://github.com/darenr/scikit-optimize<import_modules>
df_trainval["Fare_labels"].value_counts()
from scipy.stats import randint
from scipy.stats import uniform
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.model_selection import cross_val_score
from sklearn.metrics import average_precision_score, roc_auc_score, mean_absolute_error, f1_score, make_scorer
from skopt import BayesSearchCV
from skopt import gp_minimize
from skopt.space import Real, Categorical, Integer
from skopt.utils import use_named_args
from skopt.callbacks import DeadlineStopper
from skopt.callbacks import VerboseCallback
from skopt.callbacks import DeltaXStopper<train_on_grid>
df_trainval["Fare_labels*(Sex_labels+1)"]=df_trainval["Fare_labels"]*(df_trainval["Sex_labels"]+1 )
def report_perf(optimizer, X, y, title, callbacks=None):
start = time()
if callbacks:
optimizer.fit(X, y, callback=callbacks)
 else:
optimizer.fit(X, y)
 best_score = optimizer.best_score_
best_score_std = optimizer.cv_results_['std_test_score'][optimizer.best_index_]
best_params = optimizer.best_params_
print(( title + " took %.2f seconds, candidates checked: %d, best CV score: %.3f "
+u"\u00B1"+" %.3f")%(time() - start,
len(optimizer.cv_results_['params']),
best_score,
best_score_std))
print('Best parameters:')
 pprint.pprint(best_params)
 print()
return best_params<choose_model_class>
X_trainval=df_trainval.drop(columns=["Survived","Sex","Fare","Embarked","Cabin","Name","Ticket","PassengerId","Age"])
 Y_trainval=df_trainval["Survived"]
print("X_trainval shape: ",X_trainval.shape)
 print("Y_trainval shape: ",Y_trainval.shape )
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=8)
 scorer = make_scorer(f1_score, average="macro")
 search_spaces = {'iterations': Integer(100, 1000),
'depth': Integer(4, 8),
'random_strength': Real(1e-9, 10, 'log-uniform'),
'bagging_temperature': Real(0.0, 1.0),
'border_count': Integer(128, 255),
'l2_leaf_reg': Integer(2, 30),
'scale_pos_weight':Real(1.0, 5.0, 'uniform')}
opt = BayesSearchCV(ctb,
search_spaces,
scoring=scorer,
cv=skf,
n_jobs=1,
return_train_score=False,
refit=True,
optimizer_kwargs={'base_estimator': 'GP'},
fit_params={'cat_features': categ_feat_idx, 'silent': True},
random_state=22,
verbose=True )<compute_test_metric>
X_train1,X_val1,Y_train1,Y_val1=train_test_split(X_trainval,Y_trainval,test_size=0.2,random_state=42)
 print("X_train shape: ",X_train1.shape)
 print("X_val shape: ",X_val1.shape)
 print("Y_train shape: ",Y_train1.shape)
 print("Y_val shape: ",Y_val1.shape )
best_params = report_perf(opt, tx, ty,'CatBoost',
callbacks=[VerboseCallback(100),
DeltaXStopper(0.0001),
DeadlineStopper(60*5)] )<init_hyperparams>
model1=LogisticRegression()
model1.fit(X_train1,Y_train1)
 print("train set Accuracy: ",model1.score(X_train1,Y_train1))
print("test set Accuracy: ",model1.score(X_val1,Y_val1))
best_params = {'bagging_temperature': 0.36426463210895726,
'border_count': 206,
'depth': 8,
'iterations': 342,
'l2_leaf_reg': 5,
'random_strength': 3.3149120967201682e-06,
'scale_pos_weight': 4.210304871687331}<choose_model_class>
Y_val1_predict= model1.predict(X_val1)
 print(confusion_matrix(Y_val1,Y_val1_predict))
tuned_model = CatBoostClassifier(**best_params, cat_features=categ_feat_idx, verbose=100, od_type="Iter" )<count_values>
scaler=StandardScaler()
standard_scaled_features=scaler.fit_transform(X_trainval)
 X_trainval_2=pd.DataFrame(standard_scaled_features,index=X_trainval.index,columns=X_trainval.columns)
 X_trainval_2.head()
train_y.value_counts() [0] // train_y.value_counts() [1]<choose_model_class>
X_train2,X_val2,Y_train2,Y_val2=train_test_split(X_trainval_2,Y_trainval,test_size=0.2,random_state=42)
 print("X_train shape: ",X_train2.shape)
 print("X_val shape: ",X_val2.shape)
 print("Y_train shape: ",Y_train2.shape)
 print("Y_val shape: ",Y_val2.shape )
ctb2 = CatBoostClassifier(**{"random_seed": 40,
"eval_metric": "F1",
"custom_metric": ["F1", "Precision", "Recall"],
"loss_function": "Logloss",
"task_type": "GPU",
"random_strength": 3,
"od_type": "Iter",
"scale_pos_weight": 12,
"verbose":100,
"cat_features":categ_feat_idx} )<count_duplicates>
model2=LogisticRegression()
model2.fit(X_train2,Y_train2)
 print("train set Accuracy: ",model2.score(X_train2,Y_train2))
print("test set Accuracy: ",model1.score(X_val2,Y_val2))
flight2 = flight.copy()
d = flight.duplicated(subset=("account_id", "airlines_name", "trip", "is_tx_promo", "price"))
flight2 = flight[~d]<categorify>
Y_val2_predict= model2.predict(X_val2)
 print(confusion_matrix(Y_val2,Y_val2_predict))
ty2 = flight2.hotel_id.map(lambda x: 0 if x == "None" else 1)
 tx2 = cast_columns(process_data2(flight2)) [tx.columns]<define_variables>
scaler=MinMaxScaler()
minmax_scaled_features=scaler.fit_transform(X_trainval)
 X_trainval_3=pd.DataFrame(minmax_scaled_features,index=X_trainval.index,columns=X_trainval.columns)
 X_trainval_3.head()
categ_feat_idx2 = np.where(tx2.dtypes == "category")[0]<choose_model_class>
X_train3,X_val3,Y_train3,Y_val3=train_test_split(X_trainval_3,Y_trainval,test_size=0.2,random_state=42)
 print("X_train shape: ",X_train3.shape)
 print("X_val shape: ",X_val3.shape)
 print("Y_train shape: ",Y_train3.shape)
 print("Y_val shape: ",Y_val3.shape )
ctb4 = CatBoostClassifier(**{"random_seed": 40,
"eval_metric": "F1",
"custom_metric": ["F1", "Precision", "Recall"],
"loss_function": "Logloss",
"task_type": "GPU",
"random_strength": 3,
"od_type": "Iter",
"scale_pos_weight": 3.5,
"verbose":100,
"cat_features":categ_feat_idx2} )<split>
model3=LogisticRegression()
model3.fit(X_train3,Y_train3)
 print("train set Accuracy: ",model3.score(X_train3,Y_train3))
print("test set Accuracy: ",model3.score(X_val3,Y_val3))
train_X2, valid_X2, train_y2, valid_y2 = train_test_split(tx2, ty2, test_size=0.25, stratify=ty2, random_state=17 )<import_modules>
Y_val3_predict= model3.predict(X_val3)
 print(confusion_matrix(Y_val3,Y_val3_predict))
<train_model>
model4=LinearSVC()
model4.fit(X_train1,Y_train1)
 print("train set Accuracy: ",model4.score(X_train1,Y_train1))
print("test set Accuracy: ",model4.score(X_val1,Y_val1))
tuned_model.fit(tx, ty )<train_model>
Y_val4_predict= model4.predict(X_val1)
 print(confusion_matrix(Y_val1,Y_val4_predict))
ctb.fit(tx, ty)
 ctb2.fit(tx, ty)
 ctb4.fit(tx2, ty2 )<compute_test_metric>
model5=LinearSVC()
model5.fit(X_train2,Y_train2)
 print("train set Accuracy: ",model5.score(X_train2,Y_train2))
print("test set Accuracy: ",model5.score(X_val2,Y_val2))
get_metrics(valid_X,valid_y, e=vc )<train_model>
Y_val5_predict= model5.predict(X_val2)
 print(confusion_matrix(Y_val2,Y_val5_predict))
eclf = EnsembleVoteClassifier(clfs=[ctb2, tuned_model], weights=[1,1.1], refit=False, voting="soft")
 eclf.fit(tx, ty )<count_values>
model6=LinearSVC()
model6.fit(X_train3,Y_train3)
 print("train set Accuracy: ",model6.score(X_train3,Y_train3))
print("test set Accuracy: ",model6.score(X_val3,Y_val3))
f_pred = pd.Series(eclf.predict(tex.to_numpy())).map(lambda x: "no" if x == 0 else "yes")
 f_pred.value_counts()<prepare_output>
model7=DecisionTreeClassifier(max_depth=9,min_samples_split=8,min_samples_leaf=2)
 model7.fit(X_train1,Y_train1)
 print("train set Accuracy: ",model7.score(X_train1,Y_train1))
print("test set Accuracy: ",model7.score(X_val1,Y_val1))
submission = test[["order_id"]]
submission["is_cross_sell"] = f_pred
submission<save_to_csv>
Y_val7_predict= model7.predict(X_val1)
 print(confusion_matrix(Y_val1,Y_val7_predict))
submission.to_csv('submission.csv', header=True, index=False )<set_options>
model8=DecisionTreeClassifier(max_depth=9,min_samples_split=8,min_samples_leaf=2)
 model8.fit(X_train2,Y_train2)
 print("train set Accuracy: ",model8.score(X_train2,Y_train2))
print("test set Accuracy: ",model8.score(X_val2,Y_val2))
%matplotlib inline 
<load_from_csv>
Y_val8_predict= model8.predict(X_val2)
 print(confusion_matrix(Y_val2,Y_val8_predict))
train_csv = pd.read_csv(".. /input/train_set.csv", low_memory=False)
 test_csv = pd.read_csv(".. /input/test_set.csv", low_memory=False )<drop_column>
model9=DecisionTreeClassifier(max_depth=9,min_samples_split=8,min_samples_leaf=2)
 model9.fit(X_train3,Y_train3)
 print("train set Accuracy: ",model9.score(X_train3,Y_train3))
print("test set Accuracy: ",model9.score(X_val3,Y_val3))
X_trainvalid = train_csv.drop(["PRICE", "Id"], axis=1)
 X_test = test_csv.drop(["Id"], axis=1 )<data_type_conversions>
Y_val9_predict= model9.predict(X_val3)
 print(confusion_matrix(Y_val3,Y_val9_predict))
X_traintest = pd.concat([X_trainvalid, X_test], axis=0)
 X_traintest["SALEDATE"] = pd.to_datetime(X_traintest["SALEDATE"] ).astype("int")
 X_traintest["GIS_LAST_MOD_DTTM"] = pd.to_datetime(X_traintest["GIS_LAST_MOD_DTTM"] ).astype("int" )<data_type_conversions>
model10=RandomForestClassifier(n_estimators=100,max_leaf_nodes=50)
 model10.fit(X_train1,Y_train1)
 print("train set Accuracy: ",model10.score(X_train1,Y_train1))
print("test set Accuracy: ",model10.score(X_val1,Y_val1))
for c in categorical_features:
X_traintest[c] = X_traintest[c].astype('category' )<init_hyperparams>
model11=RandomForestClassifier(n_estimators=100,max_leaf_nodes=50)
 model11.fit(X_train2,Y_train2)
 print("train set Accuracy: ",model11.score(X_train2,Y_train2))
print("test set Accuracy: ",model11.score(X_val2,Y_val2))
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'l2',
'num_leaves': 511,
'learning_rate': 0.01,
'feature_fraction': 0.8,
'verbose': 0
}<train_model>
model12=RandomForestClassifier(n_estimators=100,max_leaf_nodes=50)
 model12.fit(X_train3,Y_train3)
 print("train set Accuracy: ",model12.score(X_train3,Y_train3))
print("test set Accuracy: ",model12.score(X_val3,Y_val3))
X_train = X_traintest.iloc[:train_csv.shape[0], :]
X_test = X_traintest.iloc[train_csv.shape[0]:, :]
y_train = np.log(train_csv["PRICE"].values)
 X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=71)
 lgb_train = lgb.Dataset(X_train, y_train, categorical_feature=categorical_features)
 lgb_valid = lgb.Dataset(X_valid, y_valid, categorical_feature=categorical_features)
 print('Starting training...')
 model = lgb.train(
 params,
lgb_train,
valid_sets=lgb_valid,
num_boost_round=10000,
early_stopping_rounds=100 
)
<predict_on_test>
K.clear_session()
tf.reset_default_graph()
test_pred = np.exp(model.predict(X_test))<save_to_csv>
model13 = Sequential()
model13.add(Dense(8,input_shape=(12,)))
 model13.add(Activation('sigmoid'))
model13.add(Dense(16))
model13.add(Activation('sigmoid'))
model13.add(Dense(32))
model13.add(Activation('sigmoid'))
model13.add(Dense(2))
model13.add(Activation('sigmoid'))
model13.add(Dense(1))
model13.add(Activation('sigmoid'))
sub_df = pd.DataFrame({"Id":test_csv["Id"].values,"PRICE":test_pred})
 sub_df.to_csv("sub_harada.csv", index=False )<import_modules>
model13.compile(
 loss='mean_squared_error',
optimizer='adagrad',
metrics=['accuracy']
)
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split<import_modules>
model13.fit(
 X_train3,
Y_train3,
batch_size=32,
epochs=100,
validation_split=0.2
)
import keras
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization<load_from_csv>
print("test set Accuracy: ",model13.evaluate(x=X_val3, y=Y_val3, batch_size=2, verbose=1))
data_train = pd.read_csv('.. /input/train42000.csv')
 data_test = pd.read_csv('.. /input/test28000.csv' )<prepare_x_and_y>
g1=df_test["PassengerId"]
df_test["Sex_labels"],Sex_test_uniques=pd.factorize(df_test["Sex"])
 df_test.drop(columns=["Sex"],inplace=True)
 df_test["Age"].fillna(Age_mean,inplace=True)
 a1=pd.qcut(df_test["Age"],6)
 df_test["Age_label"],Age_test_unique=pd.factorize(a1)
 df_test.drop(columns=["Age"],inplace=True)
 df_test.drop(columns=["Cabin","Name","Ticket","PassengerId"],inplace=True)
 df_test["(Sex_labels+1)/Parch"]=(df_test["Sex_labels"]+1)/df_test["Pclass"]
df_test["Parch+SibSp+1"]=df_test["SibSp"]+df_test["Parch"]+1
df_test["Pclass/Parch"]=df_test["Pclass"]/df_test["Parch"]
df_test["Parch+SibSp+sex_label"]=df_test["SibSp"]+df_test["Parch"]+df_test["Sex_labels"]
df_test["Embarked_label"],Embarked_test_unique=pd.factorize(df_test["Embarked"])
 df_test.drop(columns=["Embarked"],inplace=True)
 a2=pd.qcut(df_test["Fare"],5)
 df_test["Fare_labels"],Fare_uniques=pd.factorize(a2)
 df_test.drop(columns=["Fare"],inplace=True)
 df_test["Fare_labels*(Sex_labels+1)"]=df_test["Fare_labels"]*(df_test["Sex_labels"]+1)
 df_test["Parch+SibSp+sex_label"]=df_test["SibSp"]+df_test["Parch"]+df_test["Sex_labels"]
X = np.array(data_train.iloc[:, 1:] )<prepare_x_and_y>
result_1=model1.predict(df_test)
 Result_logistic_Regression=pd.DataFrame({'PassengerId':g1,'Survived':result_1})
 Result_logistic_Regression.to_csv("Result_logistic_Regression_1.csv",index=False,header=True )
y_tmp = np.array(data_train.iloc[:, 0])
 y_tmp<categorify>
scaler=StandardScaler()
df_test_2=scaler.fit_transform(df_test )
y = to_categorical(y_tmp)
 y<split>
result_2=model2.predict(df_test_2)
 Result_logistic_Regression_2=pd.DataFrame({'PassengerId':g1,'Survived':result_2})
 Result_logistic_Regression_2.to_csv("Result_logistic_Regression_2.csv",index=False,header=True )
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=13 )<prepare_x_and_y>
scaler=MinMaxScaler()
df_test_3=scaler.fit_transform(df_test )
X_test = np.array(data_test.iloc[:, 0:] )<train_model>
result_3=model3.predict(df_test_3)
 Result_logistic_Regression_3=pd.DataFrame({'PassengerId':g1,'Survived':result_3})
 Result_logistic_Regression_3.to_csv("Result_logistic_Regression_3.csv",index=False,header=True )
img_rows, img_cols = 28, 28
input_shape =(img_rows, img_cols, 1)
 X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
 X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
 X_val = X_val.reshape(X_val.shape[0], img_rows, img_cols, 1 )<data_type_conversions>
result_4=model4.predict(df_test)
 Result_logistic_Regression_4=pd.DataFrame({'PassengerId':g1,'Survived':result_4})
 Result_logistic_Regression_4.to_csv("Result_SVM_1.csv",index=False,header=True )
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_val = X_val.astype('float32' )<feature_engineering>
result_5=model5.predict(df_test_2)
 Result_logistic_Regression_5=pd.DataFrame({'PassengerId':g1,'Survived':result_5})
 Result_logistic_Regression_5.to_csv("Result_SVM_2.csv",index=False,header=True )
X_train /= 255
X_test /= 255
X_val /= 255<choose_model_class>
result_6=model6.predict(df_test_3)
 Result_logistic_Regression_6=pd.DataFrame({'PassengerId':g1,'Survived':result_6})
 Result_logistic_Regression_6.to_csv("Result_SVM_3.csv",index=False,header=True )
num_classes = 10
model = Sequential()
model.add(Conv2D(20, kernel_size=(5, 5),
activation='relu',
kernel_initializer='he_normal',
input_shape=input_shape))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Flatten())
 model.add(Dense(20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))<choose_model_class>
result_7=model7.predict(df_test)
 Result_logistic_Regression_7=pd.DataFrame({'PassengerId':g1,'Survived':result_7})
 Result_logistic_Regression_7.to_csv("Result_Decision_tree_1.csv",index=False,header=True )
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam() ,
metrics=['accuracy'] )<define_variables>
result_8=model8.predict(df_test_2)
 Result_logistic_Regression_8=pd.DataFrame({'PassengerId':g1,'Survived':result_8})
 Result_logistic_Regression_8.to_csv("Result_Decision_tree_2.csv",index=False,header=True )
batch_size = 256
epochs = 5<train_model>
result_9=model9.predict(df_test_3)
 Result_logistic_Regression_9=pd.DataFrame({'PassengerId':g1,'Survived':result_9})
 Result_logistic_Regression_9.to_csv("Result_Decision_tree_3.csv",index=False,header=True )
history = model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_val, y_val))<predict_on_test>
result_10=model10.predict(df_test)
 Result_logistic_Regression_10=pd.DataFrame({'PassengerId':g1,'Survived':result_10})
 Result_logistic_Regression_10.to_csv("Result_Random_Forest_1.csv",index=False,header=True )
predicted_classes = model.predict_classes(X_test )<create_dataframe>
result_11=model11.predict(df_test_2)
 Result_logistic_Regression_11=pd.DataFrame({'PassengerId':g1,'Survived':result_11})
 Result_logistic_Regression_11.to_csv("Result_Random_Forest_2.csv",index=False,header=True )
df = pd.DataFrame(predicted_classes,columns=['label'] )<save_to_csv>
result_12=model12.predict(df_test_3)
 Result_logistic_Regression_12=pd.DataFrame({'PassengerId':g1,'Survived':result_12})
 Result_logistic_Regression_12.to_csv("Result_Random_Forest_3.csv",index=False,header=True )
df.to_csv(path_or_buf="Submission.csv",index_label="image_id" )<import_modules>
result_12=model12.predict(df_test_3)
 Result_logistic_Regression_12=pd.DataFrame({'PassengerId':g1,'Survived':result_12})
 Result_logistic_Regression_12.to_csv("Result_Random_Forest_3.csv",index=False,header=True )
<normalization><EOS> 
result_13=(model13.predict(df_test_3)>0.5 ).astype(int)
 result_13=result_13.squeeze()
Result_logistic_Regression_13=pd.DataFrame({'PassengerId':g1,'Survived':result_13})
 Result_logistic_Regression_13.to_csv("Neural_Network_3.csv",index=False,header=True )