{"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":"## Overview  \nHere we have a notebook that I made for the Digit Recognizer competition. Here we go through the process of:  \n<ul>\n    <li> Loading in the data </li>\n    <li> Normalize and Shape the data (including the labels) </li>\n    <li> Create and then run the model </li>\n</ul>","metadata":{}},{"cell_type":"markdown","source":"**Please note this is my first notebook so any comments or suggestions on how to make either the model look better or my notebook overall be better presentable woudl be greatly appreciated. Thank you so much for taking the time and looking at it!**","metadata":{}},{"cell_type":"code","source":"import 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 matplotlib.image as mpimg\n%matplotlib inline\nimport seaborn as sns\n\n\n# This is for our model\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout,Flatten\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-15T20:48:17.923343Z","iopub.execute_input":"2021-07-15T20:48:17.923763Z","iopub.status.idle":"2021-07-15T20:48:24.638154Z","shell.execute_reply.started":"2021-07-15T20:48:17.923683Z","shell.execute_reply":"2021-07-15T20:48:24.637107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:32.911167Z","iopub.execute_input":"2021-07-15T20:48:32.911517Z","iopub.status.idle":"2021-07-15T20:48:39.032274Z","shell.execute_reply.started":"2021-07-15T20:48:32.911473Z","shell.execute_reply":"2021-07-15T20:48:39.031282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.isnull().any().value_counts())\nprint(test.isnull().any().value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:40.889746Z","iopub.execute_input":"2021-07-15T20:48:40.890087Z","iopub.status.idle":"2021-07-15T20:48:40.921559Z","shell.execute_reply.started":"2021-07-15T20:48:40.890058Z","shell.execute_reply":"2021-07-15T20:48:40.920563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Note that there is one extra for the train dataset because of the label column","metadata":{}},{"cell_type":"markdown","source":"##### Lets get our X_train and y_train","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:01:53.184217Z","iopub.execute_input":"2021-07-04T09:01:53.184552Z","iopub.status.idle":"2021-07-04T09:01:53.22343Z","shell.execute_reply.started":"2021-07-04T09:01:53.184523Z","shell.execute_reply":"2021-07-04T09:01:53.221598Z"}}},{"cell_type":"code","source":"y_train = train[\"label\"]\nX_train = train.drop(labels = [\"label\"],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:42.929034Z","iopub.execute_input":"2021-07-15T20:48:42.929407Z","iopub.status.idle":"2021-07-15T20:48:43.055011Z","shell.execute_reply.started":"2021-07-15T20:48:42.929376Z","shell.execute_reply":"2021-07-15T20:48:43.053818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.value_counts())\nsns.countplot(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:43.868232Z","iopub.execute_input":"2021-07-15T20:48:43.868602Z","iopub.status.idle":"2021-07-15T20:48:44.063905Z","shell.execute_reply.started":"2021-07-15T20:48:43.868570Z","shell.execute_reply":"2021-07-15T20:48:44.063205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### By the looks of our classification, we have 10 different classifications that are fairly balanced.","metadata":{}},{"cell_type":"markdown","source":"##### Normalize and reshape to make it work better with keras. because the values range from 0-255, making them range from 0-1 will make it easier for our model to run. So simply diving the values by 255 should do the trick.   \n##### It is also necessary to reshape the data to add a channel vector of one on the end for keras to work.","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest = test / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:46.056263Z","iopub.execute_input":"2021-07-15T20:48:46.056758Z","iopub.status.idle":"2021-07-15T20:48:46.343988Z","shell.execute_reply.started":"2021-07-15T20:48:46.056727Z","shell.execute_reply":"2021-07-15T20:48:46.343051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:46.438366Z","iopub.execute_input":"2021-07-15T20:48:46.438712Z","iopub.status.idle":"2021-07-15T20:48:46.443668Z","shell.execute_reply.started":"2021-07-15T20:48:46.438682Z","shell.execute_reply":"2021-07-15T20:48:46.442620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[3])\nprint(\"The number should look like\", y_train.iloc[3])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:46.986153Z","iopub.execute_input":"2021-07-15T20:48:46.986642Z","iopub.status.idle":"2021-07-15T20:48:47.114647Z","shell.execute_reply.started":"2021-07-15T20:48:46.986613Z","shell.execute_reply":"2021-07-15T20:48:47.113724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Also need to normalize and prepare our y variable as well by getting dummies and turning our one label into a categorical array","metadata":{}},{"cell_type":"code","source":"print(y_train)\ny_train = pd.get_dummies(y_train)\nprint(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:48.788439Z","iopub.execute_input":"2021-07-15T20:48:48.788836Z","iopub.status.idle":"2021-07-15T20:48:48.803749Z","shell.execute_reply.started":"2021-07-15T20:48:48.788803Z","shell.execute_reply":"2021-07-15T20:48:48.802439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[3])\nprint(\"The number should look like\", y_train.iloc[3])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:49.579082Z","iopub.execute_input":"2021-07-15T20:48:49.579442Z","iopub.status.idle":"2021-07-15T20:48:49.716139Z","shell.execute_reply.started":"2021-07-15T20:48:49.579413Z","shell.execute_reply":"2021-07-15T20:48:49.715406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Now to create our model","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:51.125533Z","iopub.execute_input":"2021-07-15T20:48:51.126133Z","iopub.status.idle":"2021-07-15T20:48:51.525250Z","shell.execute_reply.started":"2021-07-15T20:48:51.126099Z","shell.execute_reply":"2021-07-15T20:48:51.524569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = Sequential()\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(64, activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(32, activation = 'relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(10, activation = 'softmax'))\n\nmodel.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:52.424369Z","iopub.execute_input":"2021-07-15T20:48:52.424987Z","iopub.status.idle":"2021-07-15T20:48:52.485067Z","shell.execute_reply.started":"2021-07-15T20:48:52.424953Z","shell.execute_reply":"2021-07-15T20:48:52.484129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train, epochs = 25, validation_data = (X_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:48:54.706374Z","iopub.execute_input":"2021-07-15T20:48:54.706758Z","iopub.status.idle":"2021-07-15T20:50:10.747471Z","shell.execute_reply.started":"2021-07-15T20:48:54.706725Z","shell.execute_reply":"2021-07-15T20:50:10.746583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses = pd.DataFrame(model.history.history)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:17.676929Z","iopub.execute_input":"2021-07-15T20:50:17.677597Z","iopub.status.idle":"2021-07-15T20:50:17.684057Z","shell.execute_reply.started":"2021-07-15T20:50:17.677552Z","shell.execute_reply":"2021-07-15T20:50:17.682879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses.plot()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:18.406673Z","iopub.execute_input":"2021-07-15T20:50:18.407051Z","iopub.status.idle":"2021-07-15T20:50:18.604536Z","shell.execute_reply.started":"2021-07-15T20:50:18.407021Z","shell.execute_reply":"2021-07-15T20:50:18.603563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Does not look like overfitting has occured. in fact we can maybe add more layers and change some parameters to make this even better","metadata":{}},{"cell_type":"code","source":"predictions = model.predict_classes(test)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:22.374964Z","iopub.execute_input":"2021-07-15T20:50:22.375307Z","iopub.status.idle":"2021-07-15T20:50:23.531762Z","shell.execute_reply.started":"2021-07-15T20:50:22.375278Z","shell.execute_reply":"2021-07-15T20:50:23.530803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:23.579840Z","iopub.execute_input":"2021-07-15T20:50:23.580175Z","iopub.status.idle":"2021-07-15T20:50:23.586029Z","shell.execute_reply.started":"2021-07-15T20:50:23.580146Z","shell.execute_reply":"2021-07-15T20:50:23.585052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Looks good so lets export it into a csv","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(predictions, columns=['Label'])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:25.577278Z","iopub.execute_input":"2021-07-15T20:50:25.577666Z","iopub.status.idle":"2021-07-15T20:50:25.582338Z","shell.execute_reply.started":"2021-07-15T20:50:25.577632Z","shell.execute_reply":"2021-07-15T20:50:25.581433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(r'./submission.csv',index_label = 'ImageId')","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:26.120261Z","iopub.execute_input":"2021-07-15T20:50:26.120643Z","iopub.status.idle":"2021-07-15T20:50:26.187283Z","shell.execute_reply.started":"2021-07-15T20:50:26.120611Z","shell.execute_reply":"2021-07-15T20:50:26.186393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('./submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-15T20:50:27.140407Z","iopub.execute_input":"2021-07-15T20:50:27.141031Z","iopub.status.idle":"2021-07-15T20:50:27.158749Z","shell.execute_reply.started":"2021-07-15T20:50:27.140995Z","shell.execute_reply":"2021-07-15T20:50:27.157601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}