{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:29.263270Z","iopub.execute_input":"2026-01-12T03:53:29.263566Z","iopub.status.idle":"2026-01-12T03:53:30.556026Z","shell.execute_reply.started":"2026-01-12T03:53:29.263540Z","shell.execute_reply":"2026-01-12T03:53:30.555011Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Import Required Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\n\nfrom warnings import filterwarnings\nfilterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:30.558302Z","iopub.execute_input":"2026-01-12T03:53:30.558848Z","iopub.status.idle":"2026-01-12T03:53:51.050608Z","shell.execute_reply.started":"2026-01-12T03:53:30.558785Z","shell.execute_reply":"2026-01-12T03:53:51.049796Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Dataset","metadata":{}},{"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":"2026-01-12T03:53:51.051748Z","iopub.execute_input":"2026-01-12T03:53:51.052371Z","iopub.status.idle":"2026-01-12T03:53:55.493495Z","shell.execute_reply.started":"2026-01-12T03:53:51.052343Z","shell.execute_reply":"2026-01-12T03:53:55.492449Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Explore Datasets","metadata":{}},{"cell_type":"code","source":"print(\"Train shape:\", train.shape)\nprint(\"Test shape:\", test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:55.494602Z","iopub.execute_input":"2026-01-12T03:53:55.494998Z","iopub.status.idle":"2026-01-12T03:53:55.499885Z","shell.execute_reply.started":"2026-01-12T03:53:55.494968Z","shell.execute_reply":"2026-01-12T03:53:55.499002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_labels=train[\"label\"]\ntrain_labels.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:55.501018Z","iopub.execute_input":"2026-01-12T03:53:55.501437Z","iopub.status.idle":"2026-01-12T03:53:55.522426Z","shell.execute_reply.started":"2026-01-12T03:53:55.501413Z","shell.execute_reply":"2026-01-12T03:53:55.521664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(train))\nprint(len(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:55.523616Z","iopub.execute_input":"2026-01-12T03:53:55.523965Z","iopub.status.idle":"2026-01-12T03:53:55.537426Z","shell.execute_reply.started":"2026-01-12T03:53:55.523941Z","shell.execute_reply":"2026-01-12T03:53:55.536414Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"plt.imshow(train.iloc[1, 1:].values.reshape(28, 28), cmap=plt.cm.binary)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:55.540165Z","iopub.execute_input":"2026-01-12T03:53:55.540455Z","iopub.status.idle":"2026-01-12T03:53:55.753565Z","shell.execute_reply.started":"2026-01-12T03:53:55.540431Z","shell.execute_reply":"2026-01-12T03:53:55.752764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    # get the pixel values for the i-th row (skip the 'label' column) and reshape to 28x28\n    pixels = train.iloc[i, 1:].values.reshape(28,28)\n    plt.imshow(pixels, cmap=plt.cm.binary)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:55.754690Z","iopub.execute_input":"2026-01-12T03:53:55.755019Z","iopub.status.idle":"2026-01-12T03:53:56.260718Z","shell.execute_reply.started":"2026-01-12T03:53:55.754994Z","shell.execute_reply":"2026-01-12T03:53:56.260019Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Pixel Values","metadata":{}},{"cell_type":"code","source":"print(\"pixel values\")\n# use the already-defined `pixels` ndarray (28x28) instead of indexing DataFrame by 0\nfor row in pixels:\n    for pixel in row:\n        print(\"{:3}\".format(pixel), end=\" \")\n    print()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.261770Z","iopub.execute_input":"2026-01-12T03:53:56.262153Z","iopub.status.idle":"2026-01-12T03:53:56.272320Z","shell.execute_reply.started":"2026-01-12T03:53:56.262127Z","shell.execute_reply":"2026-01-12T03:53:56.271191Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Selection","metadata":{}},{"cell_type":"code","source":"x=train.drop(\"label\",axis=1)\ny=train[\"label\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.273386Z","iopub.execute_input":"2026-01-12T03:53:56.273677Z","iopub.status.idle":"2026-01-12T03:53:56.405755Z","shell.execute_reply.started":"2026-01-12T03:53:56.273653Z","shell.execute_reply":"2026-01-12T03:53:56.404652Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Split","metadata":{}},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.406888Z","iopub.execute_input":"2026-01-12T03:53:56.407140Z","iopub.status.idle":"2026-01-12T03:53:56.653641Z","shell.execute_reply.started":"2026-01-12T03:53:56.407119Z","shell.execute_reply":"2026-01-12T03:53:56.652743Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Normalize","metadata":{}},{"cell_type":"code","source":"# Normalize and reshape the splits\nx_train = x_train.values.astype('float32') / 255.0\nx_train = x_train.reshape((x_train.shape[0], 28, 28, 1))\n\nx_test = x_test.values.astype('float32') / 255.0\nx_test = x_test.reshape((x_test.shape[0], 28, 28, 1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.654956Z","iopub.execute_input":"2026-01-12T03:53:56.655446Z","iopub.status.idle":"2026-01-12T03:53:56.783793Z","shell.execute_reply.started":"2026-01-12T03:53:56.655409Z","shell.execute_reply":"2026-01-12T03:53:56.783030Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import layers\n\nmodel = tf.keras.Sequential([\n    layers.InputLayer(input_shape=(28,28,1)),\n    \n    layers.Conv2D(filters=10, kernel_size=(3,3), activation=\"relu\"),\n    layers.Conv2D(filters=10, kernel_size=(3,3), activation=\"relu\"),\n    layers.MaxPooling2D(pool_size=(2,2)),\n\n    layers.Conv2D(filters=20, kernel_size=(3,3), activation=\"relu\"),\n    layers.Conv2D(filters=20, kernel_size=(3,3), activation=\"relu\"),\n    layers.MaxPooling2D(pool_size=(2,2)),\n\n    layers.Flatten(),\n    layers.Dense(100, activation=\"softmax\"),\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.785030Z","iopub.execute_input":"2026-01-12T03:53:56.785354Z","iopub.status.idle":"2026-01-12T03:53:56.915108Z","shell.execute_reply.started":"2026-01-12T03:53:56.785325Z","shell.execute_reply":"2026-01-12T03:53:56.914204Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Summary","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.916076Z","iopub.execute_input":"2026-01-12T03:53:56.916333Z","iopub.status.idle":"2026-01-12T03:53:56.937549Z","shell.execute_reply.started":"2026-01-12T03:53:56.916305Z","shell.execute_reply":"2026-01-12T03:53:56.936783Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Compile model","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer=\"adam\",\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n              metrics=[\"accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.938630Z","iopub.execute_input":"2026-01-12T03:53:56.938936Z","iopub.status.idle":"2026-01-12T03:53:56.954753Z","shell.execute_reply.started":"2026-01-12T03:53:56.938912Z","shell.execute_reply":"2026-01-12T03:53:56.953682Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Fit model","metadata":{}},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:53:56.956066Z","iopub.execute_input":"2026-01-12T03:53:56.956621Z","iopub.status.idle":"2026-01-12T03:55:11.765333Z","shell.execute_reply.started":"2026-01-12T03:53:56.956596Z","shell.execute_reply":"2026-01-12T03:55:11.764370Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"loss,accuracy=model.evaluate(x_test,y_test)\nprint(f\"Test loss: {loss:.3f}\")\nprint(f\"Test accuracy: {accuracy:.2f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:55:11.766484Z","iopub.execute_input":"2026-01-12T03:55:11.766834Z","iopub.status.idle":"2026-01-12T03:55:13.614063Z","shell.execute_reply.started":"2026-01-12T03:55:11.766781Z","shell.execute_reply":"2026-01-12T03:55:13.613245Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Test Data Prediction","metadata":{}},{"cell_type":"code","source":"# Normalize and reshape the test data for prediction\nx_test_prediction = test.values.astype('float32') / 255.0\nx_test_prediction = x_test_prediction.reshape((x_test_prediction.shape[0], 28, 28, 1))\n\n# Make predictions with the preprocessed test data\nresult = model.predict(x_test_prediction)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:55:13.615032Z","iopub.execute_input":"2026-01-12T03:55:13.615293Z","iopub.status.idle":"2026-01-12T03:55:19.792436Z","shell.execute_reply.started":"2026-01-12T03:55:13.615261Z","shell.execute_reply":"2026-01-12T03:55:19.791510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"simple_submission=pd.DataFrame({\"Label\":result.argmax(axis=1)})\n\nsimple_submission.index.name=\"ImageId\"\nsimple_submission.index+=1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:55:19.794013Z","iopub.execute_input":"2026-01-12T03:55:19.794330Z","iopub.status.idle":"2026-01-12T03:55:19.802156Z","shell.execute_reply.started":"2026-01-12T03:55:19.794304Z","shell.execute_reply":"2026-01-12T03:55:19.801185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"simple_submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-12T03:55:19.803189Z","iopub.execute_input":"2026-01-12T03:55:19.803440Z","iopub.status.idle":"2026-01-12T03:55:19.834475Z","shell.execute_reply.started":"2026-01-12T03:55:19.803419Z","shell.execute_reply":"2026-01-12T03:55:19.833581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Thanks!","metadata":{}}]}