{"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":"Libraries","metadata":{"id":"5ORseoINPPGG"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.layers import InputLayer , Dense , Flatten","metadata":{"id":"zyKP2HgoO9z_","execution":{"iopub.status.busy":"2023-06-29T09:32:06.261460Z","iopub.execute_input":"2023-06-29T09:32:06.261845Z","iopub.status.idle":"2023-06-29T09:32:15.549990Z","shell.execute_reply.started":"2023-06-29T09:32:06.261813Z","shell.execute_reply":"2023-06-29T09:32:15.548593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read data","metadata":{"id":"7WC5oLxZRnKk"}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"id":"HJrvm-2UPbkD","execution":{"iopub.status.busy":"2023-06-29T09:32:15.552171Z","iopub.execute_input":"2023-06-29T09:32:15.552855Z","iopub.status.idle":"2023-06-29T09:32:21.028213Z","shell.execute_reply.started":"2023-06-29T09:32:15.552822Z","shell.execute_reply":"2023-06-29T09:32:21.027148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Explor Data","metadata":{"id":"XTLzz5OUSIno"}},{"cell_type":"code","source":"train_data.head()","metadata":{"id":"sx2y0oMIRlht","outputId":"00046a39-e3f1-4e18-a9d2-444f2a918653","execution":{"iopub.status.busy":"2023-06-29T09:32:21.029579Z","iopub.execute_input":"2023-06-29T09:32:21.029953Z","iopub.status.idle":"2023-06-29T09:32:21.071359Z","shell.execute_reply.started":"2023-06-29T09:32:21.029923Z","shell.execute_reply":"2023-06-29T09:32:21.070115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_images = train_data.drop('label',axis = 1 ,inplace = False)","metadata":{"id":"dnA1lY-2RlWk","execution":{"iopub.status.busy":"2023-06-29T09:32:38.244195Z","iopub.execute_input":"2023-06-29T09:32:38.244719Z","iopub.status.idle":"2023-06-29T09:32:38.369735Z","shell.execute_reply.started":"2023-06-29T09:32:38.244672Z","shell.execute_reply":"2023-06-29T09:32:38.368611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_labels = train_data['label']","metadata":{"id":"boKxWsSERlTb","execution":{"iopub.status.busy":"2023-06-29T09:32:38.777558Z","iopub.execute_input":"2023-06-29T09:32:38.778948Z","iopub.status.idle":"2023-06-29T09:32:38.784632Z","shell.execute_reply.started":"2023-06-29T09:32:38.778907Z","shell.execute_reply":"2023-06-29T09:32:38.782540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_images.shape","metadata":{"id":"_8A_OFZxRlQg","outputId":"351c5297-7811-44b1-b078-e147128016ef","execution":{"iopub.status.busy":"2023-06-29T09:32:42.026166Z","iopub.execute_input":"2023-06-29T09:32:42.027379Z","iopub.status.idle":"2023-06-29T09:32:42.034889Z","shell.execute_reply.started":"2023-06-29T09:32:42.027315Z","shell.execute_reply":"2023-06-29T09:32:42.033633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_images = test_data","metadata":{"id":"RslCJMdcRlKu","execution":{"iopub.status.busy":"2023-06-29T09:32:43.706127Z","iopub.execute_input":"2023-06-29T09:32:43.706839Z","iopub.status.idle":"2023-06-29T09:32:43.713143Z","shell.execute_reply.started":"2023-06-29T09:32:43.706802Z","shell.execute_reply":"2023-06-29T09:32:43.711285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_images.shape","metadata":{"id":"nz9zPg6qfRKm","outputId":"2c315fac-e7a4-4829-c31e-41fd51ef80cf","execution":{"iopub.status.busy":"2023-06-29T09:32:47.079004Z","iopub.execute_input":"2023-06-29T09:32:47.080362Z","iopub.status.idle":"2023-06-29T09:32:47.087817Z","shell.execute_reply.started":"2023-06-29T09:32:47.080278Z","shell.execute_reply":"2023-06-29T09:32:47.086935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_labels.value_counts()","metadata":{"id":"aI-VvC3UTlho","outputId":"7aba5292-e4ed-4fac-c8b6-e1fed5289721","execution":{"iopub.status.busy":"2023-06-29T09:32:48.697638Z","iopub.execute_input":"2023-06-29T09:32:48.698242Z","iopub.status.idle":"2023-06-29T09:32:48.712320Z","shell.execute_reply.started":"2023-06-29T09:32:48.698211Z","shell.execute_reply":"2023-06-29T09:32:48.710880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Label:',y_train_labels[0])\nplt.imshow(np.array(X_train_images.iloc[0]).reshape(28,28),cmap = 'gray')","metadata":{"id":"gN1zsRC9Uskh","outputId":"a714e495-8379-408a-bac3-10a975038003","execution":{"iopub.status.busy":"2023-06-29T09:32:50.107176Z","iopub.execute_input":"2023-06-29T09:32:50.109542Z","iopub.status.idle":"2023-06-29T09:32:50.401569Z","shell.execute_reply.started":"2023-06-29T09:32:50.109487Z","shell.execute_reply":"2023-06-29T09:32:50.399972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Build and Train","metadata":{"id":"FS4Li_IZde6o"}},{"cell_type":"code","source":"# Define the model architecture\nmodel = tf.keras.Sequential([\n  Flatten(), # input\n  Dense(128, activation='relu'),# 1 hidden layer with 128 neurons\n  Dense(10,activation = 'softmax') # output after 10 epoch\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', # using adam \n              loss=tf.keras.losses.SparseCategoricalCrossentropy(), # sparse categorical loss\n              metrics=['accuracy'])\n\n# Train the model\ntrain = model.fit(X_train_images, y_train_labels, epochs= 50)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:55:01.784231Z","iopub.execute_input":"2023-06-29T09:55:01.784599Z","iopub.status.idle":"2023-06-29T09:59:24.719497Z","shell.execute_reply.started":"2023-06-29T09:55:01.784572Z","shell.execute_reply":"2023-06-29T09:59:24.718159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(train.history['loss'])","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:59:24.721866Z","iopub.execute_input":"2023-06-29T09:59:24.722356Z","iopub.status.idle":"2023-06-29T09:59:25.015927Z","shell.execute_reply.started":"2023-06-29T09:59:24.722310Z","shell.execute_reply":"2023-06-29T09:59:25.014729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test_images)","metadata":{"id":"wYcPo82sTx3Z","outputId":"6c57a4fb-b6e7-4a70-f6a0-a2011c1ce306","execution":{"iopub.status.busy":"2023-06-29T09:52:53.822637Z","iopub.execute_input":"2023-06-29T09:52:53.822968Z","iopub.status.idle":"2023-06-29T09:52:55.841263Z","shell.execute_reply.started":"2023-06-29T09:52:53.822940Z","shell.execute_reply":"2023-06-29T09:52:55.840129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-29T10:27:04.497237Z","iopub.execute_input":"2023-06-29T10:27:04.497614Z","iopub.status.idle":"2023-06-29T10:27:04.504961Z","shell.execute_reply.started":"2023-06-29T10:27:04.497587Z","shell.execute_reply":"2023-06-29T10:27:04.503684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=np.argmax(predictions,axis=1)","metadata":{"id":"2AEDrjsybwag","outputId":"05ec8d76-e128-49d2-c510-f2367679209d","execution":{"iopub.status.busy":"2023-06-29T10:26:40.559343Z","iopub.execute_input":"2023-06-29T10:26:40.559794Z","iopub.status.idle":"2023-06-29T10:26:40.565803Z","shell.execute_reply.started":"2023-06-29T10:26:40.559760Z","shell.execute_reply":"2023-06-29T10:26:40.564729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-29T10:26:49.679898Z","iopub.execute_input":"2023-06-29T10:26:49.680605Z","iopub.status.idle":"2023-06-29T10:26:49.687330Z","shell.execute_reply.started":"2023-06-29T10:26:49.680573Z","shell.execute_reply":"2023-06-29T10:26:49.686214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 10\nprint(y_pred[i]) \nplt.imshow(X_test_images.iloc[i].to_numpy().reshape(28,28),cmap = 'gray')","metadata":{"id":"MNLzMAJGRk_T","outputId":"35b950e6-12fe-4a94-a0a5-096ebac923dc","execution":{"iopub.status.busy":"2023-06-29T10:28:57.353056Z","iopub.execute_input":"2023-06-29T10:28:57.353453Z","iopub.status.idle":"2023-06-29T10:28:57.637049Z","shell.execute_reply.started":"2023-06-29T10:28:57.353423Z","shell.execute_reply":"2023-06-29T10:28:57.635406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ouput = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nouput['Label'] = y_pred\nouput.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T10:30:07.230981Z","iopub.execute_input":"2023-06-29T10:30:07.231366Z","iopub.status.idle":"2023-06-29T10:30:07.254856Z","shell.execute_reply.started":"2023-06-29T10:30:07.231336Z","shell.execute_reply":"2023-06-29T10:30:07.253747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ouput.to_csv('sub.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T10:30:40.920767Z","iopub.execute_input":"2023-06-29T10:30:40.921187Z","iopub.status.idle":"2023-06-29T10:30:40.966717Z","shell.execute_reply.started":"2023-06-29T10:30:40.921157Z","shell.execute_reply":"2023-06-29T10:30:40.965869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}