{"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":30699,"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)\nimport matplotlib.pyplot as plt\nimport sklearn.model_selection\nimport seaborn as sns\nimport keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation, Dropout, Flatten, Dense\nfrom tensorflow.keras.layers import InputLayer\nfrom tensorflow.keras import optimizers\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-05-25T04:03:02.718749Z","iopub.execute_input":"2024-05-25T04:03:02.718992Z","iopub.status.idle":"2024-05-25T04:03:16.610937Z","shell.execute_reply.started":"2024-05-25T04:03:02.718969Z","shell.execute_reply":"2024-05-25T04:03:16.609899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ny = data['label']\nX = data.drop('label', axis = 1)\nprint(y.shape)\ny.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:03:22.609448Z","iopub.execute_input":"2024-05-25T04:03:22.609748Z","iopub.status.idle":"2024-05-25T04:03:25.118816Z","shell.execute_reply.started":"2024-05-25T04:03:22.609725Z","shell.execute_reply":"2024-05-25T04:03:25.117908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nX.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:03:27.196228Z","iopub.execute_input":"2024-05-25T04:03:27.196588Z","iopub.status.idle":"2024-05-25T04:03:27.218176Z","shell.execute_reply.started":"2024-05-25T04:03:27.196561Z","shell.execute_reply":"2024-05-25T04:03:27.217336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2, random_state=42)\nprint(type(X_train))\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:10:21.725828Z","iopub.execute_input":"2024-05-25T04:10:21.726535Z","iopub.status.idle":"2024-05-25T04:10:21.945711Z","shell.execute_reply.started":"2024-05-25T04:10:21.726506Z","shell.execute_reply":"2024-05-25T04:10:21.944773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:38:06.578474Z","iopub.execute_input":"2024-05-25T01:38:06.578831Z","iopub.status.idle":"2024-05-25T01:38:06.585670Z","shell.execute_reply.started":"2024-05-25T01:38:06.578803Z","shell.execute_reply":"2024-05-25T01:38:06.584629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_oneHot = pd.get_dummies(y_train, columns = ['label'])\ny_test_oneHot = pd.get_dummies(y_test, columns = ['label'])\nprint(y_train_oneHot.shape)\ny_train_oneHot.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:10:17.057628Z","iopub.execute_input":"2024-05-25T04:10:17.058314Z","iopub.status.idle":"2024-05-25T04:10:17.076979Z","shell.execute_reply.started":"2024-05-25T04:10:17.058283Z","shell.execute_reply":"2024-05-25T04:10:17.076025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(X_train.iloc[0, 400])\nX_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:10:25.465135Z","iopub.execute_input":"2024-05-25T04:10:25.465460Z","iopub.status.idle":"2024-05-25T04:10:25.483346Z","shell.execute_reply.started":"2024-05-25T04:10:25.465434Z","shell.execute_reply":"2024-05-25T04:10:25.482511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.astype(float)\nX_train/=255\n#print(X_train.iloc[0, 400])\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-25T04:10:33.259349Z","iopub.execute_input":"2024-05-25T04:10:33.259945Z","iopub.status.idle":"2024-05-25T04:10:33.397240Z","shell.execute_reply.started":"2024-05-25T04:10:33.259914Z","shell.execute_reply":"2024-05-25T04:10:33.396295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(InputLayer(shape=(784,)))\nmodel.add(Dense(711, activation = 'relu'))\nmodel.add(Dense(513, activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax'))\nmodel.compile(loss='categorical_crossentropy',\n                   optimizer=optimizers.SGD(learning_rate=1e-3, momentum=0.9),\n                   metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:38:09.408472Z","iopub.execute_input":"2024-05-25T01:38:09.409113Z","iopub.status.idle":"2024-05-25T01:38:10.319469Z","shell.execute_reply.started":"2024-05-25T01:38:09.409079Z","shell.execute_reply":"2024-05-25T01:38:10.318582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:38:12.867454Z","iopub.execute_input":"2024-05-25T01:38:12.867862Z","iopub.status.idle":"2024-05-25T01:38:12.875566Z","shell.execute_reply.started":"2024-05-25T01:38:12.867835Z","shell.execute_reply":"2024-05-25T01:38:12.874596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monitor = ModelCheckpoint('./model.keras', monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', save_freq='epoch')\nhistory = model.fit(X_train, y_train_oneHot, epochs = 20, validation_data = (X_test, y_test_oneHot), shuffle = True, callbacks = [monitor])","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:38:13.982600Z","iopub.execute_input":"2024-05-25T01:38:13.982953Z","iopub.status.idle":"2024-05-25T01:39:02.793970Z","shell.execute_reply.started":"2024-05-25T01:38:13.982924Z","shell.execute_reply":"2024-05-25T01:39:02.793207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:49:26.150691Z","iopub.execute_input":"2024-05-25T01:49:26.151091Z","iopub.status.idle":"2024-05-25T01:49:26.155958Z","shell.execute_reply.started":"2024-05-25T01:49:26.151043Z","shell.execute_reply":"2024-05-25T01:49:26.155013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(type(sub))\nprint(sub.shape)\nsub.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-25T02:07:24.760558Z","iopub.execute_input":"2024-05-25T02:07:24.761426Z","iopub.status.idle":"2024-05-25T02:07:26.417407Z","shell.execute_reply.started":"2024-05-25T02:07:24.761392Z","shell.execute_reply":"2024-05-25T02:07:26.416482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(sub)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:54:37.735584Z","iopub.execute_input":"2024-05-25T01:54:37.735864Z","iopub.status.idle":"2024-05-25T01:54:39.555195Z","shell.execute_reply.started":"2024-05-25T01:54:37.735839Z","shell.execute_reply":"2024-05-25T01:54:39.554235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions.shape)\nprint(type(predictions))\nprint(predictions[0])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-25T02:07:45.161557Z","iopub.execute_input":"2024-05-25T02:07:45.161912Z","iopub.status.idle":"2024-05-25T02:07:45.167516Z","shell.execute_reply.started":"2024-05-25T02:07:45.161884Z","shell.execute_reply":"2024-05-25T02:07:45.166552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predArgMax = np.argmax(predictions, axis = 1)\nprint(predArgMax)\nprint(predArgMax.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T01:56:55.859561Z","iopub.execute_input":"2024-05-25T01:56:55.860213Z","iopub.status.idle":"2024-05-25T01:56:55.866318Z","shell.execute_reply.started":"2024-05-25T01:56:55.860181Z","shell.execute_reply":"2024-05-25T01:56:55.865321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageIds = range(1, len(predArgMax) + 1)\noutput = pd.DataFrame({'ImageId': imageIds,\n                       'Label': predArgMax})\nprint(output)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T02:10:12.472248Z","iopub.execute_input":"2024-05-25T02:10:12.472628Z","iopub.status.idle":"2024-05-25T02:10:12.481142Z","shell.execute_reply.started":"2024-05-25T02:10:12.472596Z","shell.execute_reply":"2024-05-25T02:10:12.480143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-25T02:10:16.078732Z","iopub.execute_input":"2024-05-25T02:10:16.079543Z","iopub.status.idle":"2024-05-25T02:10:16.123148Z","shell.execute_reply.started":"2024-05-25T02:10:16.079508Z","shell.execute_reply":"2024-05-25T02:10:16.122218Z"},"trusted":true},"execution_count":null,"outputs":[]}]}