Kaggle competition: Titanic - Machine Learning from Disaster
model_algorithm(dtree, X_train, y_train, X_test, y_test, 'DecisionTree', labels, None )<compute_test_metric>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
model_algorithm(rtree, X_train, y_train, X_test, y_test, 'RandomForest', labels, features )<save_to_csv>
label_encoder_sex = LabelEncoder()
clf=LogisticRegression(C=0.05, solver="lbfgs", max_iter=5000)
 clf.fit(X_train, y_train)
 pred = clf.predict_proba(X_test)[:, 1]
pd.DataFrame({"id": test_ids, "target": pred} ).to_csv("submission_lr.csv", index=False )<load_from_csv>
train_data.iloc[:,4] = label_encoder_sex.fit_transform(train_data.iloc[:,4])
p = '.. /input/cat-in-the-dat-ii/'
X = pd.concat([pd.read_csv(p+'train.csv' ).iloc[:,1:-1],
pd.read_csv(p+'test.csv' ).iloc[:,1:]] ).astype('str')
 y = pd.read_csv(p+'train.csv' ).target
sample = pd.read_csv(p+'sample_submission.csv')
 X = OneHotEncoder().fit_transform(X)
 train,test = X[:600000],X[600000:]
sample['target'] = LogisticRegression(max_iter=1e5,C=0.06 ).fit(train,y ).predict_proba(test)[:,1]
sample.to_csv('submission.csv',index=False )<load_from_zip>
test_data.iloc[:,3] = label_encoder_sex.fit_transform(test_data.iloc[:,3] )
!wget https://download.knime.org/analytics-platform/linux/knime_4.1.2.linux.gtk.x86_64.tar.gz
!tar xvzf knime_4.1.2.linux.gtk.x86_64.tar.gz
!rm knime_4.1.2.linux.gtk.x86_64.tar.gz
!unzip./knime_4.1.2/knime-workspace.zip -d./knime_4.1.2/knime-workspace/
!rm./knime_4.1.2/knime-workspace.zip
!cp -R /kaggle/input/knime-cat-publ./knime_4.1.2/knime-workspace/
!sed -i 's/Xmx2048m/Xmx10240m/g'./knime_4.1.2/knime.ini
!./knime_4.1.2/knime -application org.eclipse.equinox.p2.director -nosplash -consolelog -r 'http://update.knime.com/analytics-platform/4.1,http://update.knime.com/community-contributions/4.1,http://update.knime.com/community-contributions/trusted/4.1,http://update.knime.com/partner/4.1' -i 'org.knime.features.python2.feature.group,org.knime.features.ext.h2o.feature.group,org.knime.features.datageneration.feature.group' -d./knime_4.1.2/
!pip install knime
!pip install dfply<define_variables>
X_train = train_data[["PassengerId", "Sex", "SibSp", "Parch", "Pclass"]]
Y_train = train_data["Survived"]
knime.executable_path = "./knime_4.1.2/knime"
workspace = "./knime_4.1.2/knime-workspace"
workflow = "knime-cat-publ/cat_publ/cat_publ"<choose_model_class>
X_test = test_data[["PassengerId", "Sex", "SibSp", "Parch", "Pclass"]]
knime.Workflow(workflow_path=workflow,workspace_path=workspace )<concatenate>
my_imputer = SimpleImputer()
X_train1 = my_imputer.fit_transform(X_train)
 X_test1 = my_imputer.fit_transform(X_test )
with knime.Workflow(workflow_path=workflow,workspace_path=workspace)as wf:
wf.execute()<define_variables>
sc = StandardScaler()
X_train1 = sc.fit_transform(X_train1)
 X_test1 = sc.fit_transform(X_test1 )
<import_modules>
import keras
from keras.models import Sequential
from keras.layers import Dense
sample_submission = pd.read_csv(".. /input/cat-in-the-dat-ii/sample_submission.csv")
 test = pd.read_csv(".. /input/cat-in-the-dat-ii/test.csv")
 train = pd.read_csv(".. /input/cat-in-the-dat-ii/train.csv")
