{"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 seaborn as sns\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\nimport os\nprint(os.listdir(\"../input/digit-recognizer\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-13T12:22:32.641727Z","iopub.execute_input":"2022-04-13T12:22:32.642004Z","iopub.status.idle":"2022-04-13T12:22:32.650508Z","shell.execute_reply.started":"2022-04-13T12:22:32.641971Z","shell.execute_reply":"2022-04-13T12:22:32.649703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')\nprint(train.head())\nprint(train.shape)\nprint(test.head())\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:34.244624Z","iopub.execute_input":"2022-04-13T12:22:34.245171Z","iopub.status.idle":"2022-04-13T12:22:37.874199Z","shell.execute_reply.started":"2022-04-13T12:22:34.245135Z","shell.execute_reply":"2022-04-13T12:22:37.873312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train=train['label']\nX_train=train.drop(['label'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:40.270734Z","iopub.execute_input":"2022-04-13T12:22:40.271450Z","iopub.status.idle":"2022-04-13T12:22:40.356885Z","shell.execute_reply.started":"2022-04-13T12:22:40.271403Z","shell.execute_reply":"2022-04-13T12:22:40.356157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,7))\ng = sns.distplot(Y_train,kde=True, bins=15)\nplt.title(\"Numbers\")  ","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:41.035521Z","iopub.execute_input":"2022-04-13T12:22:41.035794Z","iopub.status.idle":"2022-04-13T12:22:41.489530Z","shell.execute_reply.started":"2022-04-13T12:22:41.035764Z","shell.execute_reply":"2022-04-13T12:22:41.488857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.asarray(X_train.iloc[0])\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[0,0])\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:45.128946Z","iopub.execute_input":"2022-04-13T12:22:45.131439Z","iopub.status.idle":"2022-04-13T12:22:45.223960Z","shell.execute_reply.started":"2022-04-13T12:22:45.131392Z","shell.execute_reply":"2022-04-13T12:22:45.222653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.asarray(X_train.iloc[10])\nimg = img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train.iloc[10,0])\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:43.287818Z","iopub.execute_input":"2022-04-13T12:22:43.290409Z","iopub.status.idle":"2022-04-13T12:22:43.377814Z","shell.execute_reply.started":"2022-04-13T12:22:43.290360Z","shell.execute_reply":"2022-04-13T12:22:43.376845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X_train/255\ntest=test/255","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:47.353723Z","iopub.execute_input":"2022-04-13T12:22:47.354646Z","iopub.status.idle":"2022-04-13T12:22:47.496475Z","shell.execute_reply.started":"2022-04-13T12:22:47.354597Z","shell.execute_reply":"2022-04-13T12:22:47.494762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(\"x_train shape: \",X_train.shape)\nprint(\"test shape: \",test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:48.135501Z","iopub.execute_input":"2022-04-13T12:22:48.136162Z","iopub.status.idle":"2022-04-13T12:22:48.144717Z","shell.execute_reply.started":"2022-04-13T12:22:48.136112Z","shell.execute_reply":"2022-04-13T12:22:48.143784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nY_train = to_categorical(Y_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:48.694590Z","iopub.execute_input":"2022-04-13T12:22:48.695443Z","iopub.status.idle":"2022-04-13T12:22:48.700284Z","shell.execute_reply.started":"2022-04-13T12:22:48.695402Z","shell.execute_reply":"2022-04-13T12:22:48.699540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape #10 outputs","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:50.179661Z","iopub.execute_input":"2022-04-13T12:22:50.180178Z","iopub.status.idle":"2022-04-13T12:22:50.185915Z","shell.execute_reply.started":"2022-04-13T12:22:50.180146Z","shell.execute_reply":"2022-04-13T12:22:50.185366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, Y_train, Y_test = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:50.847117Z","iopub.execute_input":"2022-04-13T12:22:50.848031Z","iopub.status.idle":"2022-04-13T12:22:52.207207Z","shell.execute_reply.started":"2022-04-13T12:22:50.847995Z","shell.execute_reply":"2022-04-13T12:22:52.205694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:53.501504Z","iopub.execute_input":"2022-04-13T12:22:53.501906Z","iopub.status.idle":"2022-04-13T12:22:53.510096Z","shell.execute_reply.started":"2022-04-13T12:22:53.501864Z","shell.execute_reply":"2022-04-13T12:22:53.508946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10, activation = \"softmax\"))\noptimizer = Adam(lr=0.00001, beta_1=0.9, beta_2=0.999)\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:54.186287Z","iopub.execute_input":"2022-04-13T12:22:54.186566Z","iopub.status.idle":"2022-04-13T12:22:54.269424Z","shell.execute_reply.started":"2022-04-13T12:22:54.186537Z","shell.execute_reply":"2022-04-13T12:22:54.268486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=5,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.1, # Randomly zoom image 10%\n        width_shift_range=0.1,  # randomly shift images horizontally 10%\n        height_shift_range=0.1,  # randomly shift images vertically 10%\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:22:56.097883Z","iopub.execute_input":"2022-04-13T12:22:56.098157Z","iopub.status.idle":"2022-04-13T12:22:56.250982Z","shell.execute_reply.started":"2022-04-13T12:22:56.098128Z","shell.execute_reply":"2022-04-13T12:22:56.250041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=32),\n                              epochs = 5, validation_data = (X_test,Y_test))","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:31:35.550474Z","iopub.execute_input":"2022-04-13T12:31:35.550729Z","iopub.status.idle":"2022-04-13T12:32:54.778624Z","shell.execute_reply.started":"2022-04-13T12:31:35.550702Z","shell.execute_reply":"2022-04-13T12:32:54.777597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:32:59.480241Z","iopub.execute_input":"2022-04-13T12:32:59.480548Z","iopub.status.idle":"2022-04-13T12:32:59.700191Z","shell.execute_reply.started":"2022-04-13T12:32:59.480513Z","shell.execute_reply":"2022-04-13T12:32:59.699350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf = pd.DataFrame()\ndf[\"ImageId\"] = range(1,len(test)+1)\nprobalilites = model.predict(test)\ndigits =[0,1,2,3,4,5,6,7,8,9]\nlabels = [digits[np.argmax(prob)] for prob in probalilites]\ndf[\"Label\"] =labels\ndf.set_index(\"ImageId\")\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T12:33:02.957821Z","iopub.execute_input":"2022-04-13T12:33:02.958107Z","iopub.status.idle":"2022-04-13T12:33:05.509020Z","shell.execute_reply.started":"2022-04-13T12:33:02.958075Z","shell.execute_reply":"2022-04-13T12:33:05.508129Z"},"trusted":true},"execution_count":null,"outputs":[]}]}