{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:41.589287Z","iopub.execute_input":"2026-07-21T06:03:41.589691Z","iopub.status.idle":"2026-07-21T06:03:41.602615Z","shell.execute_reply.started":"2026-07-21T06:03:41.589660Z","shell.execute_reply":"2026-07-21T06:03:41.601414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndf = pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:41.605028Z","iopub.execute_input":"2026-07-21T06:03:41.605540Z","iopub.status.idle":"2026-07-21T06:03:43.772332Z","shell.execute_reply.started":"2026-07-21T06:03:41.605508Z","shell.execute_reply":"2026-07-21T06:03:43.771428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.773392Z","iopub.execute_input":"2026-07-21T06:03:43.773648Z","iopub.status.idle":"2026-07-21T06:03:43.789394Z","shell.execute_reply.started":"2026-07-21T06:03:43.773623Z","shell.execute_reply":"2026-07-21T06:03:43.788515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_counts = df['label'].value_counts().sort_index()\ndisplay(target_counts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.790742Z","iopub.execute_input":"2026-07-21T06:03:43.791204Z","iopub.status.idle":"2026-07-21T06:03:43.807298Z","shell.execute_reply.started":"2026-07-21T06:03:43.791129Z","shell.execute_reply":"2026-07-21T06:03:43.806397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = df.iloc[:,1:].to_numpy()\ntarget = df.iloc[:,0].to_numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.809693Z","iopub.execute_input":"2026-07-21T06:03:43.810070Z","iopub.status.idle":"2026-07-21T06:03:43.824071Z","shell.execute_reply.started":"2026-07-21T06:03:43.810045Z","shell.execute_reply":"2026-07-21T06:03:43.823097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(data[1,:].reshape(int(784**0.5),int(784**0.5),))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.825233Z","iopub.execute_input":"2026-07-21T06:03:43.825780Z","iopub.status.idle":"2026-07-21T06:03:43.971250Z","shell.execute_reply.started":"2026-07-21T06:03:43.825742Z","shell.execute_reply":"2026-07-21T06:03:43.970239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.972514Z","iopub.execute_input":"2026-07-21T06:03:43.972908Z","iopub.status.idle":"2026-07-21T06:03:43.979135Z","shell.execute_reply.started":"2026-07-21T06:03:43.972871Z","shell.execute_reply":"2026-07-21T06:03:43.978230Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.980420Z","iopub.execute_input":"2026-07-21T06:03:43.980733Z","iopub.status.idle":"2026-07-21T06:03:43.996070Z","shell.execute_reply.started":"2026-07-21T06:03:43.980709Z","shell.execute_reply":"2026-07-21T06:03:43.995252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:43.997199Z","iopub.execute_input":"2026-07-21T06:03:43.997430Z","iopub.status.idle":"2026-07-21T06:03:44.013062Z","shell.execute_reply.started":"2026-07-21T06:03:43.997408Z","shell.execute_reply":"2026-07-21T06:03:44.012012Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:44.014549Z","iopub.execute_input":"2026-07-21T06:03:44.014975Z","iopub.status.idle":"2026-07-21T06:03:44.054089Z","shell.execute_reply.started":"2026-07-21T06:03:44.014931Z","shell.execute_reply":"2026-07-21T06:03:44.053139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, ConfusionMatrixDisplay\nfrom sklearn.neural_network import MLPClassifier\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:44.055180Z","iopub.execute_input":"2026-07-21T06:03:44.055723Z","iopub.status.idle":"2026-07-21T06:03:44.060307Z","shell.execute_reply.started":"2026-07-21T06:03:44.055693Z","shell.execute_reply":"2026-07-21T06:03:44.059415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = data/255\ny = target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:44.061602Z","iopub.execute_input":"2026-07-21T06:03:44.061982Z","iopub.status.idle":"2026-07-21T06:03:44.187730Z","shell.execute_reply.started":"2026-07-21T06:03:44.061946Z","shell.execute_reply":"2026-07-21T06:03:44.186639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:44.188871Z","iopub.execute_input":"2026-07-21T06:03:44.189734Z","iopub.status.idle":"2026-07-21T06:03:44.196639Z","shell.execute_reply.started":"2026-07-21T06:03:44.189691Z","shell.execute_reply":"2026-07-21T06:03:44.195675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42, stratify=y\n)\n\nmodel = MLPClassifier(hidden_layer_sizes=(64,), max_iter=300, random_state=42)\nmodel.fit(X_train, y_train)\n\npredictions = model.predict(X_test)\naccuracy = accuracy_score(y_test, predictions)\nprint(f\"Accuracy: {accuracy:.3f}\")\n\nConfusionMatrixDisplay.from_predictions(y_test, predictions)\nplt.title(\"Digits classifier confusion matrix\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:03:44.199741Z","iopub.execute_input":"2026-07-21T06:03:44.200140Z","iopub.status.idle":"2026-07-21T06:04:32.759766Z","shell.execute_reply.started":"2026-07-21T06:03:44.200114Z","shell.execute_reply":"2026-07-21T06:04:32.759004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = pd.read_csv('/kaggle/input/competitions/digit-recognizer/test.csv')\n\npredictions = model.predict(X_test.to_numpy())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:04:32.760761Z","iopub.execute_input":"2026-07-21T06:04:32.761513Z","iopub.status.idle":"2026-07-21T06:04:34.315068Z","shell.execute_reply.started":"2026-07-21T06:04:32.761484Z","shell.execute_reply":"2026-07-21T06:04:34.314225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dfout = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(predictions) + 1),\n    \"Label\": predictions\n})\n\nprint(dfout)\ndfout.to_csv(\"labels.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-21T06:04:34.316128Z","iopub.execute_input":"2026-07-21T06:04:34.316869Z","iopub.status.idle":"2026-07-21T06:04:34.368309Z","shell.execute_reply.started":"2026-07-21T06:04:34.316813Z","shell.execute_reply":"2026-07-21T06:04:34.367590Z"}},"outputs":[],"execution_count":null}]}