Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
!pip install sweetviz
train = pd.read_csv('/kaggle/input/anokha-ai-adept/train.csv')
 train.head()
train['label'].value_counts().plot.bar()<normalization>
data_type={
'PassengerId': 'int64',
'Survived' : 'bool',
'Pclass' : 'category',
'Name' : 'string',
'Sex' : 'category',
'Age' : 'float64',
'SibSp' : 'int64',
'Parch' : 'category',
'Ticket' : 'string',
'Fare' : 'float64',
'Cabin' : 'category',
'Embarked' : 'category',
}
train_image = []
for i in tqdm(range(train.shape[0])) :
img = image.load_img('/kaggle/input/anokha-ai-adept/Train/'+train['filename'][i], target_size=(64,64,1), grayscale=True)
 img = image.img_to_array(img)
 img = img/255
train_image.append(img)
 X = np.array(train_image )<load_pretrained>
df_train=pd.read_csv('/kaggle/input/titanic/train.csv', dtype=data_type )
y=train['label'].values
class_weights = class_weight.compute_class_weight('balanced',np.unique(y),y)
 class_weights = {0:1.4 ,
1: 0.8,
2:1}<categorify>
X = df_train.drop(columns=['Survived','Name', 'PassengerId'], axis=1)
 di = {False: 0, True: 1}
y = df_train.replace({"Survived": di} ).Survived
y = to_categorical(y )<split>
categorical_cols = X.select_dtypes(include=['category', object, 'string'] ).columns.to_list()
for col in categorical_cols:
X[col] = X[col].astype('category')
 X[col] = X[col].cat.add_categories('Unknown')
 X[col] = X[col].fillna('Unknown' )
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.3 )<choose_model_class>
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size = 0.3, stratify=y, random_state = 123 )
model = Sequential()
model.add(Conv2D(256, kernel_size=(3, 3),activation='relu',input_shape=(64,64,1)))
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(3, activation='softmax')) 
<train_model>
train_pool = Pool(data=X_train,
label = y_train,
cat_features = categorical_cols)
 eval_pool = Pool(data=X_validation,
label = y_validation,
cat_features = categorical_cols )
def recall_m(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision_m(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
def f1_m(y_true, y_pred):
precision = precision_m(y_true, y_pred)
 recall = recall_m(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['acc',f1_m,precision_m, recall_m] )<choose_model_class>
model = CatBoostClassifier(custom_loss=['Accuracy'],iterations = 30000,od_type = 'Iter', od_wait= 1000,task_type = 'GPU', random_seed=42,)
 model.fit(train_pool,
eval_set = eval_pool,
use_best_model=True,
verbose = False )
earlystop = EarlyStopping(restore_best_weights=True )<choose_model_class>
df_test = pd.read_csv('/kaggle/input/titanic/test.csv', dtype = data_type)
 df_submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 df_test.drop(columns=['Name', 'PassengerId'], axis=1, inplace=True)
 for col in categorical_cols:
df_test[col] = df_test[col].astype('category')
 df_test[col] = df_test[col].cat.add_categories('Unknown')
 df_test[col] = df_test[col].fillna('Unknown')
 test_pool = Pool(data=df_test, cat_features=categorical_cols)
 predictions = model.predict(test_pool)
 df_submission['Survived'] = predictions
df_submission.to_csv('catboost_submission_no_cv.csv', index=False )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_f1_m',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
grid = {'learning_rate': [0.03, 0.1, 0.01],
'iterations' : [500, 1000, 3000, 5000, 10000, 20000],
'depth': [4, 6, 10],
'l2_leaf_reg': [1, 3, 5, 7, 9]}
model = CatBoostClassifier(task_type = 'GPU', eval_metric = 'Accuracy')
 model_randomized_search_result = model.randomized_search(grid,
Pool(X, y, cat_features=categorical_cols),
cv = 3,
plot=True )
callbacks = [earlystop, learning_rate_reduction]<train_model>
best_params = model_randomized_search_result['params']
print('Best parms found', best_params )
model.fit(X_train, y_train, epochs=45, validation_data=(X_test, y_test), batch_size=16, class_weight=class_weights )<load_from_csv>
best_model_params = best_params.copy()
best_model_params.update({
'task_type': 'GPU'
})
 model = CatBoostClassifier(**best_model_params)
 model.fit(train_pool,
eval_set = eval_pool,
use_best_model=True,
verbose = False )
test_file = pd.read_csv('/kaggle/input/anokha-ai-adept/test.csv' )<normalization>
predictions = model.predict(test_pool)
 df_submission['Survived'] = predictions
df_submission.to_csv('cb_submission_with_cv_param.csv', index=False )
test_image = []
for i in tqdm(range(test_file.shape[0])) :
img = image.load_img('/kaggle/input/anokha-ai-adept/Test/'+test_file['filename'][i], target_size=(64,64,1), grayscale=True)
 img = image.img_to_array(img)
 img = img/255
test_image.append(img)
 test = np.array(test_image )<predict_on_test>
X = df_train.drop(columns=['Survived','PassengerId'], axis=1)
 for col in categorical_cols:
X[col] = X[col].astype('category')
 X[col] = X[col].cat.add_categories('Unknown')
 X[col] = X[col].fillna('Unknown')
 di = {False: 0, True: 1}
y = df_train.replace({"Survived": di} ).Survived
text_feature = ['Name']
prediction = model.predict_classes(test )<save_to_csv>
X.Name = X.Name.apply(lambda x: x.lower())
 X.Name = X.Name.apply(lambda x: x.translate(string.punctuation))
X.Name = X.Name.apply(lambda x: x.translate(string.digits))
sample = pd.read_csv('/kaggle/input/anokha-ai-adept/SampleSolution.csv')
 sample['label'] = prediction
sample.to_csv('sample1.csv', header=True, index=False )<install_modules>
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size = 0.3, stratify=y, random_state = 123)
 train_text_pool = Pool(data=X_train,
label = y_train,
cat_features = categorical_cols,
text_features = text_feature,
feature_names=list(X_train), 
)
 eval_text_pool = Pool(data=X_validation,
label = y_validation,
cat_features = categorical_cols,
text_features = text_feature,
feature_names=list(X_validation), 
)
 model_with_text = CatBoostClassifier(
 custom_loss=['Accuracy'],
iterations = 30000,
od_type = 'Iter',
od_wait= 2000,
task_type = 'GPU',
random_seed=42,)
 model_with_text.fit(train_text_pool,
eval_set = eval_text_pool,
use_best_model=True,
metric_period=1000,
verbose = True )
<load_from_csv><EOS> 
df_test = pd.read_csv('/kaggle/input/titanic/test.csv', dtype = data_type)
 df_test.Name = df_test.Name.apply(lambda x: x.lower())
 df_test.Name = df_test.Name.apply(lambda x: x.translate(string.punctuation))
df_test.Name = df_test.Name.apply(lambda x: x.translate(string.digits))
df_submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 df_test.drop(columns=['PassengerId'], axis=1, inplace=True)
 for col in categorical_cols:
df_test[col] = df_test[col].astype('category')
 df_test[col] = df_test[col].cat.add_categories('Unknown')
 df_test[col] = df_test[col].fillna('Unknown')
 test_pool = Pool(data=df_test, cat_features=categorical_cols, text_features = text_feature)
 predictions = model_with_text.predict(test_pool)
 df_submission['Survived'] = predictions
df_submission.to_csv('cb_with_text.csv', index=False )