{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30746,"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":"2024-12-22T11:58:04.591527Z","iopub.execute_input":"2024-12-22T11:58:04.592127Z","iopub.status.idle":"2024-12-22T11:58:05.886882Z","shell.execute_reply.started":"2024-12-22T11:58:04.592099Z","shell.execute_reply":"2024-12-22T11:58:05.885909Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.datasets import load_digits \nimport tensorflow as tf \nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:05.889001Z","iopub.execute_input":"2024-12-22T11:58:05.889415Z","iopub.status.idle":"2024-12-22T11:58:19.503783Z","shell.execute_reply.started":"2024-12-22T11:58:05.889386Z","shell.execute_reply":"2024-12-22T11:58:19.502951Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T11:58:19.504771Z","iopub.execute_input":"2024-12-22T11:58:19.505243Z","iopub.status.idle":"2024-12-22T11:58:23.485402Z","shell.execute_reply.started":"2024-12-22T11:58:19.505220Z","shell.execute_reply":"2024-12-22T11:58:23.484706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T11:58:23.486430Z","iopub.execute_input":"2024-12-22T11:58:23.486719Z","iopub.status.idle":"2024-12-22T11:58:23.492529Z","shell.execute_reply.started":"2024-12-22T11:58:23.486698Z","shell.execute_reply":"2024-12-22T11:58:23.491737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.493665Z","iopub.execute_input":"2024-12-22T11:58:23.494131Z","iopub.status.idle":"2024-12-22T11:58:23.520963Z","shell.execute_reply.started":"2024-12-22T11:58:23.494102Z","shell.execute_reply":"2024-12-22T11:58:23.520168Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.523073Z","iopub.execute_input":"2024-12-22T11:58:23.523338Z","iopub.status.idle":"2024-12-22T11:58:23.528218Z","shell.execute_reply.started":"2024-12-22T11:58:23.523319Z","shell.execute_reply":"2024-12-22T11:58:23.527412Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(['label'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.529056Z","iopub.execute_input":"2024-12-22T11:58:23.529254Z","iopub.status.idle":"2024-12-22T11:58:23.621020Z","shell.execute_reply.started":"2024-12-22T11:58:23.529238Z","shell.execute_reply":"2024-12-22T11:58:23.620021Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.622186Z","iopub.execute_input":"2024-12-22T11:58:23.622816Z","iopub.status.idle":"2024-12-22T11:58:23.628380Z","shell.execute_reply.started":"2024-12-22T11:58:23.622790Z","shell.execute_reply":"2024-12-22T11:58:23.627495Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X /= 255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T11:58:23.629412Z","iopub.execute_input":"2024-12-22T11:58:23.629728Z","iopub.status.idle":"2024-12-22T11:58:23.700708Z","shell.execute_reply.started":"2024-12-22T11:58:23.629707Z","shell.execute_reply":"2024-12-22T11:58:23.699943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = train['label']","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.701644Z","iopub.execute_input":"2024-12-22T11:58:23.701872Z","iopub.status.idle":"2024-12-22T11:58:23.706161Z","shell.execute_reply.started":"2024-12-22T11:58:23.701854Z","shell.execute_reply":"2024-12-22T11:58:23.705166Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_nums(images, labels, num_images=10):\n    plt.figure(figsize=(10, 10))\n    for i in range(num_images):\n        plt.subplot(5, 5, i+1)\n        plt.xticks([])\n        plt.yticks([])\n        plt.grid(False)\n        plt.imshow(images[i], cmap=plt.cm.binary)\n        plt.xlabel(labels[i])\n    ","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.707253Z","iopub.execute_input":"2024-12-22T11:58:23.707601Z","iopub.status.idle":"2024-12-22T11:58:23.716061Z","shell.execute_reply.started":"2024-12-22T11:58:23.707571Z","shell.execute_reply":"2024-12-22T11:58:23.715372Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.matshow(np.array(X.iloc[6]).reshape(28, 28))","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:23.717012Z","iopub.execute_input":"2024-12-22T11:58:23.717591Z","iopub.status.idle":"2024-12-22T11:58:24.078342Z","shell.execute_reply.started":"2024-12-22T11:58:23.717561Z","shell.execute_reply":"2024-12-22T11:58:24.077462Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"reshaped_X = X.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:24.079490Z","iopub.execute_input":"2024-12-22T11:58:24.079883Z","iopub.status.idle":"2024-12-22T11:58:24.085603Z","shell.execute_reply.started":"2024-12-22T11:58:24.079847Z","shell.execute_reply":"2024-12-22T11:58:24.084518Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"reshaped_test = test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:02:57.060931Z","iopub.execute_input":"2024-12-22T12:02:57.061675Z","iopub.status.idle":"2024-12-22T12:02:57.065590Z","shell.execute_reply.started":"2024-12-22T12:02:57.061647Z","shell.execute_reply":"2024-12-22T12:02:57.064640Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_nums(reshaped_X, y)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:24.099208Z","iopub.execute_input":"2024-12-22T11:58:24.100627Z","iopub.status.idle":"2024-12-22T11:58:24.461150Z","shell.execute_reply.started":"2024-12-22T11:58:24.100587Z","shell.execute_reply":"2024-12-22T11:58:24.460263Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_2D = keras.Sequential([\n    layers.Input(shape=[28, 28, 1]),\n\n    layers.Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    \n    layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    \n    layers.Conv2D(filters=256, kernel_size=3, activation='relu', padding='same'),\n    layers.Conv2D(filters=256, kernel_size=3, activation='relu', padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    \n    layers.Flatten(),\n    layers.Dense(units=128, activation='relu'),\n    layers.Dense(units=64, activation='relu'),\n    layers.Dense(units=10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:24.462169Z","iopub.execute_input":"2024-12-22T11:58:24.462499Z","iopub.status.idle":"2024-12-22T11:58:25.400403Z","shell.execute_reply.started":"2024-12-22T11:58:24.462470Z","shell.execute_reply":"2024-12-22T11:58:25.399494Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_2D.summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:25.401266Z","iopub.execute_input":"2024-12-22T11:58:25.401525Z","iopub.status.idle":"2024-12-22T11:58:25.425799Z","shell.execute_reply.started":"2024-12-22T11:58:25.401504Z","shell.execute_reply":"2024-12-22T11:58:25.425001Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_2D.compile(\n    optimizer=tf.keras.optimizers.Adam(epsilon=0.01),\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:25.428490Z","iopub.execute_input":"2024-12-22T11:58:25.428719Z","iopub.status.idle":"2024-12-22T11:58:25.440406Z","shell.execute_reply.started":"2024-12-22T11:58:25.428701Z","shell.execute_reply":"2024-12-22T11:58:25.439781Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model_2D.fit(\n    reshaped_X, y,\n    validation_split=0.2,\n    batch_size=140,\n    epochs=30,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T11:58:25.441431Z","iopub.execute_input":"2024-12-22T11:58:25.441670Z","iopub.status.idle":"2024-12-22T12:00:15.300174Z","shell.execute_reply.started":"2024-12-22T11:58:25.441651Z","shell.execute_reply":"2024-12-22T12:00:15.299501Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:00:15.301494Z","iopub.execute_input":"2024-12-22T12:00:15.301820Z","iopub.status.idle":"2024-12-22T12:00:15.547265Z","shell.execute_reply.started":"2024-12-22T12:00:15.301790Z","shell.execute_reply":"2024-12-22T12:00:15.546492Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"reshaped_test = reshaped_test.astype('float64')\nreshaped_test /= 255","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T12:04:02.830329Z","iopub.execute_input":"2024-12-22T12:04:02.830693Z","iopub.status.idle":"2024-12-22T12:04:02.905651Z","shell.execute_reply.started":"2024-12-22T12:04:02.830666Z","shell.execute_reply":"2024-12-22T12:04:02.904708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds_2d = model_2D.predict(reshaped_test)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:03.915312Z","iopub.execute_input":"2024-12-22T12:04:03.915847Z","iopub.status.idle":"2024-12-22T12:04:06.137754Z","shell.execute_reply.started":"2024-12-22T12:04:03.915820Z","shell.execute_reply":"2024-12-22T12:04:06.137103Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds_2d[0]","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:07.940261Z","iopub.execute_input":"2024-12-22T12:04:07.941064Z","iopub.status.idle":"2024-12-22T12:04:07.946820Z","shell.execute_reply.started":"2024-12-22T12:04:07.941022Z","shell.execute_reply":"2024-12-22T12:04:07.945962Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_2d = np.argmax(preds_2d, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:08.915825Z","iopub.execute_input":"2024-12-22T12:04:08.916356Z","iopub.status.idle":"2024-12-22T12:04:08.920776Z","shell.execute_reply.started":"2024-12-22T12:04:08.916330Z","shell.execute_reply":"2024-12-22T12:04:08.919916Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_2d[0]","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:10.740225Z","iopub.execute_input":"2024-12-22T12:04:10.740923Z","iopub.status.idle":"2024-12-22T12:04:10.745974Z","shell.execute_reply.started":"2024-12-22T12:04:10.740897Z","shell.execute_reply":"2024-12-22T12:04:10.745109Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_nums(reshaped_test, labels_2d)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:17.201193Z","iopub.execute_input":"2024-12-22T12:04:17.201749Z","iopub.status.idle":"2024-12-22T12:04:17.561948Z","shell.execute_reply.started":"2024-12-22T12:04:17.201721Z","shell.execute_reply":"2024-12-22T12:04:17.560885Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': [i for i in range(1, len(labels_2d) + 1)],\n    'label': labels_2d\n})","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:32.735498Z","iopub.execute_input":"2024-12-22T12:04:32.735805Z","iopub.status.idle":"2024-12-22T12:04:32.747753Z","shell.execute_reply.started":"2024-12-22T12:04:32.735783Z","shell.execute_reply":"2024-12-22T12:04:32.746830Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:40.900280Z","iopub.execute_input":"2024-12-22T12:04:40.901054Z","iopub.status.idle":"2024-12-22T12:04:40.922722Z","shell.execute_reply.started":"2024-12-22T12:04:40.901025Z","shell.execute_reply":"2024-12-22T12:04:40.922076Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_2D.save('DigitRecogNorm.h5')","metadata":{"execution":{"iopub.status.busy":"2024-12-22T12:04:43.955509Z","iopub.execute_input":"2024-12-22T12:04:43.955805Z","iopub.status.idle":"2024-12-22T12:04:44.024584Z","shell.execute_reply.started":"2024-12-22T12:04:43.955785Z","shell.execute_reply":"2024-12-22T12:04:44.023871Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}