{"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-03-15T05:38:29.729276Z","iopub.execute_input":"2022-03-15T05:38:29.729831Z","iopub.status.idle":"2022-03-15T05:38:29.74125Z","shell.execute_reply.started":"2022-03-15T05:38:29.729787Z","shell.execute_reply":"2022-03-15T05:38:29.739946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First we check and arrange the data to get the training samples.","metadata":{}},{"cell_type":"code","source":"import math\ndata=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndata","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:29.743583Z","iopub.execute_input":"2022-03-15T05:38:29.744418Z","iopub.status.idle":"2022-03-15T05:38:32.561196Z","shell.execute_reply.started":"2022-03-15T05:38:29.744368Z","shell.execute_reply":"2022-03-15T05:38:32.560219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We need to get hold of all the columns that represent the matrix for the labels, and convert it into a 28x28x1 matrix.Because sqrt of 784 is 28 and inorder to apply convnets we need to show the length,width and height of the matrix also color pattern is monochrome(grey scale) so it is a 28x28x1.","metadata":{}},{"cell_type":"code","source":"x=data.iloc[:,1:]\nnumbers=x.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:32.562415Z","iopub.execute_input":"2022-03-15T05:38:32.562695Z","iopub.status.idle":"2022-03-15T05:38:32.567289Z","shell.execute_reply.started":"2022-03-15T05:38:32.56264Z","shell.execute_reply":"2022-03-15T05:38:32.566672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num=np.array([x.reshape(28,28,1) for x in numbers])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:32.568827Z","iopub.execute_input":"2022-03-15T05:38:32.569077Z","iopub.status.idle":"2022-03-15T05:38:32.782947Z","shell.execute_reply.started":"2022-03-15T05:38:32.569047Z","shell.execute_reply":"2022-03-15T05:38:32.781982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.imshow(num[0],cmap='inferno')","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:32.786327Z","iopub.execute_input":"2022-03-15T05:38:32.786724Z","iopub.status.idle":"2022-03-15T05:38:33.004443Z","shell.execute_reply.started":"2022-03-15T05:38:32.786675Z","shell.execute_reply":"2022-03-15T05:38:33.003543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next we import the tensorflow api and keras for constructing the network and train_test_split to get a test set to check accuracy on","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:33.006015Z","iopub.execute_input":"2022-03-15T05:38:33.007257Z","iopub.status.idle":"2022-03-15T05:38:33.012842Z","shell.execute_reply.started":"2022-03-15T05:38:33.007198Z","shell.execute_reply":"2022-03-15T05:38:33.011646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The image matrix contains the value of the intensity of the grey scale we divide these values by 255 which is the maximum posssible value to get the values ranging from(0-1).Lets plot one of the matrices.","metadata":{}},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(num/255,data['label'])\nplt.imshow(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:33.014575Z","iopub.execute_input":"2022-03-15T05:38:33.015441Z","iopub.status.idle":"2022-03-15T05:38:33.451828Z","shell.execute_reply.started":"2022-03-15T05:38:33.015387Z","shell.execute_reply":"2022-03-15T05:38:33.450985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Creating the convolutional network**\nWe set up the first layer with 16 channels of (3,3) convolution matrices. The activation is set to relu.We need to make sure that the number of convolutions at each step is less that the dimension of the original matrix otherwise we will get an error. Next we select the Maxpool layer that only takes the most significant value of the convolutional layer. This process also reduces the dimension of the matrix further.","metadata":{}},{"cell_type":"code","source":"\nmodel=keras.models.Sequential([\n    keras.layers.Conv2D(16,(3,3),activation='relu',input_shape=(28,28,1)),\n    keras.layers.MaxPooling2D(2,2),\n    keras.layers.Conv2D(32,(3,3),activation='relu'),\n    keras.layers.MaxPooling2D(2,2),\n    keras.layers.Flatten(),\n    keras.layers.Dense(200,activation='relu'),\n    keras.layers.Dense(100,activation='relu'),\n    keras.layers.Dense(10,activation='softmax')\n    ])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:33.453404Z","iopub.execute_input":"2022-03-15T05:38:33.453702Z","iopub.status.idle":"2022-03-15T05:38:33.518225Z","shell.execute_reply.started":"2022-03-15T05:38:33.453669Z","shell.execute_reply":"2022-03-15T05:38:33.517245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A good way to track the matrix that gets passed in each step is to get the model.summary()","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:33.520846Z","iopub.execute_input":"2022-03-15T05:38:33.521571Z","iopub.status.idle":"2022-03-15T05:38:33.530294Z","shell.execute_reply.started":"2022-03-15T05:38:33.521519Z","shell.execute_reply":"2022-03-15T05:38:33.529652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Next we complie the model using the classifier for the loss","metadata":{}},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy',\n             optimizer='sgd',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:38:33.531306Z","iopub.execute_input":"2022-03-15T05:38:33.531558Z","iopub.status.idle":"2022-03-15T05:38:33.546445Z","shell.execute_reply.started":"2022-03-15T05:38:33.531526Z","shell.execute_reply":"2022-03-15T05:38:33.545753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Time to run the model and check the accuracy !","metadata":{}},{"cell_type":"code","source":"model.fit(num/255,data['label'],epochs=20)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:53:30.724995Z","iopub.execute_input":"2022-03-15T05:53:30.725375Z","iopub.status.idle":"2022-03-15T05:56:56.906055Z","shell.execute_reply.started":"2022-03-15T05:53:30.725338Z","shell.execute_reply":"2022-03-15T05:56:56.905361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nnumbers1=test.to_numpy()\n\nlisted=[x.reshape(28,28,1) for x in numbers1]\nnumtes=np.array(listed)\nnumtest=np.array([x/255 for x in numtes])\nhist=model.predict(numtest)\nnp.argmax(hist[99])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:57:00.943449Z","iopub.execute_input":"2022-03-15T05:57:00.944044Z","iopub.status.idle":"2022-03-15T05:57:05.30901Z","shell.execute_reply.started":"2022-03-15T05:57:00.944003Z","shell.execute_reply":"2022-03-15T05:57:05.308073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.imshow(numtes[99])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:57:09.538107Z","iopub.execute_input":"2022-03-15T05:57:09.538445Z","iopub.status.idle":"2022-03-15T05:57:09.706569Z","shell.execute_reply.started":"2022-03-15T05:57:09.53841Z","shell.execute_reply":"2022-03-15T05:57:09.705581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test\nsample=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nImageId=np.arange(1,28001)\npredict=[np.argmax(x) for x in hist]\nnew=dict(zip(['ImageId','Label'],[ImageId,predict]))\nnew_df=pd.DataFrame(new).set_index('ImageId')\nnew_df","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:57:14.368062Z","iopub.execute_input":"2022-03-15T05:57:14.368399Z","iopub.status.idle":"2022-03-15T05:57:14.494819Z","shell.execute_reply.started":"2022-03-15T05:57:14.368362Z","shell.execute_reply":"2022-03-15T05:57:14.493906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import the modules we'll need\nfrom IPython.display import HTML\nimport pandas as pd\nimport numpy as np\nimport base64\n\n# function that takes in a dataframe and creates a text link to  \n# download it (will only work for files < 2MB or so)\ndef create_download_link(df, title = \"Download CSV file\", filename = \"data.csv\"):  \n    csv = df.to_csv()\n    b64 = base64.b64encode(csv.encode())\n    payload = b64.decode()\n    html = '<a download=\"{filename}\" href=\"data:text/csv;base64,{payload}\" target=\"_blank\">{title}</a>'\n    html = html.format(payload=payload,title=title,filename=filename)\n    return HTML(html)\n\n# create a random sample dataframe\n\n# create a link to download the dataframe\ncreate_download_link(new_df)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T05:57:20.782092Z","iopub.execute_input":"2022-03-15T05:57:20.782809Z","iopub.status.idle":"2022-03-15T05:57:20.854159Z","shell.execute_reply.started":"2022-03-15T05:57:20.78277Z","shell.execute_reply":"2022-03-15T05:57:20.85331Z"},"trusted":true},"execution_count":null,"outputs":[]}]}