Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<groupby>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
data[['Pclass', 'Embarked', 'Survived']].groupby(['Pclass', 'Embarked'] ).mean().round(2 )<feature_engineering>
print(torch.cuda.is_available() , torch.backends.cudnn.enabled )
print('Pclass: ', data[data['Cabin_group'] == 'T']['Pclass'].values)
 data.loc[data['Cabin'] == 'T', 'Cabin_group'] = 'A'<groupby>
train_df = pd.read_csv(INPUT/"train.csv")
 train_df.head(3 )
data[['Cabin_group', 'Pclass', 'Survived']].groupby(['Cabin_group', 'Pclass'] ).mean().round(2 )<categorify>
test_df = pd.read_csv(INPUT/"test.csv")
 test_df.head(3 )
data['Sex_int'] = data['Sex'].replace({'male': 1, 'female': 0})
 data['Embarked_int'] = data['Embarked'].replace({'S': 0, 'C': 1, 'Q':2})
 data['Title_int'] = data['Title'].replace({'Mr': 0, 'Mrs': 1, 'Miss':2, 'Master':3, 'Special':4})
 data['age_group_int'] = data['age_group'].replace({'Adults': 0, 'Middle age': 1, 'Infants':2, 'Adolescents':3, 'Preschool':4, 'Children':5, 'Seniors':6})
 data['Cabin_group_int'] = data['Cabin_group'].replace({'unkown': 0, 'ABC': 1, 'DE':2, 'FG':3} )<filter>
TRAIN = Path(".. /train")
 TEST = Path(".. /test" )
data = data.loc[:,~data.columns.str.endswith('_int')]<data_type_conversions>
sorted(os.listdir(TRAIN))
dummy_features = ['Sex'
, 'Pclass'
, 'Embarked'
, 'Cabin_group'
, 'Title'
, 'age_group'
]
for col in dummy_features:
data[col] = data[col].astype(object)
 drop_features = ['PassengerId', 'Ticket', 'Name', 'Cabin'
,'small_family_size'
,'Alone'
,'SibSp'
,'Parch'
,'Age'
]
data = pd.concat([data, pd.get_dummies(data[dummy_features], drop_first = True)], axis = 1, sort = False)
 data.drop(columns = data[dummy_features], inplace = True)
 data.drop(columns = data[drop_features], inplace = True)
 data.tail()<correct_missing_values>
if os.path.isdir(TRAIN):
print('Train directory has been created')
 else:
print('Train directory creation failed.')
 if os.path.isdir(TEST):
print('Test directory has been created')
 else:
print('Test directory creation failed.' )
train.dropna(inplace = True)
 test.dropna(inplace = True )<split>
from PIL import Image
y = train['Survived']
x = train.drop(columns = target)
 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 42
, stratify = y
)<train_on_grid>
def pix2img(pix_data, filepath):
img_mat = pix_data.reshape(28,28)
 img_mat = img_mat.astype(np.uint8())
 img_dat = Image.fromarray(img_mat)
 img_dat.save(filepath)
RF = ensemble.RandomForestClassifier()
params = {
'n_estimators':[n for n in range(100, 250, 50)]
,'max_depth':[n for n in range(3, 8)]
,'min_samples_leaf': [n for n in range(3, 6, 1)]
,'max_features' : [None]
,'random_state' : [42]
}
RF_model = GridSearchCV(RF, param_grid = params, cv = 5, n_jobs = -1 ).fit(x_train, y_train)
 print("Best Hyper Parameters:",RF_model.best_params_)
 RF_probs = RF_model.predict_proba(x_test)
 RF_probs = RF_probs[:, 1]
RF_auc = roc_auc_score(y_test, RF_probs)
 print('AUC: %.3f' % RF_auc)
 RF_predictions = RF_model.predict(x_test ).astype(int)
 RF_accuracy = accuracy_score(y_test, RF_predictions)
 print("RF accuracy: %.3f" % RF_accuracy)
 print("RF Recall: " + '%.3f' % recall_score(y_test, RF_predictions))
