Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
Image(".. /input/negotiation/negotiation.jpg" )
clf=DecisionTreeClassifier(max_depth=best_n,random_state=0)
 clf.fit(new_data,label )<save_to_csv>
from sklearn.ensemble import ExtraTreesClassifier
import seaborn as sns
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import GridSearchCV
cn=['0','1','2','3','4','5','6','7','8','9']
dot_data = tree.export_graphviz(clf, out_file=None,
class_names=cn,
filled=True)
 graph = graphviz.Source(dot_data,format="png")
 graph 
<compute_train_metric>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 union = [train, test]
passenger_id = []
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accDT=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accDT}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))<compute_train_metric>
for i, df in enumerate(union):
name = df['Name'].str.split('.', n=1, expand = True)
 name = name[1].str.split(expand = True)[0]
name.replace(['(\() ','(\)) '],'',regex=True, inplace = True)
 df['Name'] = name
del name
mean_age = df[['Name','Age']].groupby(['Name'] ).mean()
df = df.merge(mean_age, on='Name')
 df['Age'] = df['Age_x'].fillna(df['Age_y'])
 df = df.drop(['Age_x', 'Age_y', 'Name'], axis=1)
 df['Age'] = df['Age'].fillna(df['Age'].mean())
 age_buckets= [0,2,10,18,60,200]
age_labels = [0,1,2,3,4]
df['AgeGroup'] = pd.cut(df['Age'], bins=age_buckets, labels=age_labels, right=False)
 parch_buckets= [0,1,200]
parch_labels = [0,1]
df['Parch'] = pd.cut(df['Parch'], bins=parch_buckets, labels=parch_labels, right=False)
 sibsp_buckets= [0,1,2,200]
sibsp_labels = [0,1,2]
df['SibSp'] = pd.cut(df['SibSp'], bins=sibsp_buckets, labels=sibsp_labels, right=False ).astype(np.int8)
 df['Fare'] = df['Fare'].fillna(df['Fare'].mean())
 df['Pclass'] = df['Pclass'].astype(np.int8)
 df['Ability'] = df['Fare'] / df['Pclass']
df['Family'] = df['SibSp'].astype(np.int8)+ df['Parch'].astype(np.int8)+ 1
fare_buckets= [0,23,10000]
fare_labels = [0,1]
df['Fare'] = pd.cut(df['Fare'], bins=fare_buckets, labels=fare_labels, right=False)
 ab_buckets= [0,4,9,15,20,59,70,10000]
ab_labels = [0,1,2,3,4,5,6]
df['Ability'] = pd.cut(df['Ability'], bins=ab_buckets, labels=ab_labels, right=False)
 df = df.sort_values(by=['PassengerId'])
 passenger_id.append(df["PassengerId"])
 df['Sex'] = pd.get_dummies(df['Sex'])
 df['Fare'] = pd.get_dummies(df['Fare'])
 df['SibSp'] = pd.get_dummies(df['SibSp'])
 df['Parch'] = pd.get_dummies(df['Parch'])
 df = df.drop(['Embarked', 'PassengerId', 'Ticket', 'Age', 'Cabin'], axis=1)
 union[i] = df
clf=RandomForestClassifier(n_estimators=600,max_depth=best_n,random_state=0,n_jobs=-1)
 cv_score=cross_val_score(clf,new_data,label,cv=10,scoring='accuracy',n_jobs=-1)
 print(f' with {best_n} as depth of trees and {600} as no of trees we can expect accuracy bwetween {cv_score.min() } and {cv_score.max() } with a mean of {cv_score.mean() }' )<compute_train_metric>
x_train = union[0].drop("Survived", axis=1)
 y_train = union[0]["Survived"]
x_test = union[1]
x_train.shape, y_train.shape, x_test.shape
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accRF=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accRF}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))<import_modules>
ex = ExtraTreesClassifier(random_state = 6, bootstrap=True, oob_score=True)
 ex.fit(x_train, y_train)
 y_pred = ex.predict(x_test)
 ex.score(x_train, y_train)
 score = round(ex.score(x_train, y_train)* 100, 2)
 print('Extremely Randomized Trees', score )
import tensorflow as tf
import keras
from keras import backend as k
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation, BatchNormalization
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
from keras.callbacks import EarlyStopping<prepare_x_and_y>
for i,j in enumerate(x_train.head(1)) :
print('%s: %s' %(j, int(ex.feature_importances_[i]*100)) + '%' )
test_x=test<define_variables>
submission = pd.DataFrame({
"PassengerId": passenger_id[1],
"Survived": y_pred
})
 submission.to_csv('/kaggle/working/submission.csv', index=False)