{"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":"markdown","source":"# 93% MNIST Digit Recognizer\n\nJust a simple Neural Network that I used on this dataset while I was learning Machine Learning on Coursera.","metadata":{}},{"cell_type":"code","source":"#Library Imports\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Activation\nfrom tensorflow.keras import Sequential","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T21:32:35.916550Z","iopub.execute_input":"2022-07-25T21:32:35.917627Z","iopub.status.idle":"2022-07-25T21:32:35.924685Z","shell.execute_reply.started":"2022-07-25T21:32:35.917586Z","shell.execute_reply":"2022-07-25T21:32:35.923221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing Training Data\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:32:35.956606Z","iopub.execute_input":"2022-07-25T21:32:35.957093Z","iopub.status.idle":"2022-07-25T21:32:38.837949Z","shell.execute_reply.started":"2022-07-25T21:32:35.957049Z","shell.execute_reply":"2022-07-25T21:32:38.836563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TrainY data\ntrainY = train[\"label\"].to_numpy()\ntrainY.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:32:38.840364Z","iopub.execute_input":"2022-07-25T21:32:38.841465Z","iopub.status.idle":"2022-07-25T21:32:38.850613Z","shell.execute_reply.started":"2022-07-25T21:32:38.841411Z","shell.execute_reply":"2022-07-25T21:32:38.849091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TrainX data\ntrainX = train.drop([\"label\"], axis=1).to_numpy()/255\ntrainX = trainX.reshape(-1, 28, 28, 1)\n\ntrainX.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:32:38.852063Z","iopub.execute_input":"2022-07-25T21:32:38.852496Z","iopub.status.idle":"2022-07-25T21:32:39.105742Z","shell.execute_reply.started":"2022-07-25T21:32:38.852461Z","shell.execute_reply":"2022-07-25T21:32:39.104491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create, compile, and train model\nmodel = Sequential([\n        Conv2D(32, (2, 2), activation='relu', input_shape=trainX.shape[1:]),\n        Activation(\"relu\"),\n        MaxPooling2D(pool_size=(2, 2)),\n        Conv2D(32, (4, 4), activation='relu'),\n        MaxPooling2D(pool_size=(2, 2)),\n        Flatten(),\n        Dense(128, activation='relu'),\n        Dense(10, activation='linear')\n])\nmodel.summary()\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])\n\nhistory = model.fit(\n    trainX,trainY,\n    epochs=15\n)\n\nhistory","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:32:39.108198Z","iopub.execute_input":"2022-07-25T21:32:39.108575Z","iopub.status.idle":"2022-07-25T21:36:32.097949Z","shell.execute_reply.started":"2022-07-25T21:32:39.108542Z","shell.execute_reply":"2022-07-25T21:36:32.097102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:36:32.099343Z","iopub.execute_input":"2022-07-25T21:36:32.099910Z","iopub.status.idle":"2022-07-25T21:36:32.240541Z","shell.execute_reply.started":"2022-07-25T21:36:32.099875Z","shell.execute_reply":"2022-07-25T21:36:32.238931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import testX data\ntestX = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\").to_numpy()/255\ntestX = testX.reshape(-1, 28, 28, 1)\n\ntestX.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:36:32.243284Z","iopub.execute_input":"2022-07-25T21:36:32.243805Z","iopub.status.idle":"2022-07-25T21:36:34.199773Z","shell.execute_reply.started":"2022-07-25T21:36:32.243724Z","shell.execute_reply":"2022-07-25T21:36:34.198540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create predictions\nm = testX.shape\n\ntestY = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\npredictions = model.predict(testX)\ntestY","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:36:34.201043Z","iopub.execute_input":"2022-07-25T21:36:34.201400Z","iopub.status.idle":"2022-07-25T21:36:37.157059Z","shell.execute_reply.started":"2022-07-25T21:36:34.201369Z","shell.execute_reply":"2022-07-25T21:36:37.156265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Conversion for submission\nfor i in range(m[0]):\n    testY[\"Label\"][i] = np.argmax(predictions[i])\n\ntestY.to_csv(\"submission.csv\", index=False)\ntestY","metadata":{"execution":{"iopub.status.busy":"2022-07-25T21:38:49.969468Z","iopub.execute_input":"2022-07-25T21:38:49.971177Z","iopub.status.idle":"2022-07-25T21:38:51.677783Z","shell.execute_reply.started":"2022-07-25T21:38:49.971135Z","shell.execute_reply":"2022-07-25T21:38:51.676436Z"},"trusted":true},"execution_count":null,"outputs":[]}]}