{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-10T04:19:19.377427Z","iopub.execute_input":"2023-06-10T04:19:19.378343Z","iopub.status.idle":"2023-06-10T04:19:19.424288Z","shell.execute_reply.started":"2023-06-10T04:19:19.378299Z","shell.execute_reply":"2023-06-10T04:19:19.422642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils.np_utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:19:29.819498Z","iopub.execute_input":"2023-06-10T04:19:29.819906Z","iopub.status.idle":"2023-06-10T04:19:39.889548Z","shell.execute_reply.started":"2023-06-10T04:19:29.819848Z","shell.execute_reply":"2023-06-10T04:19:39.888553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_dataset = pd.read_csv(\"../input/digit-recognizer/test.csv\")\nX = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:24:40.500679Z","iopub.execute_input":"2023-06-10T04:24:40.502420Z","iopub.status.idle":"2023-06-10T04:24:45.937642Z","shell.execute_reply.started":"2023-06-10T04:24:40.502376Z","shell.execute_reply":"2023-06-10T04:24:45.936437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head(100)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:26:45.140418Z","iopub.execute_input":"2023-06-10T04:26:45.140854Z","iopub.status.idle":"2023-06-10T04:26:45.154680Z","shell.execute_reply.started":"2023-06-10T04:26:45.140821Z","shell.execute_reply":"2023-06-10T04:26:45.153308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train_dataset[\"label\"]\nX_train = train_dataset.drop(labels = [\"label\"],axis = 1)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:43:40.882340Z","iopub.execute_input":"2023-06-10T04:43:40.882800Z","iopub.status.idle":"2023-06-10T04:43:41.023597Z","shell.execute_reply.started":"2023-06-10T04:43:40.882768Z","shell.execute_reply":"2023-06-10T04:43:41.022258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = test_dataset / 255.0\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:43:50.500767Z","iopub.execute_input":"2023-06-10T04:43:50.501200Z","iopub.status.idle":"2023-06-10T04:43:50.638958Z","shell.execute_reply.started":"2023-06-10T04:43:50.501161Z","shell.execute_reply":"2023-06-10T04:43:50.637966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\nX_test = X_test.values.reshape(-1,28,28,1)\nY_train = to_categorical(Y_train, num_classes = 10)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:43:52.604112Z","iopub.execute_input":"2023-06-10T04:43:52.604481Z","iopub.status.idle":"2023-06-10T04:43:52.613084Z","shell.execute_reply.started":"2023-06-10T04:43:52.604454Z","shell.execute_reply":"2023-06-10T04:43:52.611545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(11,5))\nfor i in range(30):  \n    plt.subplot(3, 10, i+1)\n    plt.imshow(X_train[i].reshape((28,28)),cmap=plt.cm.binary)\n    plt.axis('off')\nplt.subplots_adjust(wspace=-0.1, hspace=-0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:43:55.402036Z","iopub.execute_input":"2023-06-10T04:43:55.402504Z","iopub.status.idle":"2023-06-10T04:43:57.199103Z","shell.execute_reply.started":"2023-06-10T04:43:55.402472Z","shell.execute_reply":"2023-06-10T04:43:57.197892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rotation_range=10,zoom_range = 0.10,shear_range = 0.10,width_shift_range=0.1,height_shift_range=0.1)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:44:00.364850Z","iopub.execute_input":"2023-06-10T04:44:00.365329Z","iopub.status.idle":"2023-06-10T04:44:00.372308Z","shell.execute_reply.started":"2023-06-10T04:44:00.365296Z","shell.execute_reply":"2023-06-10T04:44:00.370688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:48:43.980891Z","iopub.execute_input":"2023-06-10T04:48:43.981398Z","iopub.status.idle":"2023-06-10T04:48:43.992169Z","shell.execute_reply.started":"2023-06-10T04:48:43.981361Z","shell.execute_reply":"2023-06-10T04:48:43.990771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = (28, 28, 1)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32, kernel_size = 3, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\nmodel.add(Conv2D(64, kernel_size = 3, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64, kernel_size = 3, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.4))\n\nmodel.add(Conv2D(128, kernel_size = 4, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\nmodel.add(Dropout(0.4))\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:48:47.143957Z","iopub.execute_input":"2023-06-10T04:48:47.144456Z","iopub.status.idle":"2023-06-10T04:48:47.425985Z","shell.execute_reply.started":"2023-06-10T04:48:47.144422Z","shell.execute_reply":"2023-06-10T04:48:47.424425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\n    ","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:48:48.757643Z","iopub.execute_input":"2023-06-10T04:48:48.759036Z","iopub.status.idle":"2023-06-10T04:48:48.783238Z","shell.execute_reply.started":"2023-06-10T04:48:48.758993Z","shell.execute_reply":"2023-06-10T04:48:48.781891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.15)\nmodel.fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64),\n        epochs = 25,validation_data = (X_val2,Y_val2))","metadata":{"execution":{"iopub.status.busy":"2023-06-10T04:48:50.069476Z","iopub.execute_input":"2023-06-10T04:48:50.069919Z","iopub.status.idle":"2023-06-10T05:36:48.721617Z","shell.execute_reply.started":"2023-06-10T04:48:50.069881Z","shell.execute_reply":"2023-06-10T05:36:48.719943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = np.zeros( (X_test.shape[0],10) ) \nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"MNIST_data-result.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T06:16:31.225071Z","iopub.execute_input":"2023-06-10T06:16:31.227148Z","iopub.status.idle":"2023-06-10T06:16:31.339430Z","shell.execute_reply.started":"2023-06-10T06:16:31.227068Z","shell.execute_reply":"2023-06-10T06:16:31.337691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-10T03:39:09.949266Z","iopub.execute_input":"2023-06-10T03:39:09.950466Z","iopub.status.idle":"2023-06-10T03:39:24.473357Z","shell.execute_reply.started":"2023-06-10T03:39:09.950413Z","shell.execute_reply":"2023-06-10T03:39:24.472465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-06-10T03:31:23.914915Z","iopub.execute_input":"2023-06-10T03:31:23.915764Z","iopub.status.idle":"2023-06-10T03:31:23.964263Z","shell.execute_reply.started":"2023-06-10T03:31:23.915719Z","shell.execute_reply":"2023-06-10T03:31:23.963436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}