Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_train_metric>
!pip install -q -U keras-tuner
clear_output()
scores = cross_val_score(knn, Xadult, Yadult, cv=10)
 scores<train_model>
TRAIN_PATH = ".. /input/titanic/train.csv"
TEST_PATH = ".. /input/titanic/test.csv"
SAMPLE_SUBMISSION_PATH = ".. /input/titanic/gender_submission.csv"
SUBMISSION_PATH = "submission.csv"
ID = "PassengerId"
TARGET = "Survived"
TEST_SIZE = 0.2
RANDOM_SEED = 42
MAX_TRIAL = 3
EPOCHS = 5
VALIDATION_SPLIT = 0.15
knn.fit(Xadult, Yadult )<train_model>
train = pd.read_csv(TRAIN_PATH)
 test = pd.read_csv(TEST_PATH )
knn.fit(Xadult, Yadult )<categorify>
drop_elements = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp','Parch']
train = train.drop(drop_elements, axis = 1)
 test = test.drop(drop_elements, axis = 1)
 def checkNull_fillData(df):
for col in df.columns:
if len(df.loc[df[col].isnull() == True])!= 0:
if df[col].dtype == "float64" or df[col].dtype == "int64":
df.loc[df[col].isnull() == True,col] = df[col].mean()
else:
df.loc[df[col].isnull() == True,col] = df[col].mode() [0]
checkNull_fillData(train)
 checkNull_fillData(test)
 str_list = []
num_list = []
for colname, colvalue in train.iteritems() :
if type(colvalue[1])== str:
str_list.append(colname)
 else:
num_list.append(colname)
 train = pd.get_dummies(train, columns=str_list)
 test = pd.get_dummies(test, columns=str_list )
adult['Sex'] = adult['Sex'].transform(lambda x: 1 if x=='Male' else 0 if x==x else x )<predict_on_test>
y = train[TARGET]
X = train.drop([TARGET],axis=1)
 X_test = test
gc.collect()
predictions = knn.predict(testAdult[["Age","Education-Num","Capital Gain", "Capital Loss", "Hours per week"]] )<create_dataframe>
X_train,X_val,y_train,y_val=train_test_split(X,y,test_size=TEST_SIZE,random_state=RANDOM_SEED )
result = np.vstack(( testAdult.index.values, predictions)).T
x = ['Id','income']
resultado = pd.DataFrame(columns=x, data=result)
 resultado.set_index('Id', inplace=True )<save_to_csv>
def build_random_forest(hp):
model = ensemble.RandomForestClassifier(
 n_estimators=hp.Int('n_estimators', 10, 50, step=10),
max_depth=hp.Int('max_depth', 3, 10))
return model
tuner = kt.tuners.Sklearn(
 oracle=kt.oracles.BayesianOptimization(
 objective=kt.Objective('score', 'max'),
max_trials=10),
hypermodel= build_random_forest,
directory='.',
project_name='random_forest')
 tuner.search(X_train.values, y_train.values.ravel())
 best_hp = tuner.get_best_hyperparameters(num_trials=1)[0]
resultado.to_csv('mypredictions.csv' )<install_modules>
model = tuner.hypermodel.build(best_hp)
 model.fit(X_train, y_train.values )
!pip show fastai<set_options>
pred_val = model.predict(X_val)
 print(accuracy_score(y_val, pred_val))
%matplotlib inline 
<define_variables>
pred_test = model.predict(X_test )
<compute_test_metric><EOS> 
sub = pd.read_csv(SAMPLE_SUBMISSION_PATH)
 sub[TARGET]=(pred_test > 0.5 ).astype(int)
 sub.to_csv(SUBMISSION_PATH,index=False)
 sub.head()