{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-27T20:25:13.057011Z","iopub.execute_input":"2023-03-27T20:25:13.058079Z","iopub.status.idle":"2023-03-27T20:25:14.209324Z","shell.execute_reply.started":"2023-03-27T20:25:13.058019Z","shell.execute_reply":"2023-03-27T20:25:14.207584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:14.211786Z","iopub.execute_input":"2023-03-27T20:25:14.212913Z","iopub.status.idle":"2023-03-27T20:25:18.087043Z","shell.execute_reply.started":"2023-03-27T20:25:14.212863Z","shell.execute_reply":"2023-03-27T20:25:18.085752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:18.088579Z","iopub.execute_input":"2023-03-27T20:25:18.089713Z","iopub.status.idle":"2023-03-27T20:25:20.136842Z","shell.execute_reply.started":"2023-03-27T20:25:18.089662Z","shell.execute_reply":"2023-03-27T20:25:20.135582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.drop(labels = [\"label\"],axis = 1)\ny_train = train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:20.140320Z","iopub.execute_input":"2023-03-27T20:25:20.141069Z","iopub.status.idle":"2023-03-27T20:25:20.272296Z","shell.execute_reply.started":"2023-03-27T20:25:20.141019Z","shell.execute_reply":"2023-03-27T20:25:20.271314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#plt.figure(figsize = (12,5))\n#sns.countplot(y_train, palette = \"icefire\")\n#plt.title(\"Number of digit classes\")\n#y_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-28T20:07:15.001476Z","iopub.execute_input":"2023-03-28T20:07:15.003062Z","iopub.status.idle":"2023-03-28T20:07:15.032087Z","shell.execute_reply.started":"2023-03-28T20:07:15.002997Z","shell.execute_reply":"2023-03-28T20:07:15.030254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = x_train.iloc[0].values\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":"2023-03-27T20:25:20.545254Z","iopub.execute_input":"2023-03-27T20:25:20.545614Z","iopub.status.idle":"2023-03-27T20:25:20.610192Z","shell.execute_reply.started":"2023-03-27T20:25:20.545579Z","shell.execute_reply":"2023-03-27T20:25:20.608890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = x_train.iloc[3].values\nimg = img.reshape(28,28)\nplt.imshow(img,cmap = \"gray\")\nplt.title(train.iloc[3,0])\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:20.611831Z","iopub.execute_input":"2023-03-27T20:25:20.612461Z","iopub.status.idle":"2023-03-27T20:25:20.676419Z","shell.execute_reply.started":"2023-03-27T20:25:20.612412Z","shell.execute_reply":"2023-03-27T20:25:20.675182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalization - Label Encoding","metadata":{}},{"cell_type":"code","source":"x_train = x_train / 255.0\ntest = test / 255.0\nprint(\"x_train shape :\",x_train.shape)\nprint(\"test shape :\",test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:20.678144Z","iopub.execute_input":"2023-03-27T20:25:20.679149Z","iopub.status.idle":"2023-03-27T20:25:20.827111Z","shell.execute_reply.started":"2023-03-27T20:25:20.679110Z","shell.execute_reply":"2023-03-27T20:25:20.825812Z"},"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 :\",test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:20.831270Z","iopub.execute_input":"2023-03-27T20:25:20.831635Z","iopub.status.idle":"2023-03-27T20:25:20.838199Z","shell.execute_reply.started":"2023-03-27T20:25:20.831597Z","shell.execute_reply":"2023-03-27T20:25:20.837161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\ny_train = to_categorical(y_train,num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:20.839390Z","iopub.execute_input":"2023-03-27T20:25:20.840380Z","iopub.status.idle":"2023-03-27T20:25:28.363445Z","shell.execute_reply.started":"2023-03-27T20:25:20.840345Z","shell.execute_reply":"2023-03-27T20:25:28.362246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=2)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_val shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_val shape\",Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:28.364851Z","iopub.execute_input":"2023-03-27T20:25:28.365590Z","iopub.status.idle":"2023-03-27T20:25:28.929225Z","shell.execute_reply.started":"2023-03-27T20:25:28.365546Z","shell.execute_reply":"2023-03-27T20:25:28.928005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom 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 keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:28.930568Z","iopub.execute_input":"2023-03-27T20:25:28.930892Z","iopub.status.idle":"2023-03-27T20:25:29.133924Z","shell.execute_reply.started":"2023-03-27T20:25:28.930860Z","shell.execute_reply":"2023-03-27T20:25:29.132828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 8,kernel_size = (5,5),padding = \"Same\",activation = \"relu\",input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 16,kernel_size = (3,3), padding = \"Same\",activation = \"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2), strides = (2,2)))\nmodel.add(Dropout(0.25))\n\n\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":"2023-03-27T20:25:29.135345Z","iopub.execute_input":"2023-03-27T20:25:29.135673Z","iopub.status.idle":"2023-03-27T20:25:29.357314Z","shell.execute_reply.started":"2023-03-27T20:25:29.135640Z","shell.execute_reply":"2023-03-27T20:25:29.356392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adam optimizer: Change the learning rate\noptimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:29.358835Z","iopub.execute_input":"2023-03-27T20:25:29.359411Z","iopub.status.idle":"2023-03-27T20:25:29.364027Z","shell.execute_reply.started":"2023-03-27T20:25:29.359374Z","shell.execute_reply":"2023-03-27T20:25:29.362983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#categorical crossentropy\n#We make binary cross entropy at previous parts and in machine learning tutorial\n#At this time we use categorical crossentropy. That means that we have multi class.\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:25:29.365458Z","iopub.execute_input":"2023-03-27T20:25:29.366179Z","iopub.status.idle":"2023-03-27T20:25:29.384369Z","shell.execute_reply.started":"2023-03-27T20:25:29.366123Z","shell.execute_reply":"2023-03-27T20:25:29.383307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 2\nbatch_size = 200","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:56:23.133185Z","iopub.execute_input":"2023-03-27T20:56:23.133640Z","iopub.status.idle":"2023-03-27T20:56:23.139146Z","shell.execute_reply.started":"2023-03-27T20:56:23.133599Z","shell.execute_reply":"2023-03-27T20:56:23.137819Z"},"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":"2023-03-27T20:29:17.801864Z","iopub.execute_input":"2023-03-27T20:29:17.802332Z","iopub.status.idle":"2023-03-27T20:29:17.934952Z","shell.execute_reply.started":"2023-03-27T20:29:17.802295Z","shell.execute_reply":"2023-03-27T20:29:17.933609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,Y_train,batch_size = batch_size),epochs = epochs,validation_data = (X_val,Y_val),steps_per_epoch = X_train.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T20:56:24.834049Z","iopub.execute_input":"2023-03-27T20:56:24.834465Z","iopub.status.idle":"2023-03-27T21:04:36.100221Z","shell.execute_reply.started":"2023-03-27T20:56:24.834428Z","shell.execute_reply":"2023-03-27T21:04:36.098902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}