{"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":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\n%matplotlib inline\ntf.get_logger().setLevel('ERROR')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-04T01:15:52.796631Z","iopub.execute_input":"2022-07-04T01:15:52.796974Z","iopub.status.idle":"2022-07-04T01:15:52.814290Z","shell.execute_reply.started":"2022-07-04T01:15:52.796944Z","shell.execute_reply":"2022-07-04T01:15:52.813295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the data\n","metadata":{}},{"cell_type":"code","source":"Data = pd.read_csv(r\"/kaggle/input/digit-recognizer/train.csv\")\ntest_Data = pd.read_csv(r\"/kaggle/input/digit-recognizer/test.csv\")\n\nData.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:52.826667Z","iopub.execute_input":"2022-07-04T01:15:52.826914Z","iopub.status.idle":"2022-07-04T01:15:56.747854Z","shell.execute_reply.started":"2022-07-04T01:15:52.826891Z","shell.execute_reply":"2022-07-04T01:15:56.746844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:56.749895Z","iopub.execute_input":"2022-07-04T01:15:56.750286Z","iopub.status.idle":"2022-07-04T01:15:56.793028Z","shell.execute_reply.started":"2022-07-04T01:15:56.750246Z","shell.execute_reply":"2022-07-04T01:15:56.792086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:56.794306Z","iopub.execute_input":"2022-07-04T01:15:56.795163Z","iopub.status.idle":"2022-07-04T01:15:58.948765Z","shell.execute_reply.started":"2022-07-04T01:15:56.795112Z","shell.execute_reply":"2022-07-04T01:15:58.947727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Viewing an Image\n#### We take sample in row 11 and It's Label = 9","metadata":{}},{"cell_type":"code","source":"print('Label = ',Data.iloc[11,0])\nSample=(Data.iloc[11,1:])\nSample=list(Sample)\nSample= np.reshape(Sample,(28,28))\nplt.imshow(Sample)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:58.951284Z","iopub.execute_input":"2022-07-04T01:15:58.951722Z","iopub.status.idle":"2022-07-04T01:15:59.115965Z","shell.execute_reply.started":"2022-07-04T01:15:58.951684Z","shell.execute_reply":"2022-07-04T01:15:59.115007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### View Pixel Values in The Sample in row 11 \n##### Show The Scale From (0:255) in this sample zero value Duplicated 649 ...etc","metadata":{}},{"cell_type":"code","source":"plt.hist(Data.iloc[11,1:])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:59.117430Z","iopub.execute_input":"2022-07-04T01:15:59.117791Z","iopub.status.idle":"2022-07-04T01:15:59.287982Z","shell.execute_reply.started":"2022-07-04T01:15:59.117755Z","shell.execute_reply":"2022-07-04T01:15:59.287032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Separation","metadata":{}},{"cell_type":"code","source":"X_train=(Data.iloc[:,1:])\ny_train=(Data.iloc[:,0])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:59.289414Z","iopub.execute_input":"2022-07-04T01:15:59.289742Z","iopub.status.idle":"2022-07-04T01:15:59.295460Z","shell.execute_reply.started":"2022-07-04T01:15:59.289707Z","shell.execute_reply":"2022-07-04T01:15:59.294541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### you Can Scaling input Training and Test Data But Here I Didn't because the maximum number is 255 in all Data so we\n\n#### know the maximum so we don't need to Scaling if you Want Just remove Comment ","metadata":{}},{"cell_type":"code","source":"#X_train = tf.keras.utils.normalize(X_train, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:59.297001Z","iopub.execute_input":"2022-07-04T01:15:59.297635Z","iopub.status.idle":"2022-07-04T01:15:59.304266Z","shell.execute_reply.started":"2022-07-04T01:15:59.297598Z","shell.execute_reply":"2022-07-04T01:15:59.303370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Doing CNN with 3 Hidden Layers \n#### use softmax as it use in Multi-Classification  and use softplus because constrain the output of a machine to always be positive and we want It","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(512, activation=tf.nn.softplus))\nmodel.add(tf.keras.layers.Dense(256, activation=tf.nn.softplus)) \nmodel.add(tf.keras.layers.Dense(128, activation=tf.nn.softplus))\nmodel.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\nmodel.fit(X_train, y_train, epochs=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:15:59.305748Z","iopub.execute_input":"2022-07-04T01:15:59.306091Z","iopub.status.idle":"2022-07-04T01:17:22.141619Z","shell.execute_reply.started":"2022-07-04T01:15:59.306054Z","shell.execute_reply":"2022-07-04T01:17:22.140629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test The Prediction Of Training Data_Set","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_train)\npredictions = np.argmax(predictions, axis = 1) \nprint('Prediction = ',predictions[11])\nplt.imshow(Sample)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:17:22.143285Z","iopub.execute_input":"2022-07-04T01:17:22.143676Z","iopub.status.idle":"2022-07-04T01:17:25.502753Z","shell.execute_reply.started":"2022-07-04T01:17:22.143639Z","shell.execute_reply":"2022-07-04T01:17:25.501494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict Test Data","metadata":{}},{"cell_type":"code","source":"pred=model.predict(test_Data)\npred = np.argmax(pred, axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:17:25.506786Z","iopub.execute_input":"2022-07-04T01:17:25.507088Z","iopub.status.idle":"2022-07-04T01:17:27.240421Z","shell.execute_reply.started":"2022-07-04T01:17:25.507063Z","shell.execute_reply":"2022-07-04T01:17:27.239377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ndf = pd.DataFrame({'ImageId':range(1,len(pred)+1),'Label':pred})\ndf.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T01:17:38.796188Z","iopub.execute_input":"2022-07-04T01:17:38.796753Z","iopub.status.idle":"2022-07-04T01:17:38.848848Z","shell.execute_reply.started":"2022-07-04T01:17:38.796718Z","shell.execute_reply":"2022-07-04T01:17:38.847998Z"},"trusted":true},"execution_count":null,"outputs":[]}]}