<feature_engineering>
model = Sequential()
train["nom_0by1"]=train.nom_0.str.cat(train['nom_1'])
 train["nom_0by2"]=train.nom_0.str.cat(train['nom_2'])
 train["nom_0by3"]=train.nom_0.str.cat(train['nom_3'])
 train["nom_0by4"]=train.nom_0.str.cat(train['nom_4'])
 test["nom_0by1"]=test.nom_0.str.cat(test['nom_1'])
 test["nom_0by2"]=test.nom_0.str.cat(test['nom_2'])
 test["nom_0by3"]=test.nom_0.str.cat(test['nom_3'])
 test["nom_0by4"]=test.nom_0.str.cat(test['nom_4'])
 train["nom_1by2"]=train.nom_1.str.cat(train['nom_2'])
 train["nom_1by3"]=train.nom_1.str.cat(train['nom_3'])
 train["nom_1by4"]=train.nom_1.str.cat(train['nom_4'])
 test["nom_1by2"]=test.nom_1.str.cat(test['nom_2'])
 test["nom_1by3"]=test.nom_1.str.cat(test['nom_3'])
 test["nom_1by4"]=test.nom_1.str.cat(test['nom_4'])
 train["nom_2by3"]=train.nom_2.str.cat(train['nom_3'])
 train["nom_2by4"]=train.nom_2.str.cat(train['nom_4'])
 test["nom_2by3"]=test.nom_2.str.cat(test['nom_3'])
 test["nom_2by4"]=test.nom_2.str.cat(test['nom_4'])
 train["nom_3by4"]=train.nom_3.str.cat(train['nom_4'])
 test["nom_3by4"]=test.nom_3.str.cat(test['nom_4'])
 train["bin_3by4"]=train.bin_3.str.cat(train['bin_4'])
 test["bin_3by4"]=test.bin_3.str.cat(test['bin_4'])
 train["bin_3bynom_0"]=train.bin_3.str.cat(train['nom_0'])
 train["bin_3bynom_1"]=train.bin_3.str.cat(train['nom_1'])
 train["bin_3bynom_2"]=train.bin_3.str.cat(train['nom_2'])
 train["bin_3bynom_3"]=train.bin_3.str.cat(train['nom_3'])
 train["bin_3bynom_4"]=train.bin_3.str.cat(train['nom_4'])
 test["bin_3bynom_0"]=test.bin_3.str.cat(test['nom_0'])
 test["bin_3bynom_1"]=test.bin_3.str.cat(test['nom_1'])
 test["bin_3bynom_2"]=test.bin_3.str.cat(test['nom_2'])
 test["bin_3bynom_3"]=test.bin_3.str.cat(test['nom_3'])
 test["bin_3bynom_4"]=test.bin_3.str.cat(test['nom_4'])
 train["bin_4bynom_0"]=train.bin_4.str.cat(train['nom_0'])
 train["bin_4bynom_1"]=train.bin_4.str.cat(train['nom_1'])
 train["bin_4bynom_2"]=train.bin_4.str.cat(train['nom_2'])
 train["bin_4bynom_3"]=train.bin_4.str.cat(train['nom_3'])
 train["bin_4bynom_4"]=train.bin_4.str.cat(train['nom_4'])
 test["bin_4bynom_0"]=test.bin_4.str.cat(test['nom_0'])
 test["bin_4bynom_1"]=test.bin_4.str.cat(test['nom_1'])
 test["bin_4bynom_2"]=test.bin_4.str.cat(test['nom_2'])
 test["bin_4bynom_3"]=test.bin_4.str.cat(test['nom_3'])
 test["bin_4bynom_4"]=test.bin_4.str.cat(test['nom_4'])
 train["nom_5bynom_8"]=train.nom_5.str.cat(train['nom_8'])
 test["nom_5bynom_8"]=test.nom_5.str.cat(test['nom_8'])
 train["nom_6bynom_9"]=train.nom_6.str.cat(train['nom_9'])
 test["nom_6bynom_9"]=test.nom_6.str.cat(test['nom_9'])
<data_type_conversions>
model.add(Dense(units = 4, activation = 'relu', input_dim = 5))
model.add(Dense(units = 3, activation = 'relu'))
model.add(Dense(units = 2, activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))
train['day_obj']=train.day.astype('str')
 test['day_obj']=test.day.astype('str')
 train['month_obj']=train.month.astype('str')
 test['month_obj']=test.month.astype('str')
 train['ord_0_obj']=train.ord_0
