{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30763,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nimport pandas as pd\nimport matplotlib.pyplot as plt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-20T00:12:29.083578Z","iopub.execute_input":"2024-09-20T00:12:29.083891Z","iopub.status.idle":"2024-09-20T00:12:41.484772Z","shell.execute_reply.started":"2024-09-20T00:12:29.083855Z","shell.execute_reply":"2024-09-20T00:12:41.483955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(r'/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:41.486333Z","iopub.execute_input":"2024-09-20T00:12:41.486860Z","iopub.status.idle":"2024-09-20T00:12:45.055107Z","shell.execute_reply.started":"2024-09-20T00:12:41.486824Z","shell.execute_reply":"2024-09-20T00:12:45.054260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:45.056317Z","iopub.execute_input":"2024-09-20T00:12:45.056630Z","iopub.status.idle":"2024-09-20T00:12:45.083357Z","shell.execute_reply.started":"2024-09-20T00:12:45.056597Z","shell.execute_reply":"2024-09-20T00:12:45.082394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(df)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:45.085636Z","iopub.execute_input":"2024-09-20T00:12:45.085942Z","iopub.status.idle":"2024-09-20T00:12:45.180392Z","shell.execute_reply.started":"2024-09-20T00:12:45.085910Z","shell.execute_reply":"2024-09-20T00:12:45.179586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_train  = np.array(data[:, 1:])\nimage_label = np.array(data[:,:1])","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:45.181639Z","iopub.execute_input":"2024-09-20T00:12:45.182301Z","iopub.status.idle":"2024-09-20T00:12:45.278522Z","shell.execute_reply.started":"2024-09-20T00:12:45.182247Z","shell.execute_reply":"2024-09-20T00:12:45.277512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_train = np.reshape(image_train ,(-1,28,28,1))","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:45.279698Z","iopub.execute_input":"2024-09-20T00:12:45.279984Z","iopub.status.idle":"2024-09-20T00:12:45.284507Z","shell.execute_reply.started":"2024-09-20T00:12:45.279954Z","shell.execute_reply":"2024-09-20T00:12:45.283576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_train = image_train / 255\nimage_label = tf.keras.utils.to_categorical(image_label , 10)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:45.285714Z","iopub.execute_input":"2024-09-20T00:12:45.286000Z","iopub.status.idle":"2024-09-20T00:12:45.465238Z","shell.execute_reply.started":"2024-09-20T00:12:45.285970Z","shell.execute_reply":"2024-09-20T00:12:45.464445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Input(shape=(28 ,28 , 1)),\n    tf.keras.layers.Conv2D(filters= 64 , kernel_size= (3,3) , activation='relu'),\n    tf.keras.layers.MaxPool2D(),\n    tf.keras.layers.Conv2D(filters = 128 , kernel_size= (3,3) , activation= 'relu'),\n    tf.keras.layers.MaxPool2D(),\n\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128 , activation= 'relu'),\n    tf.keras.layers.Dense(10 , activation= 'softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:45.466288Z","iopub.execute_input":"2024-09-20T00:12:45.466574Z","iopub.status.idle":"2024-09-20T00:12:46.149023Z","shell.execute_reply.started":"2024-09-20T00:12:45.466543Z","shell.execute_reply":"2024-09-20T00:12:46.148250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer= 'adam' , loss= 'categorical_crossentropy' , metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:46.150112Z","iopub.execute_input":"2024-09-20T00:12:46.150431Z","iopub.status.idle":"2024-09-20T00:12:46.163147Z","shell.execute_reply.started":"2024-09-20T00:12:46.150397Z","shell.execute_reply":"2024-09-20T00:12:46.162415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:46.166443Z","iopub.execute_input":"2024-09-20T00:12:46.166790Z","iopub.status.idle":"2024-09-20T00:12:46.192385Z","shell.execute_reply.started":"2024-09-20T00:12:46.166759Z","shell.execute_reply":"2024-09-20T00:12:46.191555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(image_train ,image_label , epochs = 25)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:12:46.193589Z","iopub.execute_input":"2024-09-20T00:12:46.194182Z","iopub.status.idle":"2024-09-20T00:14:01.152680Z","shell.execute_reply.started":"2024-09-20T00:12:46.194137Z","shell.execute_reply":"2024-09-20T00:14:01.151826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:14:26.297111Z","iopub.execute_input":"2024-09-20T00:14:26.297555Z","iopub.status.idle":"2024-09-20T00:14:26.304455Z","shell.execute_reply.started":"2024-09-20T00:14:26.297501Z","shell.execute_reply":"2024-09-20T00:14:26.303523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(r'/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:16:08.635668Z","iopub.execute_input":"2024-09-20T00:16:08.636659Z","iopub.status.idle":"2024-09-20T00:16:10.793304Z","shell.execute_reply.started":"2024-09-20T00:16:08.636617Z","shell.execute_reply":"2024-09-20T00:16:10.792260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(df)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:16:16.473168Z","iopub.execute_input":"2024-09-20T00:16:16.473613Z","iopub.status.idle":"2024-09-20T00:16:16.538934Z","shell.execute_reply.started":"2024-09-20T00:16:16.473577Z","shell.execute_reply":"2024-09-20T00:16:16.538067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_test = np.reshape(data , (-1 , 28 , 28 , 1)) /255","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:25:35.402113Z","iopub.execute_input":"2024-09-20T00:25:35.403082Z","iopub.status.idle":"2024-09-20T00:25:35.474421Z","shell.execute_reply.started":"2024-09-20T00:25:35.403040Z","shell.execute_reply":"2024-09-20T00:25:35.473571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(image_test)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:27:39.713149Z","iopub.execute_input":"2024-09-20T00:27:39.713590Z","iopub.status.idle":"2024-09-20T00:27:41.431807Z","shell.execute_reply.started":"2024-09-20T00:27:39.713551Z","shell.execute_reply":"2024-09-20T00:27:41.430933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions , axis = 1)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:27:41.433477Z","iopub.execute_input":"2024-09-20T00:27:41.433784Z","iopub.status.idle":"2024-09-20T00:27:41.438709Z","shell.execute_reply.started":"2024-09-20T00:27:41.433751Z","shell.execute_reply":"2024-09-20T00:27:41.437840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:27:41.439853Z","iopub.execute_input":"2024-09-20T00:27:41.440232Z","iopub.status.idle":"2024-09-20T00:27:41.451558Z","shell.execute_reply.started":"2024-09-20T00:27:41.440172Z","shell.execute_reply":"2024-09-20T00:27:41.450680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = range(1 , len(predictions) +1)\nsubmission = pd.DataFrame({\n    'ImageId' : image_ids , \n    'Label'   : predictions\n})","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:27:41.462548Z","iopub.status.idle":"2024-09-20T00:27:41.462880Z","shell.execute_reply.started":"2024-09-20T00:27:41.462713Z","shell.execute_reply":"2024-09-20T00:27:41.462730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('mnist_submission.csv' , index = False)","metadata":{"execution":{"iopub.status.busy":"2024-09-20T00:27:59.974647Z","iopub.execute_input":"2024-09-20T00:27:59.975506Z","iopub.status.idle":"2024-09-20T00:28:00.013226Z","shell.execute_reply.started":"2024-09-20T00:27:59.975465Z","shell.execute_reply":"2024-09-20T00:28:00.012453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}