Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
%matplotlib inline
warnings.filterwarnings('ignore')
batch_size = 64
IMG_HEIGHT = 224
IMG_WIDTH = 224<define_variables>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train_image_generator = ImageDataGenerator(rescale=1./255, validation_split=0.2, horizontal_flip=True, rotation_range=5, shear_range=0.2, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2)
 validation_image_generator = ImageDataGenerator(rescale=1./255)
<categorify>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
def oh_to_class(Y):
result = np.zeros(( Y.shape[0], 1))
for i in range(Y.shape[0]):
result[i]=np.argmax(Y[i])
 return result<define_variables>
train.Embarked.value_counts() /train.shape[0]
train["Embarked"] = train["Embarked"].fillna("S")
 test["Embarked"] = test["Embarked"].fillna("S" )
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='categorical')
<define_variables>
group_dataset = [train,test]
for df in group_dataset:
nan_age_index = list(df["Age"][df["Age"].isnull() ].index)
 for i in nan_age_index :
age_med = df["Age"].median()
age_pred = df["Age"][(( df['SibSp'] == df.iloc[i]["SibSp"])& 
(df['Parch'] == df.iloc[i]["Parch"])& 
(df['Pclass'] == df.iloc[i]["Pclass"])) ].median()
if not np.isnan(age_pred):
df['Age'].iloc[i] = age_pred
else :
df['Age'].iloc[i] = age_med
f, ax = plt.subplots(figsize =(10,5))
ax = sns.kdeplot(train.Age,shade=True)
 ax.axvline(train.Age.mean() ,color ='r',linestyle = '--')
 ax.legend(['Age','Age mean'],fontsize=12)
 ax.set_xlabel('Age',fontsize=12,color='black')
 ax.set_title('Age Distribution',color='black',fontsize=14)
 y_axis = ax.axes.get_yaxis().set_visible(False)
 sns.despine(left=True )
test_dir='/kaggle/input/car-classificationproject-vision/Test/Test/'<define_variables>
col_drop = ['Cabin','Name','Ticket']
group_dataset = [train,test]
for df in group_dataset:
df.drop(columns=col_drop,axis=1,inplace=True )
test_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=test_dir,
shuffle=False,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode=None)
<define_variables>
col_dummies = ['Embarked','Sex','family_size','Title','Pclass']
train = pd.get_dummies(train,columns=col_dummies,drop_first=True)
 test = pd.get_dummies(test,columns=col_dummies,drop_first=True)
 train.dropna(inplace=True)
 train.Survived =train.Survived.astype('int' )
sample_training_images, labels = next(train_data_gen)
<find_best_score>
train.drop('PassengerId',axis=1,inplace=True)
 X = train.drop('Survived',axis= 1)
 y = train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
 sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 X_test = sc.transform(X_test )
train_data_gen.class_indices<categorify>
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report,accuracy_score
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier,AdaBoostClassifier,GradientBoostingClassifier,ExtraTreesClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import GridSearchCV, cross_val_score, StratifiedKFold, learning_curve
from sklearn.neural_network import MLPClassifier
import xgboost as xgb
def output_labels_to_map_labels(Y, output_label_dict, map_label_dict):
result = np.zeros(Y.shape)
 for i in range(Y.shape[0]):
curr_label_ind = Y[i][0]
car_name=0
for key, val in output_label_dict.items() :
if val == curr_label_ind:
car_name = key
break
print
req_label_index = map_label_dict[car_name]['Class Numbers']
result[i][0]=req_label_index
return result<categorify>
def model_select(classifier,kfold_n):
cv_result = []
cv_means = []
kfold = StratifiedKFold(n_splits= kfold_n)
 cv_result.append(cross_val_score(classifier, X_train, y = y_train, scoring = "accuracy", cv = kfold, n_jobs=4))