print("RF Precission: " + '%.3f' % precision_score(y_test, RF_predictions))
print("RF cohen_kappa_score: %.3f" % cohen_kappa_score(y_test, RF_predictions))
plt.figure(figsize =(8, 6))
RF_fpr, RF_tpr, RF_thresholds = roc_curve(y_test, RF_probs)
 plt.plot([0, 1], [0, 1], linestyle = '--')
 plt.plot(RF_fpr, RF_tpr, color = 'tab:green')
 plt.show()<predict_on_test>
tfms = get_transforms(do_flip = False)
data['churn_proba'] = RF_model.best_estimator_.predict_proba(data[x.columns])[:,1]<save_to_csv>
print('test : ',TEST)
 print('train: ', TRAIN)
 print(type(TEST))
predict_RF = RF_model.predict(test ).astype(int)
 submit_RF = pd.DataFrame({'PassengerId': testing['PassengerId'],
'Survived': predict_RF})
 filename_RF = 'Titanic Prediction RF.csv'
submit_RF.to_csv(filename_RF,index=False)
 print('Saved file: ' + filename_RF )<load_from_csv>
data = ImageDataBunch.from_folder(
 path =(".. /train"),
test =(".. /test"),
valid_pct = 0.1,
bs = 256,
size = 28,
num_workers = 0,
ds_tfms = tfms
)
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
data.normalize(imagenet_stats)
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<define_variables>
learn = cnn_learner(data, base_arch = models.resnet34, metrics = accuracy,model_dir="/tmp/models", callback_fns=ShowGraph)
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )<define_variables>
doc(fit_one_cycle )
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )<count_missing_values>
learn.fit_one_cycle(3 )
for column in train_data.columns:
print("%-15s%3d" %(column, pd.isnull(train_data[column] ).sum()))<count_missing_values>
learn.save('model1' )
for column in test_data.columns:
print("%-15s%3d" %(column, pd.isnull(test_data[column] ).sum()))<count_duplicates>
learn.load('model1' )
print(train_data.drop_duplicates().groupby('Embarked')['PassengerId'].count() )<feature_engineering>
learn.lr_find()
train_data['Embarked'] = train_data['Embarked'].fillna("S" )<feature_engineering>
learn.fit_one_cycle(30 , slice(1e-3, 1e-2))
test_data['Fare'] = test_data['Fare'].fillna(0 )<feature_engineering>
interp = ClassificationInterpretation.from_learner(learn )
train_data['TicketLength'] = train_data['Ticket'].apply(lambda x: len(x))<feature_engineering>
class_score , y = learn.get_preds(DatasetType.Test )
test_data['TicketLength'] = test_data['Ticket'].apply(lambda x: len(x))<save_to_csv>
probabilities = class_score[0].tolist()
[f"{index}: {probabilities[index]}" for index in range(len(probabilities)) ]
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Fare", "Embarked", "TicketLength"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
class_score = np.argmax(class_score, axis=1 )
import random
import html
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
import tensorflow as tf
import tensorflow.keras.backend as K
import os
from scipy.stats import spearmanr
from scipy.optimize import minimize
from transformers import *
from tensorflow.keras.layers import Flatten, Dense, Dropout, GlobalAveragePooling1D
from tensorflow.keras.models import Model
from sklearn.model_selection import KFold
from scipy.stats import spearmanr<set_options>
ImageId = [os.path.splitext(path)[0] for path in os.listdir(TEST)]
ImageId = [int(path)for path in ImageId]
ImageId = [ID+1 for ID in ImageId]
<set_options><EOS> 
submission = pd.DataFrame({
"ImageId": ImageId,
"Label": class_score
})
 submission.to_csv("submission.csv", index=False)
 display(submission.head(3))
display(submission.tail(3))