{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.layers import Dense,BatchNormalization,Dropout\nfrom keras.models import Sequential\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:00:17.572671Z","iopub.execute_input":"2024-02-19T07:00:17.573019Z","iopub.status.idle":"2024-02-19T07:00:32.486378Z","shell.execute_reply.started":"2024-02-19T07:00:17.572992Z","shell.execute_reply":"2024-02-19T07:00:32.485279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_ds=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:00:40.386915Z","iopub.execute_input":"2024-02-19T07:00:40.387829Z","iopub.status.idle":"2024-02-19T07:00:45.922515Z","shell.execute_reply.started":"2024-02-19T07:00:40.387787Z","shell.execute_reply":"2024-02-19T07:00:45.921654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:00:48.511156Z","iopub.execute_input":"2024-02-19T07:00:48.512016Z","iopub.status.idle":"2024-02-19T07:00:48.535208Z","shell.execute_reply.started":"2024-02-19T07:00:48.511977Z","shell.execute_reply":"2024-02-19T07:00:48.534363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train_ds.drop(columns=['label'])\ny=train_ds['label']","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:00:51.062444Z","iopub.execute_input":"2024-02-19T07:00:51.062800Z","iopub.status.idle":"2024-02-19T07:00:51.147793Z","shell.execute_reply.started":"2024-02-19T07:00:51.062775Z","shell.execute_reply":"2024-02-19T07:00:51.146991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X.astype(float)/255","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:00:53.355972Z","iopub.execute_input":"2024-02-19T07:00:53.356335Z","iopub.status.idle":"2024-02-19T07:00:53.506647Z","shell.execute_reply.started":"2024-02-19T07:00:53.356308Z","shell.execute_reply":"2024-02-19T07:00:53.505783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:00:55.797816Z","iopub.execute_input":"2024-02-19T07:00:55.798789Z","iopub.status.idle":"2024-02-19T07:00:56.013989Z","shell.execute_reply.started":"2024-02-19T07:00:55.798754Z","shell.execute_reply":"2024-02-19T07:00:56.012544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Dense(256,activation='relu',input_dim=784))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(128,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(32,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:23:57.872487Z","iopub.execute_input":"2024-02-19T07:23:57.873309Z","iopub.status.idle":"2024-02-19T07:23:58.010136Z","shell.execute_reply.started":"2024-02-19T07:23:57.873273Z","shell.execute_reply":"2024-02-19T07:23:58.009260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy',metrics=['accuracy'],optimizer=keras.optimizers.RMSprop(learning_rate=2e-4))","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:24:00.821905Z","iopub.execute_input":"2024-02-19T07:24:00.822556Z","iopub.status.idle":"2024-02-19T07:24:00.834900Z","shell.execute_reply.started":"2024-02-19T07:24:00.822524Z","shell.execute_reply":"2024-02-19T07:24:00.834005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=30)","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:24:03.148801Z","iopub.execute_input":"2024-02-19T07:24:03.149140Z","iopub.status.idle":"2024-02-19T07:26:27.673315Z","shell.execute_reply.started":"2024-02-19T07:24:03.149115Z","shell.execute_reply":"2024-02-19T07:26:27.672221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:20.618419Z","iopub.execute_input":"2024-02-19T07:28:20.619364Z","iopub.status.idle":"2024-02-19T07:28:20.627098Z","shell.execute_reply.started":"2024-02-19T07:28:20.619320Z","shell.execute_reply":"2024-02-19T07:28:20.626055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds=test_ds.astype(float)/255","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:22.241258Z","iopub.execute_input":"2024-02-19T07:28:22.241881Z","iopub.status.idle":"2024-02-19T07:28:22.333647Z","shell.execute_reply.started":"2024-02-19T07:28:22.241850Z","shell.execute_reply":"2024-02-19T07:28:22.332581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_arr=model.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:24.100270Z","iopub.execute_input":"2024-02-19T07:28:24.100869Z","iopub.status.idle":"2024-02-19T07:28:27.130790Z","shell.execute_reply.started":"2024-02-19T07:28:24.100838Z","shell.execute_reply":"2024-02-19T07:28:27.129825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_arr[0]","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:27.364390Z","iopub.execute_input":"2024-02-19T07:28:27.364736Z","iopub.status.idle":"2024-02-19T07:28:27.370960Z","shell.execute_reply.started":"2024-02-19T07:28:27.364702Z","shell.execute_reply":"2024-02-19T07:28:27.370007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=[]\nfor arr in prediction_arr:\n    predictions.append(np.argmax(arr))","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:32.218523Z","iopub.execute_input":"2024-02-19T07:28:32.219152Z","iopub.status.idle":"2024-02-19T07:28:32.317757Z","shell.execute_reply.started":"2024-02-19T07:28:32.219118Z","shell.execute_reply":"2024-02-19T07:28:32.316774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output=pd.DataFrame({\n    'ImageId':np.arange(1,28001),\n    'Label':predictions\n})\noutput.to_csv('./output.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:44.950216Z","iopub.execute_input":"2024-02-19T07:28:44.950564Z","iopub.status.idle":"2024-02-19T07:28:45.077340Z","shell.execute_reply.started":"2024-02-19T07:28:44.950537Z","shell.execute_reply":"2024-02-19T07:28:45.076386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:28:46.809923Z","iopub.execute_input":"2024-02-19T07:28:46.810339Z","iopub.status.idle":"2024-02-19T07:28:46.821035Z","shell.execute_reply.started":"2024-02-19T07:28:46.810308Z","shell.execute_reply":"2024-02-19T07:28:46.819898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'],color='red',label='train')\nplt.plot(history.history['val_accuracy'],color='blue',label='validation')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:35:25.387422Z","iopub.execute_input":"2024-02-19T07:35:25.387767Z","iopub.status.idle":"2024-02-19T07:35:25.640667Z","shell.execute_reply.started":"2024-02-19T07:35:25.387741Z","shell.execute_reply":"2024-02-19T07:35:25.639722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'],color='red',label='train')\nplt.plot(history.history['val_loss'],color='blue',label='validation')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-19T07:35:38.909318Z","iopub.execute_input":"2024-02-19T07:35:38.910009Z","iopub.status.idle":"2024-02-19T07:35:39.111632Z","shell.execute_reply.started":"2024-02-19T07:35:38.909976Z","shell.execute_reply":"2024-02-19T07:35:39.110598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}