Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
plt.style.use('ggplot')
 warnings.simplefilter(action='ignore', category=FutureWarning )
train = pd.read_csv(path/'.. /input/aptos2019-blindness-detection/train.csv')[:100]
test = pd.read_csv(path/'.. /input/aptos2019-blindness-detection/sample_submission.csv')
<data_type_conversions>
dataset = pd.read_csv('.. /input/titanic/train.csv')
 dataset.head()
test2 = test.copy()
test2['id_code'] = np.arange(0,test.shape[0],dtype=np.int ).astype('str' )<feature_engineering>
dataset.isnull().sum(axis=0 )
tfms = get_transforms(flip_vert=True,max_rotate = 10,max_warp = 0,max_zoom =1.05,max_lighting = 0 )<concatenate>
dataset = dataset.fillna({"Embarked": "S"} )
tfms[1].append(tfms[0][1])
 tfms[1].append(tfms[0][2])
<set_options>
dataset = pd.get_dummies(dataset, columns=['Sex'])
 dataset.head()
gc.collect() 
<define_variables>
dataset = pd.get_dummies(dataset, columns=['Embarked'])
 dataset.head()
image_dic = {}<load_pretrained>
feat_names = ['Pclass', 'Sex_male', 'Sex_female', 'Embarked_C', 'Embarked_Q', 'Embarked_S', 'Parch', 'SibSp', 'Fare']
targ_names = ['Dead(0)', 'Survived(1)']
train_class = dataset[['Survived']]
train_feature = dataset[feat_names]
train_feature.head()
def load_image_on_ram(p,names,k):
for i in tqdm(names):
path= p+i+'.png'
image = read_image2(path,k)
 image_dic[i] = image<define_variables>
clf = DecisionTreeClassifier(random_state=0)
 scoring = {'acc': 'accuracy',
'prec_macro': 'precision_macro',
'rec_macro': 'recall_macro',
'f1_macro': 'f1_macro'}
scores = cross_validate(clf, train_feature, train_class, cv=10, scoring=scoring)
 print('Accuracy score : %.3f' % scores['test_acc'].mean())
 print('Precisoin score : %.3f' % scores['test_prec_macro'].mean())
 print('Recall score : %.3f' % scores['test_rec_macro'].mean())
 print('F1 score : %.3f' % scores['test_f1_macro'].mean() )
path1 = '.. /input/aptos2019-blindness-detection/train_images/'
path2 = '.. /input/aptos2019-blindness-detection/test_images/'<load_pretrained>
para_grid = {
'min_samples_split' : range(10,500,20),
'max_depth': range(1,20,2),
'criterion':("gini", "entropy")
 }
clf_tree = DecisionTreeClassifier()
clf_cv = GridSearchCV(clf_tree,
para_grid,
scoring='accuracy',
cv=5,
n_jobs=-1)
 clf_cv.fit(train_feature,train_class)
 best_parameters = clf_cv.best_params_
print(best_parameters )
load_image_on_ram(path1,train.id_code.unique() ,1 )<load_pretrained>
clf = clf_cv.best_estimator_
scoring = {'acc': 'accuracy',
'prec_macro': 'precision_macro',
'rec_macro': 'recall_macro',
'f1_macro': 'f1_macro'}
scores = cross_validate(clf, train_feature, train_class, cv=10, scoring=scoring)
 print('Accuracy score : %.3f' % scores['test_acc'].mean())
 print('Precisoin score : %.3f' % scores['test_prec_macro'].mean())
 print('Recall score : %.3f' % scores['test_rec_macro'].mean())
 print('F1 score score : %.3f' % scores['test_f1_macro'].mean() )
image_type = lambda x : load_image_on_ram(path2,test.id_code.unique() ,x )<categorify>
clf2 = clf_cv.best_estimator_
clf2.fit(X_train,y_train)
 predictions = clf2.predict(X_test)
 print(metrics.classification_report(y_test,predictions, target_names=targ_names, digits=3))
def im_b(path=""):
img = path.split('/')[-1]
image = image_dic[img]/255
return image 
<categorify>
fig, ax = plt.subplots(figsize=(3,3))
cm = ConfusionMatrix(clf2, classes=[0, 1], cmap='RdPu')
 cm.score(X_test, y_test)
 for label in cm.ax.texts:
label.set_size(14)
 cm.poof()
class MyImageItemList(ImageList):
def open(self,path:PathOrStr)->Image:
image = im_load(path)
 xx = vision.Image(px=pil2tensor(image,np.float32))
return xx<load_pretrained>
modelviz = clf_cv.best_estimator_
visualizer = ROCAUC(modelviz, classes=["Dead", "Survived"])
 visualizer.fit(X_train, y_train)
 visualizer.score(X_test, y_test)
 visualizer.show()
im_load = im_b<train_model>
data = export_graphviz(clf,out_file=None,feature_names=feat_names,class_names=targ_names,
filled=True, rounded=True,
special_characters=True)
 graph = graphviz.Source(data)
 graph
def get_data(sz=256):
data =(MyImageItemList.from_df(df = train,path = '',folder = path1)
.split_by_rand_pct (.15)
.label_from_df(cols = 'diagnosis' 
)
.add_test(MyImageItemList.from_df(df = test,path='',folder =path2)) 
.transform(tfms,size=sz,resize_method = ResizeMethod.SQUISH, padding_mode = 'zeros')
.databunch(bs = 8,num_workers = 4))
data.normalize(imagenet_stats)
 return data
data = get_data(400 )<define_variables>
test = pd.read_csv('.. /input/titanic/test.csv')
 clf.fit(train_feature, train_class)
 meanFare = dataset['Fare'].mean()
test = test.fillna({"Fare": meanFare})
 test = pd.get_dummies(test, columns=['Sex'])
 test = pd.get_dummies(test, columns=['Embarked'])
 ids = test['PassengerId']
test_feature = test[feat_names]
predictions = clf.predict(test_feature)
 output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.head()
<load_pretrained><EOS> 
output.to_csv('submission.csv', index=False )