{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-03-27T21:53:18.495511Z","iopub.status.busy":"2023-03-27T21:53:18.495135Z","iopub.status.idle":"2023-03-27T21:53:18.507737Z","shell.execute_reply":"2023-03-27T21:53:18.506345Z"},"papermill":{"duration":0.025269,"end_time":"2023-03-27T21:53:18.510567","exception":false,"start_time":"2023-03-27T21:53:18.485298","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{"papermill":{"duration":0.005973,"end_time":"2023-03-27T21:53:18.523088","exception":false,"start_time":"2023-03-27T21:53:18.517115","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:18.538102Z","iopub.status.busy":"2023-03-27T21:53:18.536860Z","iopub.status.idle":"2023-03-27T21:53:28.285666Z","shell.execute_reply":"2023-03-27T21:53:28.284134Z"},"papermill":{"duration":9.75907,"end_time":"2023-03-27T21:53:28.288329","exception":false,"start_time":"2023-03-27T21:53:18.529259","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Data","metadata":{"papermill":{"duration":0.005891,"end_time":"2023-03-27T21:53:28.300745","exception":false,"start_time":"2023-03-27T21:53:28.294854","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain.shape, test.shape","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:28.316018Z","iopub.status.busy":"2023-03-27T21:53:28.314830Z","iopub.status.idle":"2023-03-27T21:53:32.777715Z","shell.execute_reply":"2023-03-27T21:53:32.776789Z"},"papermill":{"duration":4.472617,"end_time":"2023-03-27T21:53:32.779662","exception":false,"start_time":"2023-03-27T21:53:28.307045","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split into subsets","metadata":{"papermill":{"duration":0.005826,"end_time":"2023-03-27T21:53:32.791721","exception":false,"start_time":"2023-03-27T21:53:32.785895","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:32.805696Z","iopub.status.busy":"2023-03-27T21:53:32.805137Z","iopub.status.idle":"2023-03-27T21:53:33.559758Z","shell.execute_reply":"2023-03-27T21:53:33.558688Z"},"papermill":{"duration":0.764434,"end_time":"2023-03-27T21:53:33.562278","exception":false,"start_time":"2023-03-27T21:53:32.797844","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train.iloc[:, 1:], train['label'], test_size=0.2, random_state=42)\nX_train.shape, X_val.shape, y_train.shape, y_val.shape","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:33.577365Z","iopub.status.busy":"2023-03-27T21:53:33.577011Z","iopub.status.idle":"2023-03-27T21:53:33.764697Z","shell.execute_reply":"2023-03-27T21:53:33.763244Z"},"papermill":{"duration":0.197881,"end_time":"2023-03-27T21:53:33.767351","exception":false,"start_time":"2023-03-27T21:53:33.569470","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalize","metadata":{"papermill":{"duration":0.006245,"end_time":"2023-03-27T21:53:33.780674","exception":false,"start_time":"2023-03-27T21:53:33.774429","status":"completed"},"tags":[]}},{"cell_type":"code","source":"X_train = X_train/255\nX_val = X_val/255\ntest = test/255","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:33.795207Z","iopub.status.busy":"2023-03-27T21:53:33.794824Z","iopub.status.idle":"2023-03-27T21:53:33.879086Z","shell.execute_reply":"2023-03-27T21:53:33.877989Z"},"papermill":{"duration":0.094446,"end_time":"2023-03-27T21:53:33.881592","exception":false,"start_time":"2023-03-27T21:53:33.787146","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling","metadata":{"papermill":{"duration":0.006058,"end_time":"2023-03-27T21:53:33.894348","exception":false,"start_time":"2023-03-27T21:53:33.888290","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:33.908460Z","iopub.status.busy":"2023-03-27T21:53:33.908107Z","iopub.status.idle":"2023-03-27T21:53:33.913521Z","shell.execute_reply":"2023-03-27T21:53:33.911894Z"},"papermill":{"duration":0.015193,"end_time":"2023-03-27T21:53:33.915830","exception":false,"start_time":"2023-03-27T21:53:33.900637","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_val = scaler.transform(X_val)\ntest = scaler.transform(test)","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:33.930414Z","iopub.status.busy":"2023-03-27T21:53:33.930063Z","iopub.status.idle":"2023-03-27T21:53:34.211364Z","shell.execute_reply":"2023-03-27T21:53:34.209746Z"},"papermill":{"duration":0.291853,"end_time":"2023-03-27T21:53:34.214238","exception":false,"start_time":"2023-03-27T21:53:33.922385","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One Hot Encoding","metadata":{"papermill":{"duration":0.006035,"end_time":"2023-03-27T21:53:34.226709","exception":false,"start_time":"2023-03-27T21:53:34.220674","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from keras.utils import to_categorical","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.241856Z","iopub.status.busy":"2023-03-27T21:53:34.241496Z","iopub.status.idle":"2023-03-27T21:53:34.247246Z","shell.execute_reply":"2023-03-27T21:53:34.245616Z"},"papermill":{"duration":0.016539,"end_time":"2023-03-27T21:53:34.249780","exception":false,"start_time":"2023-03-27T21:53:34.233241","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_val = to_categorical(y_val)","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.264224Z","iopub.status.busy":"2023-03-27T21:53:34.263858Z","iopub.status.idle":"2023-03-27T21:53:34.269962Z","shell.execute_reply":"2023-03-27T21:53:34.268763Z"},"papermill":{"duration":0.016316,"end_time":"2023-03-27T21:53:34.272671","exception":false,"start_time":"2023-03-27T21:53:34.256355","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reshape","metadata":{"papermill":{"duration":0.005951,"end_time":"2023-03-27T21:53:34.285442","exception":false,"start_time":"2023-03-27T21:53:34.279491","status":"completed"},"tags":[]}},{"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\nX_val = X_val.reshape(-1,28,28,1)\ntest = test.reshape(-1,28,28,1)\nX_train.shape, X_val.shape, y_train.shape, y_val.shape, test.shape","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.299459Z","iopub.status.busy":"2023-03-27T21:53:34.299119Z","iopub.status.idle":"2023-03-27T21:53:34.308358Z","shell.execute_reply":"2023-03-27T21:53:34.307114Z"},"papermill":{"duration":0.019203,"end_time":"2023-03-27T21:53:34.310891","exception":false,"start_time":"2023-03-27T21:53:34.291688","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.00599,"end_time":"2023-03-27T21:53:34.323625","exception":false,"start_time":"2023-03-27T21:53:34.317635","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.337546Z","iopub.status.busy":"2023-03-27T21:53:34.337188Z","iopub.status.idle":"2023-03-27T21:53:34.344080Z","shell.execute_reply":"2023-03-27T21:53:34.343374Z"},"papermill":{"duration":0.01623,"end_time":"2023-03-27T21:53:34.346000","exception":false,"start_time":"2023-03-27T21:53:34.329770","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.360761Z","iopub.status.busy":"2023-03-27T21:53:34.360381Z","iopub.status.idle":"2023-03-27T21:53:34.593236Z","shell.execute_reply":"2023-03-27T21:53:34.592017Z"},"papermill":{"duration":0.243477,"end_time":"2023-03-27T21:53:34.596082","exception":false,"start_time":"2023-03-27T21:53:34.352605","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{"papermill":{"duration":0.006073,"end_time":"2023-03-27T21:53:34.608858","exception":false,"start_time":"2023-03-27T21:53:34.602785","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.624536Z","iopub.status.busy":"2023-03-27T21:53:34.624142Z","iopub.status.idle":"2023-03-27T21:53:34.631460Z","shell.execute_reply":"2023-03-27T21:53:34.630082Z"},"papermill":{"duration":0.018138,"end_time":"2023-03-27T21:53:34.634228","exception":false,"start_time":"2023-03-27T21:53:34.616090","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.1, fill_mode='nearest')\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.650595Z","iopub.status.busy":"2023-03-27T21:53:34.648994Z","iopub.status.idle":"2023-03-27T21:53:34.692952Z","shell.execute_reply":"2023-03-27T21:53:34.691711Z"},"papermill":{"duration":0.054327,"end_time":"2023-03-27T21:53:34.695420","exception":false,"start_time":"2023-03-27T21:53:34.641093","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compile & Train Model","metadata":{"papermill":{"duration":0.006217,"end_time":"2023-03-27T21:53:34.708550","exception":false,"start_time":"2023-03-27T21:53:34.702333","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.724074Z","iopub.status.busy":"2023-03-27T21:53:34.722742Z","iopub.status.idle":"2023-03-27T21:53:34.739876Z","shell.execute_reply":"2023-03-27T21:53:34.739119Z"},"papermill":{"duration":0.027217,"end_time":"2023-03-27T21:53:34.742277","exception":false,"start_time":"2023-03-27T21:53:34.715060","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(datagen.flow(X_train, y_train, batch_size=128), epochs=12, validation_data=(X_val, y_val), verbose=1, steps_per_epoch=X_train.shape[0] // 128)","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:53:34.757188Z","iopub.status.busy":"2023-03-27T21:53:34.756517Z"},"papermill":{"duration":null,"end_time":null,"exception":false,"start_time":"2023-03-27T21:53:34.748892","status":"running"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_val, y_val)\nprint(f'Validation loss: {loss:.4f}')\nprint(f'Validation accuracy: {accuracy:.4f}')","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:46:56.274379Z","iopub.status.busy":"2023-03-27T21:46:56.274024Z","iopub.status.idle":"2023-03-27T21:47:01.453229Z","shell.execute_reply":"2023-03-27T21:47:01.452114Z","shell.execute_reply.started":"2023-03-27T21:46:56.274352Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"predictions = model.predict(test)\npredictions = np.argmax(predictions, axis=1)\npredictions","metadata":{"execution":{"iopub.execute_input":"2023-03-27T21:47:14.488014Z","iopub.status.busy":"2023-03-27T21:47:14.487663Z","iopub.status.idle":"2023-03-27T21:47:29.376677Z","shell.execute_reply":"2023-03-27T21:47:29.375391Z","shell.execute_reply.started":"2023-03-27T21:47:14.487989Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]}]}