{"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-01-26T16:10:36.439514Z","iopub.execute_input":"2023-01-26T16:10:36.440755Z","iopub.status.idle":"2023-01-26T16:10:36.452004Z","shell.execute_reply.started":"2023-01-26T16:10:36.440705Z","shell.execute_reply":"2023-01-26T16:10:36.450585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport cv2\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:36.454858Z","iopub.execute_input":"2023-01-26T16:10:36.455786Z","iopub.status.idle":"2023-01-26T16:10:36.460635Z","shell.execute_reply.started":"2023-01-26T16:10:36.455749Z","shell.execute_reply":"2023-01-26T16:10:36.459754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load the data","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:36.462079Z","iopub.execute_input":"2023-01-26T16:10:36.462743Z","iopub.status.idle":"2023-01-26T16:10:39.819591Z","shell.execute_reply.started":"2023-01-26T16:10:36.462575Z","shell.execute_reply":"2023-01-26T16:10:39.818611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.822316Z","iopub.execute_input":"2023-01-26T16:10:39.822738Z","iopub.status.idle":"2023-01-26T16:10:39.844800Z","shell.execute_reply.started":"2023-01-26T16:10:39.822700Z","shell.execute_reply":"2023-01-26T16:10:39.843772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.846481Z","iopub.execute_input":"2023-01-26T16:10:39.846863Z","iopub.status.idle":"2023-01-26T16:10:39.868109Z","shell.execute_reply.started":"2023-01-26T16:10:39.846823Z","shell.execute_reply":"2023-01-26T16:10:39.867074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.869860Z","iopub.execute_input":"2023-01-26T16:10:39.870220Z","iopub.status.idle":"2023-01-26T16:10:39.877534Z","shell.execute_reply.started":"2023-01-26T16:10:39.870181Z","shell.execute_reply":"2023-01-26T16:10:39.876632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.878785Z","iopub.execute_input":"2023-01-26T16:10:39.880786Z","iopub.status.idle":"2023-01-26T16:10:39.887933Z","shell.execute_reply.started":"2023-01-26T16:10:39.880749Z","shell.execute_reply":"2023-01-26T16:10:39.886868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train_df.drop('label', axis=1)\ntarget = train_df.label","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.889286Z","iopub.execute_input":"2023-01-26T16:10:39.890086Z","iopub.status.idle":"2023-01-26T16:10:39.972597Z","shell.execute_reply.started":"2023-01-26T16:10:39.890049Z","shell.execute_reply":"2023-01-26T16:10:39.971612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.973868Z","iopub.execute_input":"2023-01-26T16:10:39.974863Z","iopub.status.idle":"2023-01-26T16:10:39.980764Z","shell.execute_reply.started":"2023-01-26T16:10:39.974828Z","shell.execute_reply":"2023-01-26T16:10:39.979823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:39.985333Z","iopub.execute_input":"2023-01-26T16:10:39.986291Z","iopub.status.idle":"2023-01-26T16:10:40.032235Z","shell.execute_reply.started":"2023-01-26T16:10:39.986255Z","shell.execute_reply":"2023-01-26T16:10:40.031122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the data","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\n\nsns.countplot(target)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:40.033811Z","iopub.execute_input":"2023-01-26T16:10:40.034268Z","iopub.status.idle":"2023-01-26T16:10:40.243465Z","shell.execute_reply.started":"2023-01-26T16:10:40.034233Z","shell.execute_reply":"2023-01-26T16:10:40.242539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-processing for CNN","metadata":{}},{"cell_type":"code","source":"train_reshaped = train.values.reshape(-1, 28, 28, 1)\ntrain_reshaped.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:40.244965Z","iopub.execute_input":"2023-01-26T16:10:40.245603Z","iopub.status.idle":"2023-01-26T16:10:40.253513Z","shell.execute_reply.started":"2023-01-26T16:10:40.245551Z","shell.execute_reply":"2023-01-26T16:10:40.252612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:40.255027Z","iopub.execute_input":"2023-01-26T16:10:40.255662Z","iopub.status.idle":"2023-01-26T16:10:40.263110Z","shell.execute_reply.started":"2023-01-26T16:10:40.255618Z","shell.execute_reply":"2023-01-26T16:10:40.262292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Target encoding\nfrom sklearn.preprocessing import OneHotEncoder\nfrom keras.utils.np_utils import to_categorical\n\ntarget_reshaped = to_categorical(target, 10)\ntarget_reshaped.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:40.264529Z","iopub.execute_input":"2023-01-26T16:10:40.265198Z","iopub.status.idle":"2023-01-26T16:10:40.273485Z","shell.execute_reply.started":"2023-01-26T16:10:40.265160Z","shell.execute_reply":"2023-01-26T16:10:40.272832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and test split","metadata":{}},{"cell_type":"code","source":"# train test split\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(train_reshaped, target_reshaped, test_size=0.2, random_state=42, stratify=target_reshaped)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:40.274773Z","iopub.execute_input":"2023-01-26T16:10:40.275801Z","iopub.status.idle":"2023-01-26T16:10:41.157641Z","shell.execute_reply.started":"2023-01-26T16:10:40.275765Z","shell.execute_reply":"2023-01-26T16:10:41.156677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.159240Z","iopub.execute_input":"2023-01-26T16:10:41.159624Z","iopub.status.idle":"2023-01-26T16:10:41.166702Z","shell.execute_reply.started":"2023-01-26T16:10:41.159582Z","shell.execute_reply":"2023-01-26T16:10:41.165616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.168707Z","iopub.execute_input":"2023-01-26T16:10:41.169429Z","iopub.status.idle":"2023-01-26T16:10:41.177504Z","shell.execute_reply.started":"2023-01-26T16:10:41.169387Z","shell.execute_reply":"2023-01-26T16:10:41.176609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.179318Z","iopub.execute_input":"2023-01-26T16:10:41.180049Z","iopub.status.idle":"2023-01-26T16:10:41.186243Z","shell.execute_reply.started":"2023-01-26T16:10:41.180010Z","shell.execute_reply":"2023-01-26T16:10:41.185313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_reshaped = test_df.values.reshape(-1, 28, 28, 1)\nX_test_reshaped.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.187853Z","iopub.execute_input":"2023-01-26T16:10:41.188489Z","iopub.status.idle":"2023-01-26T16:10:41.195748Z","shell.execute_reply.started":"2023-01-26T16:10:41.188449Z","shell.execute_reply":"2023-01-26T16:10:41.194837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling CNN","metadata":{}},{"cell_type":"markdown","source":"## First model CNN- model1","metadata":{}},{"cell_type":"markdown","source":"- with MaxPooling, Drropout, Flatten, Early stopping, callback\n- score in leaderboard Score: 0.99103","metadata":{}},{"cell_type":"code","source":"model1 = tf.keras.Sequential()\n\n# First layer\nmodel1.add(tf.keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu', input_shape=(28,28,1), padding='same' ))\nmodel1.add(tf.keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu', padding='same' ))\nmodel1.add(tf.keras.layers.Dropout(0.25))\nmodel1.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))\n\n# Second\nmodel1.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(5,5), activation='relu', padding='same' ) )\nmodel1.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(5,5), activation='relu', padding='same' ) )\nmodel1.add(tf.keras.layers.Dropout(0.25))\nmodel1.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))\n\n# Flatten\nmodel1.add(tf.keras.layers.Flatten())\nmodel1.add(tf.keras.layers.Dense(256, activation='relu' )) # 10 iba pri vystupnej vrstve, tu vacsie cislo (256)\nmodel1.add(tf.keras.layers.Dropout(0.25))\nmodel1.add(tf.keras.layers.Dense(10, activation='softmax' ))","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.197329Z","iopub.execute_input":"2023-01-26T16:10:41.198043Z","iopub.status.idle":"2023-01-26T16:10:41.267395Z","shell.execute_reply.started":"2023-01-26T16:10:41.198008Z","shell.execute_reply":"2023-01-26T16:10:41.266518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anhealer = tf.keras.callbacks.ReduceLROnPlateau(patience=3, factor=0.5, min_lr=0.0001) # Reduce learning rate when a metric has stopped improving.\nearly_stopping = tf.keras.callbacks.EarlyStopping(patience=3)\ncallback=[anhealer, early_stopping]","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.268899Z","iopub.execute_input":"2023-01-26T16:10:41.269253Z","iopub.status.idle":"2023-01-26T16:10:41.274323Z","shell.execute_reply.started":"2023-01-26T16:10:41.269219Z","shell.execute_reply":"2023-01-26T16:10:41.273399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['acc'])   \nmodel1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:10:41.275848Z","iopub.execute_input":"2023-01-26T16:10:41.276486Z","iopub.status.idle":"2023-01-26T16:10:41.290065Z","shell.execute_reply.started":"2023-01-26T16:10:41.276449Z","shell.execute_reply":"2023-01-26T16:10:41.288965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.fit(X_train, y_train,\n          validation_data = (X_val, y_val),\n          batch_size=124,\n          epochs=22,\n          callbacks=callback\n          ) # batch 64-128","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:33.785246Z","iopub.execute_input":"2023-01-26T16:19:33.785637Z","iopub.status.idle":"2023-01-26T16:19:47.199755Z","shell.execute_reply.started":"2023-01-26T16:19:33.785602Z","shell.execute_reply":"2023-01-26T16:19:47.198798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"pred1 = model1.predict(X_test_reshaped)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:56.702016Z","iopub.execute_input":"2023-01-26T16:19:56.702381Z","iopub.status.idle":"2023-01-26T16:19:58.409595Z","shell.execute_reply.started":"2023-01-26T16:19:56.702350Z","shell.execute_reply":"2023-01-26T16:19:58.408576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred1.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:58.414448Z","iopub.execute_input":"2023-01-26T16:19:58.414753Z","iopub.status.idle":"2023-01-26T16:19:58.423281Z","shell.execute_reply.started":"2023-01-26T16:19:58.414726Z","shell.execute_reply":"2023-01-26T16:19:58.422322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred1[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:58.425039Z","iopub.execute_input":"2023-01-26T16:19:58.425542Z","iopub.status.idle":"2023-01-26T16:19:58.432666Z","shell.execute_reply.started":"2023-01-26T16:19:58.425501Z","shell.execute_reply":"2023-01-26T16:19:58.431739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred1[0].argmax()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:58.434934Z","iopub.execute_input":"2023-01-26T16:19:58.435746Z","iopub.status.idle":"2023-01-26T16:19:58.442055Z","shell.execute_reply.started":"2023-01-26T16:19:58.435708Z","shell.execute_reply":"2023-01-26T16:19:58.441111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"#submit your work\n\nclasses = pred1.argmax(axis=-1)\n\nsub = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsub.Label = classes\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:58.443216Z","iopub.execute_input":"2023-01-26T16:19:58.444219Z","iopub.status.idle":"2023-01-26T16:19:58.462242Z","shell.execute_reply.started":"2023-01-26T16:19:58.444184Z","shell.execute_reply":"2023-01-26T16:19:58.461251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:58.463618Z","iopub.execute_input":"2023-01-26T16:19:58.464030Z","iopub.status.idle":"2023-01-26T16:19:58.493297Z","shell.execute_reply.started":"2023-01-26T16:19:58.463998Z","shell.execute_reply":"2023-01-26T16:19:58.492493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsub_df","metadata":{"execution":{"iopub.status.busy":"2023-01-26T16:19:58.494677Z","iopub.execute_input":"2023-01-26T16:19:58.495084Z","iopub.status.idle":"2023-01-26T16:19:58.512576Z","shell.execute_reply.started":"2023-01-26T16:19:58.495049Z","shell.execute_reply":"2023-01-26T16:19:58.511760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}