{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30804,"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":"2024-12-13T21:19:20.797364Z","iopub.execute_input":"2024-12-13T21:19:20.798355Z","iopub.status.idle":"2024-12-13T21:19:20.806698Z","shell.execute_reply.started":"2024-12-13T21:19:20.798312Z","shell.execute_reply":"2024-12-13T21:19:20.805266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix, classification_report, ConfusionMatrixDisplay","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:19:21.273940Z","iopub.execute_input":"2024-12-13T21:19:21.274322Z","iopub.status.idle":"2024-12-13T21:19:21.279739Z","shell.execute_reply.started":"2024-12-13T21:19:21.274288Z","shell.execute_reply":"2024-12-13T21:19:21.278512Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"originalTrainData = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\noriginalTestData = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\noriginalTrainData.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:19:21.475746Z","iopub.execute_input":"2024-12-13T21:19:21.476154Z","iopub.status.idle":"2024-12-13T21:19:25.687324Z","shell.execute_reply.started":"2024-12-13T21:19:21.476116Z","shell.execute_reply":"2024-12-13T21:19:25.685910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = originalTrainData.drop(columns=\"label\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:19:25.689850Z","iopub.execute_input":"2024-12-13T21:19:25.690327Z","iopub.status.idle":"2024-12-13T21:19:25.846599Z","shell.execute_reply.started":"2024-12-13T21:19:25.690274Z","shell.execute_reply":"2024-12-13T21:19:25.845028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = originalTrainData['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:19:25.848207Z","iopub.execute_input":"2024-12-13T21:19:25.848598Z","iopub.status.idle":"2024-12-13T21:19:25.854369Z","shell.execute_reply.started":"2024-12-13T21:19:25.848560Z","shell.execute_reply":"2024-12-13T21:19:25.853137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:19:25.856547Z","iopub.execute_input":"2024-12-13T21:19:25.856949Z","iopub.status.idle":"2024-12-13T21:19:25.875205Z","shell.execute_reply.started":"2024-12-13T21:19:25.856914Z","shell.execute_reply":"2024-12-13T21:19:25.873899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Convert labels from string to integer\ny = y.astype(int)\n\n# Set the random seed for reproducibility\nnp.random.seed(22)\n\n# TODO: Split the dataset into train and and test sets #################\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.15)\n########################################################################\n\nassert X_train is not None, \"X_train is not set\"\nassert y_train is not None, \"y_train is not set\"\nassert X_test is not None, \"X_test is not set\"\nassert y_test is not None, \"y_test is not set\"\n\nprint(f\"Train set size: {len(X_train)}\")\nprint(f\"Test set size: {len(X_test)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:19:25.877221Z","iopub.execute_input":"2024-12-13T21:19:25.877817Z","iopub.status.idle":"2024-12-13T21:19:26.588448Z","shell.execute_reply.started":"2024-12-13T21:19:25.877761Z","shell.execute_reply":"2024-12-13T21:19:26.587278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier\n\n# Define the MLPClassifier with early stopping\nmlp = MLPClassifier(hidden_layer_sizes=(512,), batch_size=30,early_stopping=True, activation='relu', max_iter=3000,\n                    learning_rate_init=0.001, verbose=True, random_state=22,\n                    validation_fraction=0.2)\n\n# Train the model\nmlp.fit(X_train, y_train)\n\nprint(f\"Best iteration: {mlp.n_iter_}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:32:07.295147Z","iopub.execute_input":"2024-12-13T21:32:07.295567Z","iopub.status.idle":"2024-12-13T21:32:13.070022Z","shell.execute_reply.started":"2024-12-13T21:32:07.295532Z","shell.execute_reply":"2024-12-13T21:32:13.068376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = mlp.predict(originalTestData)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:21:20.002381Z","iopub.execute_input":"2024-12-13T21:21:20.002800Z","iopub.status.idle":"2024-12-13T21:21:20.262972Z","shell.execute_reply.started":"2024-12-13T21:21:20.002765Z","shell.execute_reply":"2024-12-13T21:21:20.261549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': range(1, len(predictions) + 1), 'Label': predictions})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-13T21:21:21.091833Z","iopub.execute_input":"2024-12-13T21:21:21.092254Z","iopub.status.idle":"2024-12-13T21:21:21.123077Z","shell.execute_reply.started":"2024-12-13T21:21:21.092216Z","shell.execute_reply":"2024-12-13T21:21:21.121800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}