cv_means.append(np.mean(cv_result))
return cv_means
model_type = [LogisticRegression() ,SVC() ,KNeighborsClassifier() ,GaussianNB() ,RandomForestClassifier() ,
AdaBoostClassifier() ,GradientBoostingClassifier() ,DecisionTreeClassifier() ,ExtraTreesClassifier() ,
MLPClassifier() ,LinearDiscriminantAnalysis() ]
model_score = [model_select(i,5)for i in model_type]
classifier = ['Logistic Regression','SVC','KNeighbors','Naive Bayes','Random Forest',
'AdaBoost','Gradient Boosting','Decision Tree','Extra Trees','Multiple Layer Perceptron',
'Linear Discriminant']
gbm = xgb.XGBClassifier(n_estimators=1000, learning_rate=0.05,silent=False,n_jobs=4)
 gbm.fit(X_train, y_train,early_stopping_rounds=5,eval_set=[(X_test, y_test)], verbose=False)
 y_pred = gbm.predict(X_test)
 acc_score_1 = accuracy_score(y_pred,y_test)
 ml_model = pd.DataFrame(model_score,classifier ).reset_index()
ml_model.columns=['Model','acc_score']
ml_model.loc[11] = ['xgboost',acc_score_1]
ml_model.sort_values('acc_score',ascending = False,inplace=True)
 ml_model.reset_index(drop=True,inplace = True)
 ml_model
f, ax = plt.subplots(figsize=(10,8))
sns.barplot('acc_score','Model',data=ml_model, ax=ax,palette='RdBu_r',edgecolor=".2")
 for i in ax.patches:
ax.text(i.get_width() +.01, i.get_y() +.55, \
str(round(( i.get_width()), 2)) , fontsize=12, color='black')
 kwargs= {'length':3, 'width':1, 'colors':'black','labelsize':'large'}
ax.tick_params(**kwargs)
 x_axis = ax.axes.get_xaxis().set_visible(False)
 ax.set_title('Model & Accuracy Score',fontsize=16)
 sns.despine(bottom=True)
 plt.show()
output_labels_to_map_labels(oh_to_class(labels[10:15]),train_data_gen.class_indices, car_map_dict )<define_variables>
def keras_model(kernel_init,act_func,optimizer):
classifier = Sequential()
classifier.add(Dense(9,input_shape=(X_train.shape[1],),kernel_initializer=kernel_init,activation=act_func))
classifier.add(Dense(9,kernel_initializer=kernel_init,activation=act_func))
classifier.add(Dense(9,kernel_initializer=kernel_init,activation=act_func))
classifier.add(Dense(9,kernel_initializer=kernel_init,activation=act_func))
classifier.add(Dense(9,kernel_initializer=kernel_init,activation=act_func))
classifier.add(Dense(1,kernel_initializer=kernel_init,activation='sigmoid'))
classifier.compile(optimizer=optimizer,loss = 'binary_crossentropy',metrics=['accuracy'])
 return classifier
feature_extractor_url = "https://tfhub.dev/tensorflow/resnet_50/feature_vector/1" 
<choose_model_class>
clf = keras_model('lecun_normal','selu','adam')
 clf.fit(X_train,y_train,batch_size=10,epochs=150,validation_data=(X_test,y_test))
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
input_shape=(224,224,3)) 
<choose_model_class>
y_pred_2 = clf.predict_classes(X_test,batch_size=32)
 y_pred =(y_pred_2 > 0.5)
 def y_pred_val(input):
y_pred_1 = []
for i in list(input):
if i == True:
y_pred_1.append(1)
 else:
y_pred_1.append(0)
 return y_pred_1
accuracy_score(y_test,y_pred_2 )
model.compile(
 optimizer=tf.keras.optimizers.Adam() ,
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['acc'])
<train_model>
test_1 = test.drop('PassengerId',axis=1)
 test_1 = sc.transform(test_1)
 y_test_pred = clf.predict_classes(test_1,batch_size=32)
 y_test_pred =(y_test_pred > 0.5)
 y_test_pred_2 = y_pred_val(y_test_pred )
class CollectBatchStats(tf.keras.callbacks.Callback):
def __init__(self):
self.batch_losses = []
self.batch_acc = []
def on_train_batch_end(self, batch, logs=None):
self.batch_losses.append(logs['loss'])
 self.batch_acc.append(logs['acc'])
 self.model.reset_metrics() 
<train_model>
submit = pd.DataFrame({"PassengerId": test["PassengerId"],"Survived": y_test_pred_2})
 submit.to_csv(".. /working/submit.csv", index=False )