Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
predictions = pred_test*0.5 + pred_test2*0.5<save_to_csv>
data = pd.read_csv('.. /input/titanic/train.csv',index_col = "PassengerId")
 test = pd.read_csv('.. /input/titanic/test.csv',index_col = "PassengerId")
predictions = pd.DataFrame(predictions, columns =['target'])
 sub = pd.concat([test[['ID_code']], predictions[['target']]], axis = 1)
 sub.to_csv('submission.csv', index=False )<set_options>
indexs= test.index
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
X = data.iloc[:,1:]
y = data.iloc[:,0]
from fastai import *
from fastai.vision import *<define_variables>
X['Ticket'].mode
path = Path('.. /input/aerial-cactus-identification/')
<load_from_csv>
X =X.drop(columns =['Name'] )
train = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 test = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv' )<define_variables>
imputer_no = SimpleImputer(missing_values= np.nan ,strategy = 'mean')
 imputer_no.fit(X[['Pclass','Age','SibSp','Fare','Parch']])
 X[['Pclass','Age','SibSp','Fare','Parch']] = imputer_no.transform(X[['Pclass','Age','SibSp','Fare','Parch']])
np.random.seed(50)
<feature_engineering>
imputer_cat = SimpleImputer(missing_values= np.nan ,strategy = 'most_frequent')
 imputer_cat.fit(X[['Sex','Cabin','Embarked','Ticket']])
 X[['Sex','Cabin','Embarked','Ticket']]=imputer_cat.transform(X[['Sex','Cabin','Embarked','Ticket']] )
tfms = get_transforms(do_flip = True, )<define_variables>
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
data.show_batch(rows = 3,figsize=(7,8))<choose_model_class>
ct = ColumnTransformer(transformers= [('encoder',OneHotEncoder(handle_unknown='ignore'),[1,5,7,8])],remainder = 'passthrough')
 X= ct.fit_transform(X )
learn = cnn_learner(data , models.resnet50 , metrics = error_rate )<train_model>
train_X,test_X,train_y,test_y = train_test_split(X,y )
learn.fit_one_cycle(4)
<create_dataframe>
for i in range(10,300,10):
classifier = RandomForestClassifier(n_estimators= i,criterion='gini')
 classifier.fit(train_X, train_y)
 y_predict = classifier.predict(test_X)
 print('for {} estimators and {}'.format({i},{accuracy_score(y_true=test_y,y_pred=y_predict)}))
test_data = ImageList.from_df(test, path=path/'test', folder='test')
 data.add_test(test_data )<predict_on_test>
test =test.drop(columns =['Name'] )
preds, _ = learn.get_preds(ds_type=DatasetType.Test)
 test.has_cactus = preds.numpy() [:, 0]<save_to_csv>
imputer_no.fit(test[['Pclass','Age','SibSp','Fare','Parch']])
 test[['Pclass','Age','SibSp','Fare','Parch']] = imputer_no.transform(test[['Pclass','Age','SibSp','Fare','Parch']])
 imputer_cat.fit(test[['Sex','Cabin','Embarked','Ticket']])
 test[['Sex','Cabin','Embarked','Ticket']]=imputer_cat.transform(test[['Sex','Cabin','Embarked','Ticket']])
 test= ct.transform(test)
test.to_csv("submit.csv", index=False )<load_from_csv>
classifier = RandomForestClassifier(n_estimators= 150,criterion='gini')
 classifier.fit(X, y)
 y_predict = classifier.predict(test)
 pd.DataFrame(y_predict,index=indexs,columns=['Survived'] ).to_csv('output.csv' )
train_dir=".. /input/train/train"
test_dir=".. /input/test/test"
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv(".. /input/sample_submission.csv")
 data_folder = Path(".. /input")
<choose_model_class>
learn = cnn_learner(train_img, models.densenet161, metrics=[error_rate, accuracy])
<find_best_params>