{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-18T09:54:45.871461Z","iopub.execute_input":"2023-11-18T09:54:45.871752Z","iopub.status.idle":"2023-11-18T09:54:45.881187Z","shell.execute_reply.started":"2023-11-18T09:54:45.871727Z","shell.execute_reply":"2023-11-18T09:54:45.880195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport itertools\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nnp.random.seed(2)\nimport gc\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nfrom keras.utils import to_categorical\n\nsns.set(style='white', context='notebook', palette='deep')","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:45.882855Z","iopub.execute_input":"2023-11-18T09:54:45.883164Z","iopub.status.idle":"2023-11-18T09:54:45.893067Z","shell.execute_reply.started":"2023-11-18T09:54:45.883138Z","shell.execute_reply":"2023-11-18T09:54:45.892276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense , Conv2D , MaxPool2D , Dropout , Flatten\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:45.894109Z","iopub.execute_input":"2023-11-18T09:54:45.894379Z","iopub.status.idle":"2023-11-18T09:54:45.906256Z","shell.execute_reply.started":"2023-11-18T09:54:45.894355Z","shell.execute_reply":"2023-11-18T09:54:45.905569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(train.shape , test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:45.907839Z","iopub.execute_input":"2023-11-18T09:54:45.908118Z","iopub.status.idle":"2023-11-18T09:54:52.177109Z","shell.execute_reply.started":"2023-11-18T09:54:45.908094Z","shell.execute_reply":"2023-11-18T09:54:52.176218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop('label' , axis = 1)\ny = train['label']\nprint(X.shape , y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.179031Z","iopub.execute_input":"2023-11-18T09:54:52.179569Z","iopub.status.idle":"2023-11-18T09:54:52.296558Z","shell.execute_reply.started":"2023-11-18T09:54:52.179536Z","shell.execute_reply":"2023-11-18T09:54:52.295483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.297645Z","iopub.execute_input":"2023-11-18T09:54:52.297940Z","iopub.status.idle":"2023-11-18T09:54:52.521958Z","shell.execute_reply.started":"2023-11-18T09:54:52.297915Z","shell.execute_reply":"2023-11-18T09:54:52.520747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.524985Z","iopub.execute_input":"2023-11-18T09:54:52.526275Z","iopub.status.idle":"2023-11-18T09:54:52.545163Z","shell.execute_reply.started":"2023-11-18T09:54:52.526233Z","shell.execute_reply":"2023-11-18T09:54:52.543839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.isnull().sum().any())\nprint(test.isnull().sum().any())","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.546723Z","iopub.execute_input":"2023-11-18T09:54:52.550145Z","iopub.status.idle":"2023-11-18T09:54:52.603638Z","shell.execute_reply.started":"2023-11-18T09:54:52.550111Z","shell.execute_reply":"2023-11-18T09:54:52.602604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X/255.0\ntest = test/255.0","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.605255Z","iopub.execute_input":"2023-11-18T09:54:52.606346Z","iopub.status.idle":"2023-11-18T09:54:52.713130Z","shell.execute_reply.started":"2023-11-18T09:54:52.606299Z","shell.execute_reply":"2023-11-18T09:54:52.711611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.values.reshape(-1 , 28 , 28 , 1)\ntest = test.values.reshape(-1 , 28 , 28 , 1)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.714658Z","iopub.execute_input":"2023-11-18T09:54:52.716014Z","iopub.status.idle":"2023-11-18T09:54:52.722404Z","shell.execute_reply.started":"2023-11-18T09:54:52.715975Z","shell.execute_reply":"2023-11-18T09:54:52.721286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y , num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.724114Z","iopub.execute_input":"2023-11-18T09:54:52.724525Z","iopub.status.idle":"2023-11-18T09:54:52.742724Z","shell.execute_reply.started":"2023-11-18T09:54:52.724491Z","shell.execute_reply":"2023-11-18T09:54:52.741386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train , X_test , y_train , y_test = train_test_split(X , y , test_size = 0.2 , random_state = 2)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:52.744182Z","iopub.execute_input":"2023-11-18T09:54:52.744534Z","iopub.status.idle":"2023-11-18T09:54:53.144844Z","shell.execute_reply.started":"2023-11-18T09:54:52.744502Z","shell.execute_reply":"2023-11-18T09:54:53.143840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[5])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:53.148258Z","iopub.execute_input":"2023-11-18T09:54:53.148555Z","iopub.status.idle":"2023-11-18T09:54:53.555746Z","shell.execute_reply.started":"2023-11-18T09:54:53.148531Z","shell.execute_reply":"2023-11-18T09:54:53.554763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32 , kernel_size = (5,5) , padding = 'Same' , input_shape = (28,28,1) , activation = 'relu'),\n    Conv2D(32 , kernel_size = (5,5) , padding = 'Same' , input_shape = (28,28,1) , activation = 'relu'),\n    MaxPool2D(pool_size = (2,2)),\n    Dropout(0.25),\n    \n    Conv2D(64 , kernel_size = (3,3) , padding = 'Same' , activation = 'relu'),\n    Conv2D(64 , kernel_size = (3,3) , padding = 'Same' , activation = 'relu'),\n    MaxPool2D(pool_size = (2,2)),\n    Dropout(0.25),\n    \n    Flatten(),\n    Dense(256 , activation = 'relu'),\n    Dropout(0.5),\n    Dense(10 , activation = 'Softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:53.557261Z","iopub.execute_input":"2023-11-18T09:54:53.557644Z","iopub.status.idle":"2023-11-18T09:54:53.665769Z","shell.execute_reply.started":"2023-11-18T09:54:53.557611Z","shell.execute_reply":"2023-11-18T09:54:53.664943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = Adam(learning_rate = 0.001) , loss = 'categorical_crossentropy' , metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:53.666957Z","iopub.execute_input":"2023-11-18T09:54:53.667285Z","iopub.status.idle":"2023-11-18T09:54:53.680153Z","shell.execute_reply.started":"2023-11-18T09:54:53.667251Z","shell.execute_reply":"2023-11-18T09:54:53.679294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_data = ImageDataGenerator(rotation_range=10,\n                               zoom_range=0.1,\n                               width_shift_range=0.1,\n                               height_shift_range=0.1)\nimage_data.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:53.681242Z","iopub.execute_input":"2023-11-18T09:54:53.681532Z","iopub.status.idle":"2023-11-18T09:54:53.752864Z","shell.execute_reply.started":"2023-11-18T09:54:53.681507Z","shell.execute_reply":"2023-11-18T09:54:53.752030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(image_data.flow(X_train , y_train, batch_size = 86 ) , epochs = 4 , validation_data=(X_test , y_test) , verbose = 2 )","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:54:53.754169Z","iopub.execute_input":"2023-11-18T09:54:53.754826Z","iopub.status.idle":"2023-11-18T09:55:55.425952Z","shell.execute_reply.started":"2023-11-18T09:54:53.754780Z","shell.execute_reply":"2023-11-18T09:55:55.425149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test , y_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:55:55.427147Z","iopub.execute_input":"2023-11-18T09:55:55.427456Z","iopub.status.idle":"2023-11-18T09:55:56.382868Z","shell.execute_reply.started":"2023-11-18T09:55:55.427431Z","shell.execute_reply":"2023-11-18T09:55:56.381930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\nY_pred = model.predict(X_test)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(y_test,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(10)) ","metadata":{"execution":{"iopub.status.busy":"2023-11-18T09:55:56.384164Z","iopub.execute_input":"2023-11-18T09:55:56.384451Z","iopub.status.idle":"2023-11-18T09:55:57.984478Z","shell.execute_reply.started":"2023-11-18T09:55:56.384427Z","shell.execute_reply":"2023-11-18T09:55:57.983506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"error = (Y_pred_classes - Y_true != 0)\n\nY_pred_classes_error = Y_pred_classes[error]\nY_pred_error = Y_pred[error]\nY_true_error = Y_true[error]\nX_test_error = X_test[error]","metadata":{"execution":{"iopub.status.busy":"2023-11-18T10:10:03.355181Z","iopub.execute_input":"2023-11-18T10:10:03.355638Z","iopub.status.idle":"2023-11-18T10:10:03.361726Z","shell.execute_reply.started":"2023-11-18T10:10:03.355608Z","shell.execute_reply":"2023-11-18T10:10:03.360832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_error(index_error , image_error , pred_error , obs_error):\n    n = 0\n    n_rows = 2\n    n_cols = 3\n    fig , ax = plt.subplots(n_rows , n_cols , sharex = True , sharey = True)\n    for row in range(n_rows):\n        for col in range(n_cols):\n            error = index_error[n]\n            ax[row , col].imshow((image_error[error]).reshape((28,28)))\n            ax[row , col].set_title(\"Predicted label :{}\\nTrue label :{}\".format(pred_error[error],obs_error[error]))\n            n += 1\n\nY_pred_error_prob = np.max(Y_pred_error , axis = 1)\ntrue_prob_error = np.diagonal(np.take(Y_pred_error , Y_true_error , axis = 1))\ndelta_pred_true_error = Y_pred_error_prob - true_prob_error\nsorted_delta_error = np.argsort(delta_pred_true_error)\nmost_important_error = sorted_delta_error[-6:]\ndisplay_error(most_important_error , X_test_error , Y_pred_classes_error , Y_true_error)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T11:08:24.363945Z","iopub.execute_input":"2023-11-18T11:08:24.364638Z","iopub.status.idle":"2023-11-18T11:08:25.646747Z","shell.execute_reply.started":"2023-11-18T11:08:24.364603Z","shell.execute_reply":"2023-11-18T11:08:25.645805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(test)\nresult = np.argmax(result , axis = 1)\nresult = pd.Series(result , name = 'Label')","metadata":{"execution":{"iopub.status.busy":"2023-11-18T11:25:31.457157Z","iopub.execute_input":"2023-11-18T11:25:31.457942Z","iopub.status.idle":"2023-11-18T11:25:33.859469Z","shell.execute_reply.started":"2023-11-18T11:25:31.457912Z","shell.execute_reply":"2023-11-18T11:25:33.858367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1 , 28001) , name = 'ImageId') , result] ,axis = 1)\nsubmission.to_csv('submission.csv' , index = False)","metadata":{"execution":{"iopub.status.busy":"2023-11-18T11:26:54.382574Z","iopub.execute_input":"2023-11-18T11:26:54.382944Z","iopub.status.idle":"2023-11-18T11:26:54.452094Z","shell.execute_reply.started":"2023-11-18T11:26:54.382916Z","shell.execute_reply":"2023-11-18T11:26:54.451109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}