train['ord_1_obj']=train.ord_1
train['ord_2_obj']=train.ord_2
train['ord_3_obj']=train.ord_3
train['ord_4_obj']=train.ord_4
train['ord_5_obj']=train.ord_5
test['ord_0_obj']=test.ord_0
test['ord_1_obj']=test.ord_1
test['ord_2_obj']=test.ord_2
test['ord_3_obj']=test.ord_3
test['ord_4_obj']=test.ord_4
test['ord_5_obj']=test.ord_5
train['nom_0_obj']=train.nom_0
train['nom_1_obj']=train.nom_1
train['nom_2_obj']=train.nom_2
train['nom_3_obj']=train.nom_3
train['nom_4_obj']=train.nom_4
test['nom_0_obj']=test.nom_0
test['nom_1_obj']=test.nom_1
test['nom_2_obj']=test.nom_2
test['nom_3_obj']=test.nom_3
test['nom_4_obj']=test.nom_4
train['bin_0_obj']=train.bin_0
train['bin_1_obj']=train.bin_1
train['bin_2_obj']=train.bin_2
train['bin_3_obj']=train.bin_3
train['bin_4_obj']=train.bin_4
test['bin_0_obj']=test.bin_0
test['bin_1_obj']=test.bin_1
test['bin_2_obj']=test.bin_2
test['bin_3_obj']=test.bin_3
test['bin_4_obj']=test.bin_4<feature_engineering>
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'] )
train["daybymonth"]=train.month_obj.str.cat(train['day_obj'])
 test["daybymonth"]=test.month_obj.str.cat(test['day_obj'] )<define_variables>
model.fit(X_train1, Y_train, epochs = 100 )
cat_cols=['nom_5','nom_6','nom_7','nom_8','nom_9','day_obj','month_obj','ord_0_obj','ord_1_obj','ord_2_obj','ord_3_obj','ord_4_obj',
'ord_5_obj',
'nom_0_obj','nom_1_obj','nom_2_obj','nom_3_obj','nom_4_obj',
'bin_0_obj','bin_1_obj','bin_2_obj','bin_3_obj','bin_4_obj',
"nom_3by4","bin_4bynom_0"]
for c in cat_cols:
data_tmp=pd.DataFrame({c:train[c],'target':train.target})
 target_mean=data_tmp.groupby(c)['target'].mean()
test[c]=test[c].map(target_mean)
 tmp=np.repeat(np.nan, train.shape[0])
 kf=KFold(n_splits=4, shuffle=True, random_state=72)
 for idx_1,idx_2 in kf.split(train):
target_mean=data_tmp.iloc[idx_1].groupby(c)['target'].mean()
tmp[idx_2]=train[c].iloc[idx_2].map(target_mean)
 train[c]=tmp<feature_engineering>
prediction = model.predict(X_test1 )
train['nom_mean']=(train.nom_5+train.nom_6+train.nom_7)/3
test['nom_mean']=(test.nom_5+test.nom_6+test.nom_7)/3<define_variables>
pred = []
for i in prediction:
if i[0] < 0.5:
pred.append(0)
 else:
pred.append(1 )
cols_to_use = ['ord_1', 'ord_2', 'ord_3', 'ord_4','ord_5','nom_0','nom_1','nom_2','nom_3','nom_4','bin_3','bin_4']
num_cols_to_use = ['ord_0','bin_0','bin_1','bin_2','day','month','nom_5','nom_6','nom_7','nom_8','nom_9','day_obj','month_obj',
'ord_0_obj','ord_1_obj','ord_2_obj','ord_3_obj','ord_4_obj','ord_5_obj',
'nom_0_obj','nom_1_obj','nom_2_obj','nom_3_obj','nom_4_obj',
'bin_0_obj','bin_1_obj','bin_2_obj','bin_3_obj','bin_4_obj',
"nom_3by4","bin_4bynom_0"
]
cols_sum = cols_to_use + num_cols_to_use
X = train[cols_sum]
y = train.target
train_X,val_X,train_y,val_y = train_test_split(X,y,random_state = 0)
 X_val = test[cols_sum]
numerical_transformer = SimpleImputer(strategy='mean')
 categorical_transformer = Pipeline(steps=[ 
('imputer',SimpleImputer(strategy='constant')) , 
('onehot',OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, num_cols_to_use), 
('cat',categorical_transformer, cols_to_use)
 ])
 my_model = CatBoost({'loss_function':'Logloss','num_boost_round':2500,'learning_rate':0.03,'early_stopping_rounds': 10,'depth':2})
 my_pipeline = Pipeline(steps=[('preprocessor',preprocessor), 
('model',my_model)
 ])
 my_pipeline.fit(train_X,train_y)
 prediction = my_pipeline.predict(X_val )<compute_train_metric>
pred = np.array(pred )
val_predictions = my_pipeline.predict(val_X)
 print(roc_auc_score(val_y,val_predictions))<save_to_csv>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': pred})
 len(output )
ss=sample_submission
ss.target = prediction
ss.to_csv("submission.csv", index=False )<import_modules>
output.to_csv('my_submission_nn_1.csv', index = False)
 print("Your submission was successfully saved!" )