{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\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-21T02:15:13.498538Z","iopub.execute_input":"2023-06-21T02:15:13.499017Z","iopub.status.idle":"2023-06-21T02:15:13.511835Z","shell.execute_reply.started":"2023-06-21T02:15:13.498980Z","shell.execute_reply":"2023-06-21T02:15:13.509845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:13.515265Z","iopub.execute_input":"2023-06-21T02:15:13.517336Z","iopub.status.idle":"2023-06-21T02:15:19.302223Z","shell.execute_reply.started":"2023-06-21T02:15:13.517269Z","shell.execute_reply":"2023-06-21T02:15:19.300429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.305031Z","iopub.execute_input":"2023-06-21T02:15:19.305607Z","iopub.status.idle":"2023-06-21T02:15:19.337273Z","shell.execute_reply.started":"2023-06-21T02:15:19.305550Z","shell.execute_reply":"2023-06-21T02:15:19.335690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.339058Z","iopub.execute_input":"2023-06-21T02:15:19.339553Z","iopub.status.idle":"2023-06-21T02:15:19.370944Z","shell.execute_reply.started":"2023-06-21T02:15:19.339499Z","shell.execute_reply":"2023-06-21T02:15:19.369267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.375858Z","iopub.execute_input":"2023-06-21T02:15:19.376274Z","iopub.status.idle":"2023-06-21T02:15:19.392097Z","shell.execute_reply.started":"2023-06-21T02:15:19.376236Z","shell.execute_reply":"2023-06-21T02:15:19.390247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data = train_data, x = \"label\")","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.394529Z","iopub.execute_input":"2023-06-21T02:15:19.395010Z","iopub.status.idle":"2023-06-21T02:15:19.684405Z","shell.execute_reply.started":"2023-06-21T02:15:19.394967Z","shell.execute_reply":"2023-06-21T02:15:19.682910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.687042Z","iopub.execute_input":"2023-06-21T02:15:19.688189Z","iopub.status.idle":"2023-06-21T02:15:19.749105Z","shell.execute_reply.started":"2023-06-21T02:15:19.688126Z","shell.execute_reply":"2023-06-21T02:15:19.747142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_id = sample_sub.ImageId.copy()","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.750672Z","iopub.execute_input":"2023-06-21T02:15:19.751893Z","iopub.status.idle":"2023-06-21T02:15:19.758197Z","shell.execute_reply.started":"2023-06-21T02:15:19.751839Z","shell.execute_reply":"2023-06-21T02:15:19.756862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.760557Z","iopub.execute_input":"2023-06-21T02:15:19.761145Z","iopub.status.idle":"2023-06-21T02:15:19.776918Z","shell.execute_reply.started":"2023-06-21T02:15:19.761090Z","shell.execute_reply":"2023-06-21T02:15:19.775046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_data.drop([\"label\"], axis = 1)\ny_train = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.779644Z","iopub.execute_input":"2023-06-21T02:15:19.780159Z","iopub.status.idle":"2023-06-21T02:15:19.917077Z","shell.execute_reply.started":"2023-06-21T02:15:19.780108Z","shell.execute_reply":"2023-06-21T02:15:19.915280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_data\ny_test = sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.919425Z","iopub.execute_input":"2023-06-21T02:15:19.920849Z","iopub.status.idle":"2023-06-21T02:15:19.930709Z","shell.execute_reply.started":"2023-06-21T02:15:19.920761Z","shell.execute_reply":"2023-06-21T02:15:19.928790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_vals = x_train.isnull().sum()\nprint(missing_vals[missing_vals > 0])","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.932642Z","iopub.execute_input":"2023-06-21T02:15:19.934363Z","iopub.status.idle":"2023-06-21T02:15:19.997278Z","shell.execute_reply.started":"2023-06-21T02:15:19.934290Z","shell.execute_reply":"2023-06-21T02:15:19.995254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_vals = x_test.isnull().sum()\nprint(missing_vals[missing_vals > 0])","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:19.999490Z","iopub.execute_input":"2023-06-21T02:15:19.999924Z","iopub.status.idle":"2023-06-21T02:15:20.047196Z","shell.execute_reply.started":"2023-06-21T02:15:19.999886Z","shell.execute_reply":"2023-06-21T02:15:20.045266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.054238Z","iopub.execute_input":"2023-06-21T02:15:20.054723Z","iopub.status.idle":"2023-06-21T02:15:20.066489Z","shell.execute_reply.started":"2023-06-21T02:15:20.054684Z","shell.execute_reply":"2023-06-21T02:15:20.064235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.get_dummies(y_train).values","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.068683Z","iopub.execute_input":"2023-06-21T02:15:20.069211Z","iopub.status.idle":"2023-06-21T02:15:20.079058Z","shell.execute_reply.started":"2023-06-21T02:15:20.069158Z","shell.execute_reply":"2023-06-21T02:15:20.077210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx, x_val, y, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.080987Z","iopub.execute_input":"2023-06-21T02:15:20.081539Z","iopub.status.idle":"2023-06-21T02:15:20.303537Z","shell.execute_reply.started":"2023-06-21T02:15:20.081486Z","shell.execute_reply":"2023-06-21T02:15:20.302188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.305109Z","iopub.execute_input":"2023-06-21T02:15:20.305664Z","iopub.status.idle":"2023-06-21T02:15:20.316343Z","shell.execute_reply.started":"2023-06-21T02:15:20.305610Z","shell.execute_reply":"2023-06-21T02:15:20.314375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.318567Z","iopub.execute_input":"2023-06-21T02:15:20.320045Z","iopub.status.idle":"2023-06-21T02:15:20.331243Z","shell.execute_reply.started":"2023-06-21T02:15:20.319960Z","shell.execute_reply":"2023-06-21T02:15:20.329473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.334015Z","iopub.execute_input":"2023-06-21T02:15:20.334523Z","iopub.status.idle":"2023-06-21T02:15:20.345368Z","shell.execute_reply.started":"2023-06-21T02:15:20.334482Z","shell.execute_reply":"2023-06-21T02:15:20.343225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\nfrom keras import models\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\n\n# Define the CNN model\nmodel = models.Sequential()\n\nmodel.add(layers.Conv2D(32, (5,5), activation='relu',padding = 'Same', input_shape=(28, 28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (5, 5), padding = 'Same', activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(256, (5, 5), padding = 'Same', activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (5, 5), padding = 'Same', activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (5, 5), padding = 'Same', activation='relu'))\nmodel.add(layers.Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(layers.Dense(10, activation='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.347444Z","iopub.execute_input":"2023-06-21T02:15:20.347871Z","iopub.status.idle":"2023-06-21T02:15:20.550874Z","shell.execute_reply.started":"2023-06-21T02:15:20.347834Z","shell.execute_reply":"2023-06-21T02:15:20.549067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(x).reshape(x.shape[0], 28, 28, 1)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.552836Z","iopub.execute_input":"2023-06-21T02:15:20.553213Z","iopub.status.idle":"2023-06-21T02:15:20.655392Z","shell.execute_reply.started":"2023-06-21T02:15:20.553178Z","shell.execute_reply":"2023-06-21T02:15:20.653673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.657999Z","iopub.execute_input":"2023-06-21T02:15:20.658973Z","iopub.status.idle":"2023-06-21T02:15:20.668671Z","shell.execute_reply.started":"2023-06-21T02:15:20.658916Z","shell.execute_reply":"2023-06-21T02:15:20.666739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = x\ntrain_images = train_images.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.669795Z","iopub.execute_input":"2023-06-21T02:15:20.670194Z","iopub.status.idle":"2023-06-21T02:15:20.751259Z","shell.execute_reply.started":"2023-06-21T02:15:20.670158Z","shell.execute_reply":"2023-06-21T02:15:20.749816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.array(x_test).reshape(x_test.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.753371Z","iopub.execute_input":"2023-06-21T02:15:20.753751Z","iopub.status.idle":"2023-06-21T02:15:20.839119Z","shell.execute_reply.started":"2023-06-21T02:15:20.753715Z","shell.execute_reply":"2023-06-21T02:15:20.837415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = x_test\ntest_images = test_images.astype('float32') / 255\n\ntest_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.841051Z","iopub.execute_input":"2023-06-21T02:15:20.841584Z","iopub.status.idle":"2023-06-21T02:15:20.932711Z","shell.execute_reply.started":"2023-06-21T02:15:20.841516Z","shell.execute_reply":"2023-06-21T02:15:20.930924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = np.array(x_val).reshape(x_val.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.934922Z","iopub.execute_input":"2023-06-21T02:15:20.935674Z","iopub.status.idle":"2023-06-21T02:15:20.946655Z","shell.execute_reply.started":"2023-06-21T02:15:20.935627Z","shell.execute_reply":"2023-06-21T02:15:20.944725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = x_val.astype('float32') / 255\n\nx_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.951166Z","iopub.execute_input":"2023-06-21T02:15:20.951646Z","iopub.status.idle":"2023-06-21T02:15:20.970279Z","shell.execute_reply.started":"2023-06-21T02:15:20.951605Z","shell.execute_reply":"2023-06-21T02:15:20.968424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.971878Z","iopub.execute_input":"2023-06-21T02:15:20.972280Z","iopub.status.idle":"2023-06-21T02:15:20.984245Z","shell.execute_reply.started":"2023-06-21T02:15:20.972243Z","shell.execute_reply":"2023-06-21T02:15:20.982367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:20.986062Z","iopub.execute_input":"2023-06-21T02:15:20.986615Z","iopub.status.idle":"2023-06-21T02:15:20.997414Z","shell.execute_reply.started":"2023-06-21T02:15:20.986557Z","shell.execute_reply":"2023-06-21T02:15:20.995853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:21.001047Z","iopub.execute_input":"2023-06-21T02:15:21.001548Z","iopub.status.idle":"2023-06-21T02:15:21.011986Z","shell.execute_reply.started":"2023-06-21T02:15:21.001505Z","shell.execute_reply":"2023-06-21T02:15:21.010530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:21.013484Z","iopub.execute_input":"2023-06-21T02:15:21.014007Z","iopub.status.idle":"2023-06-21T02:15:21.032440Z","shell.execute_reply.started":"2023-06-21T02:15:21.013953Z","shell.execute_reply":"2023-06-21T02:15:21.031022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the training data to train the model\nmodel.fit(train_images, y,\n          validation_data = (x_val,y_val),\n          batch_size=100,\n          epochs=20,\n          verbose=1)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-21T02:15:21.034271Z","iopub.execute_input":"2023-06-21T02:15:21.035805Z","iopub.status.idle":"2023-06-21T03:05:44.223859Z","shell.execute_reply.started":"2023-06-21T02:15:21.035730Z","shell.execute_reply":"2023-06-21T03:05:44.221711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-21T03:05:44.225967Z","iopub.execute_input":"2023-06-21T03:05:44.226624Z","iopub.status.idle":"2023-06-21T03:06:08.756316Z","shell.execute_reply.started":"2023-06-21T03:05:44.226565Z","shell.execute_reply":"2023-06-21T03:06:08.755033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-21T03:06:08.758322Z","iopub.execute_input":"2023-06-21T03:06:08.759254Z","iopub.status.idle":"2023-06-21T03:06:08.768708Z","shell.execute_reply.started":"2023-06-21T03:06:08.759211Z","shell.execute_reply":"2023-06-21T03:06:08.767503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = np.argmax(preds_test,axis = 1)\n\nresults = pd.Series(preds_test,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-06-21T03:06:08.770131Z","iopub.execute_input":"2023-06-21T03:06:08.770637Z","iopub.status.idle":"2023-06-21T03:06:08.779863Z","shell.execute_reply.started":"2023-06-21T03:06:08.770575Z","shell.execute_reply":"2023-06-21T03:06:08.778393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': test_id,\n                       'Label': preds_test})\n\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-21T03:06:08.781564Z","iopub.execute_input":"2023-06-21T03:06:08.782006Z","iopub.status.idle":"2023-06-21T03:06:08.846530Z","shell.execute_reply.started":"2023-06-21T03:06:08.781956Z","shell.execute_reply":"2023-06-21T03:06:08.845354Z"},"trusted":true},"execution_count":null,"outputs":[]}]}