{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2022-09-23T23:42:08.715029Z","iopub.execute_input":"2022-09-23T23:42:08.715616Z","iopub.status.idle":"2022-09-23T23:42:08.744649Z","shell.execute_reply.started":"2022-09-23T23:42:08.715538Z","shell.execute_reply":"2022-09-23T23:42:08.743552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Much of code adapted from \"https://www.kaggle.com/code/raibabar/digit-recognizer-with-simple-cnn-model/notebook\"\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-09-23T23:42:27.041057Z","iopub.execute_input":"2022-09-23T23:42:27.041417Z","iopub.status.idle":"2022-09-23T23:42:33.088749Z","shell.execute_reply.started":"2022-09-23T23:42:27.041392Z","shell.execute_reply":"2022-09-23T23:42:33.087771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n\ny_train = df_train['label']\ny_train = np.array(y_train)\ny_train = y_train.reshape(y_train.shape[0], 1)\nX_train = df_train.drop(['label'], axis=1)\nX_train = np.array(X_train)\nX_train = X_train.reshape(X_train.shape[0], 28, 28)\n\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n# df_test.head()\n\nX_test = np.array(df_test)\nX_test = X_test.reshape(X_test.shape[0], 28, 28)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:03:07.804660Z","iopub.execute_input":"2022-09-24T00:03:07.805023Z","iopub.status.idle":"2022-09-24T00:03:10.389917Z","shell.execute_reply.started":"2022-09-24T00:03:07.804993Z","shell.execute_reply":"2022-09-24T00:03:10.388871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:03:21.769171Z","iopub.execute_input":"2022-09-24T00:03:21.769515Z","iopub.status.idle":"2022-09-24T00:03:21.774365Z","shell.execute_reply.started":"2022-09-24T00:03:21.769488Z","shell.execute_reply":"2022-09-24T00:03:21.773392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Flatten\nmodel = Sequential()\ninput_layers = Flatten(input_shape=(28,28))\nmodel.add(input_layers)\nhidden1 = Dense(784,activation='relu')\nmodel.add(hidden1)\nhidden2 = Dense(512,activation='relu')\nmodel.add(hidden1)\noutput = Dense(10,activation='softmax')\nmodel.add(output)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:03:50.300589Z","iopub.execute_input":"2022-09-24T00:03:50.300940Z","iopub.status.idle":"2022-09-24T00:03:50.864651Z","shell.execute_reply.started":"2022-09-24T00:03:50.300913Z","shell.execute_reply":"2022-09-24T00:03:50.863520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:04:17.210730Z","iopub.execute_input":"2022-09-24T00:04:17.211183Z","iopub.status.idle":"2022-09-24T00:04:17.223717Z","shell.execute_reply.started":"2022-09-24T00:04:17.211153Z","shell.execute_reply":"2022-09-24T00:04:17.222979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train,epochs=20,verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:04:19.180536Z","iopub.execute_input":"2022-09-24T00:04:19.180882Z","iopub.status.idle":"2022-09-24T00:06:41.722479Z","shell.execute_reply.started":"2022-09-24T00:04:19.180854Z","shell.execute_reply":"2022-09-24T00:06:41.721586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(X_test)\nresults = np.argmax(results, axis = 1)\n\nsubmission = pd.read_csv('../input/digit-recognizer/sample_submission.csv', index_col = 'ImageId')\nsubmission.Label = results\nsubmission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:23:11.147918Z","iopub.execute_input":"2022-09-24T00:23:11.148525Z","iopub.status.idle":"2022-09-24T00:23:12.815239Z","shell.execute_reply.started":"2022-09-24T00:23:11.148491Z","shell.execute_reply":"2022-09-24T00:23:12.814531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train2, X_test2, y_train2, y_test2 = train_test_split(X_train, y_train, shuffle=True, stratify=y_train, test_size=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:27:53.351528Z","iopub.execute_input":"2022-09-24T00:27:53.351956Z","iopub.status.idle":"2022-09-24T00:27:54.007086Z","shell.execute_reply.started":"2022-09-24T00:27:53.351921Z","shell.execute_reply":"2022-09-24T00:27:54.005762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train2.shape)\nprint(X_test2.shape)\nprint(y_train2.shape)\nprint(y_test2.shape)\n\n# Merging code from https://www.kaggle.com/code/nicholasfrankenberg/mnist-notebook/\n\ntrain2 = pd.DataFrame(X_train2.reshape(X_train2.shape[0], 28 * 28))\ntrain2.insert(0, 'labels', y_train2)\ntrain2.to_csv('train_halfImages.csv')\n\ntest2 = pd.DataFrame(X_test2.reshape(X_test2.shape[0], 28 * 28))\ntest2.insert(0, 'labels', y_test2)\ntest2.to_csv('test_halfImages.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-24T01:03:12.682221Z","iopub.execute_input":"2022-09-24T01:03:12.682599Z","iopub.status.idle":"2022-09-24T01:03:17.440549Z","shell.execute_reply.started":"2022-09-24T01:03:12.682570Z","shell.execute_reply":"2022-09-24T01:03:17.439569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train2,y_train2,epochs=20,verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:28:55.221406Z","iopub.execute_input":"2022-09-24T00:28:55.221792Z","iopub.status.idle":"2022-09-24T00:30:15.484200Z","shell.execute_reply.started":"2022-09-24T00:28:55.221759Z","shell.execute_reply":"2022-09-24T00:30:15.483442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_acuracy = model.evaluate(X_test2,y_test2,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:32:37.977247Z","iopub.execute_input":"2022-09-24T00:32:37.977576Z","iopub.status.idle":"2022-09-24T00:32:39.680989Z","shell.execute_reply.started":"2022-09-24T00:32:37.977551Z","shell.execute_reply":"2022-09-24T00:32:39.679935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train3 = df_train[df_train.label < 5]\n\ndf_train3.to_csv(\"train_halfDigits.csv\")\n\nX_train3 = df_train3.drop(['label'], axis=1)\ny_train3 = df_train3.label\n\n\n\ndf_test3 = df_train[df_train.label > 4]\n\ndf_train3.to_csv(\"test_halfDigits.csv\")\n\nX_test3 = df_test3.drop(['label'], axis=1)\ny_test3 = df_test3.label","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:57:21.681852Z","iopub.execute_input":"2022-09-24T00:57:21.682248Z","iopub.status.idle":"2022-09-24T00:57:26.678072Z","shell.execute_reply.started":"2022-09-24T00:57:21.682221Z","shell.execute_reply":"2022-09-24T00:57:26.677185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train3,y_train3,epochs=20,verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:51:07.986574Z","iopub.execute_input":"2022-09-24T00:51:07.986943Z","iopub.status.idle":"2022-09-24T00:52:16.494071Z","shell.execute_reply.started":"2022-09-24T00:51:07.986913Z","shell.execute_reply":"2022-09-24T00:52:16.493382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_acuracy = model.evaluate(X_test3,y_test3,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T00:53:29.139289Z","iopub.execute_input":"2022-09-24T00:53:29.140388Z","iopub.status.idle":"2022-09-24T00:53:30.763057Z","shell.execute_reply.started":"2022-09-24T00:53:29.140348Z","shell.execute_reply":"2022-09-24T00:53:30.762193Z"},"trusted":true},"execution_count":null,"outputs":[]}]}