{"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 matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom keras.models import Sequential\nimport keras\nfrom keras.utils import np_utils","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:47:28.874894Z","iopub.execute_input":"2022-07-12T09:47:28.875325Z","iopub.status.idle":"2022-07-12T09:47:39.678726Z","shell.execute_reply.started":"2022-07-12T09:47:28.875293Z","shell.execute_reply":"2022-07-12T09:47:39.677296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/digit-recognizer/train.csv'\ntest_dir = '../input/digit-recognizer/test.csv'","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:47:39.682121Z","iopub.execute_input":"2022-07-12T09:47:39.683089Z","iopub.status.idle":"2022-07-12T09:47:39.689030Z","shell.execute_reply.started":"2022-07-12T09:47:39.683039Z","shell.execute_reply":"2022-07-12T09:47:39.687634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(train_dir)\ntest_df = pd.read_csv(test_dir) # for validation and competition submission\nprint(train_df.shape, test_df.shape) ","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:47:39.691475Z","iopub.execute_input":"2022-07-12T09:47:39.692428Z","iopub.status.idle":"2022-07-12T09:47:46.409278Z","shell.execute_reply.started":"2022-07-12T09:47:39.692370Z","shell.execute_reply":"2022-07-12T09:47:46.407621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_df.drop(columns='label', inplace=False) #drop 'label' column\ny_train = train_df.label\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:34:06.198379Z","iopub.execute_input":"2022-07-11T15:34:06.198838Z","iopub.status.idle":"2022-07-11T15:34:06.324705Z","shell.execute_reply.started":"2022-07-11T15:34:06.198801Z","shell.execute_reply":"2022-07-11T15:34:06.323428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.array(x_train)\ny_train = np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:34:29.982765Z","iopub.execute_input":"2022-07-11T15:34:29.983232Z","iopub.status.idle":"2022-07-11T15:34:30.107131Z","shell.execute_reply.started":"2022-07-11T15:34:29.983196Z","shell.execute_reply":"2022-07-11T15:34:30.105810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], 28 ,28 ,1)\nprint(x_train.shape, y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:39:06.230058Z","iopub.execute_input":"2022-07-11T15:39:06.230561Z","iopub.status.idle":"2022-07-11T15:39:06.237674Z","shell.execute_reply.started":"2022-07-11T15:39:06.230525Z","shell.execute_reply":"2022-07-11T15:39:06.236377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = keras.utils.np_utils.to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:42:42.452515Z","iopub.execute_input":"2022-07-11T15:42:42.453082Z","iopub.status.idle":"2022-07-11T15:42:42.459715Z","shell.execute_reply.started":"2022-07-11T15:42:42.453023Z","shell.execute_reply":"2022-07-11T15:42:42.458542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:47:56.082504Z","iopub.execute_input":"2022-07-12T09:47:56.083129Z","iopub.status.idle":"2022-07-12T09:47:56.259312Z","shell.execute_reply.started":"2022-07-12T09:47:56.083094Z","shell.execute_reply":"2022-07-12T09:47:56.257967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss=keras.losses.categorical_crossentropy,\n    optimizer='adam',\n    metrics=['accuracy']\n)\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:48:10.618864Z","iopub.execute_input":"2022-07-12T09:48:10.619276Z","iopub.status.idle":"2022-07-12T09:48:10.640227Z","shell.execute_reply.started":"2022-07-12T09:48:10.619244Z","shell.execute_reply":"2022-07-12T09:48:10.638794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train,\n                    y_train,\n                    validation_split=0.2,\n                    epochs=10,\n                    batch_size=128,\n                    verbose=1,\n                    shuffle=True\n                   )","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:07:21.700096Z","iopub.execute_input":"2022-07-11T16:07:21.700622Z","iopub.status.idle":"2022-07-11T16:19:43.136315Z","shell.execute_reply.started":"2022-07-11T16:07:21.700586Z","shell.execute_reply":"2022-07-11T16:19:43.135163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Data')\nplt.plot(history.history['val_loss'], label='Test Data')\nplt.ylabel('Loss value')\nplt.xlabel('No. epoch')\nplt.legend(loc=\"upper right\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:21:00.200208Z","iopub.execute_input":"2022-07-11T16:21:00.200744Z","iopub.status.idle":"2022-07-11T16:21:00.459188Z","shell.execute_reply.started":"2022-07-11T16:21:00.200706Z","shell.execute_reply":"2022-07-11T16:21:00.457831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='Training Data')\nplt.plot(history.history['val_accuracy'], label='Test Data')\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:21:01.718278Z","iopub.execute_input":"2022-07-11T16:21:01.718736Z","iopub.status.idle":"2022-07-11T16:21:01.996961Z","shell.execute_reply.started":"2022-07-11T16:21:01.718701Z","shell.execute_reply":"2022-07-11T16:21:01.995851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = np.array(test_df).reshape(-1,28,28,1)\nres = model.predict(test_df/255)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:48:19.401365Z","iopub.execute_input":"2022-07-12T09:48:19.402007Z","iopub.status.idle":"2022-07-12T09:48:29.719766Z","shell.execute_reply.started":"2022-07-12T09:48:19.401944Z","shell.execute_reply":"2022-07-12T09:48:29.718706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = res.argmax(axis=1)\npredicted","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:49:47.847775Z","iopub.execute_input":"2022-07-12T09:49:47.848187Z","iopub.status.idle":"2022-07-12T09:49:47.858084Z","shell.execute_reply.started":"2022-07-12T09:49:47.848155Z","shell.execute_reply":"2022-07-12T09:49:47.856627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create submission csv file\noutput = pd.DataFrame({'ImageId':range(1,test_df.shape[0]+1),'Label': predicted})\noutput.to_csv('my_submission.csv',index = False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:49:58.272582Z","iopub.execute_input":"2022-07-12T09:49:58.272975Z","iopub.status.idle":"2022-07-12T09:49:58.329920Z","shell.execute_reply.started":"2022-07-12T09:49:58.272944Z","shell.execute_reply":"2022-07-12T09:49:58.328736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('my_submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:51:38.377985Z","iopub.execute_input":"2022-07-12T09:51:38.378411Z","iopub.status.idle":"2022-07-12T09:51:38.405690Z","shell.execute_reply.started":"2022-07-12T09:51:38.378378Z","shell.execute_reply":"2022-07-12T09:51:38.404494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(r'./MNISTsubmission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T09:58:14.209778Z","iopub.execute_input":"2022-07-12T09:58:14.210203Z","iopub.status.idle":"2022-07-12T09:58:14.265209Z","shell.execute_reply.started":"2022-07-12T09:58:14.210170Z","shell.execute_reply":"2022-07-12T09:58:14.263798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}