{"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-03-27T21:30:27.710132Z","iopub.execute_input":"2023-03-27T21:30:27.710451Z","iopub.status.idle":"2023-03-27T21:30:27.718595Z","shell.execute_reply.started":"2023-03-27T21:30:27.710425Z","shell.execute_reply":"2023-03-27T21:30:27.717472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:30:28.493095Z","iopub.execute_input":"2023-03-27T21:30:28.49344Z","iopub.status.idle":"2023-03-27T21:30:28.499315Z","shell.execute_reply.started":"2023-03-27T21:30:28.493413Z","shell.execute_reply":"2023-03-27T21:30:28.497724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Data","metadata":{}},{"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.status.busy":"2023-03-27T21:30:29.133217Z","iopub.execute_input":"2023-03-27T21:30:29.133785Z","iopub.status.idle":"2023-03-27T21:30:31.929712Z","shell.execute_reply.started":"2023-03-27T21:30:29.133737Z","shell.execute_reply":"2023-03-27T21:30:31.928134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split into subsets","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:30:31.932037Z","iopub.execute_input":"2023-03-27T21:30:31.932399Z","iopub.status.idle":"2023-03-27T21:30:31.938718Z","shell.execute_reply.started":"2023-03-27T21:30:31.932371Z","shell.execute_reply":"2023-03-27T21:30:31.937727Z"},"trusted":true},"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.status.busy":"2023-03-27T21:30:31.940312Z","iopub.execute_input":"2023-03-27T21:30:31.940909Z","iopub.status.idle":"2023-03-27T21:30:32.183906Z","shell.execute_reply.started":"2023-03-27T21:30:31.940873Z","shell.execute_reply":"2023-03-27T21:30:32.182915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalize","metadata":{}},{"cell_type":"code","source":"X_train = X_train/255\nX_val = X_val/255\ntest = test/255","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:30:32.185834Z","iopub.execute_input":"2023-03-27T21:30:32.186086Z","iopub.status.idle":"2023-03-27T21:30:32.275665Z","shell.execute_reply.started":"2023-03-27T21:30:32.18606Z","shell.execute_reply":"2023-03-27T21:30:32.274235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:30:32.276827Z","iopub.execute_input":"2023-03-27T21:30:32.277157Z","iopub.status.idle":"2023-03-27T21:30:32.282444Z","shell.execute_reply.started":"2023-03-27T21:30:32.277122Z","shell.execute_reply":"2023-03-27T21:30:32.280924Z"},"trusted":true},"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.status.busy":"2023-03-27T21:30:32.283971Z","iopub.execute_input":"2023-03-27T21:30:32.284281Z","iopub.status.idle":"2023-03-27T21:30:32.670056Z","shell.execute_reply.started":"2023-03-27T21:30:32.284253Z","shell.execute_reply":"2023-03-27T21:30:32.668471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One Hot Encoding","metadata":{}},{"cell_type":"code","source":"from keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:30:32.672094Z","iopub.execute_input":"2023-03-27T21:30:32.672542Z","iopub.status.idle":"2023-03-27T21:30:32.6787Z","shell.execute_reply.started":"2023-03-27T21:30:32.672502Z","shell.execute_reply":"2023-03-27T21:30:32.677387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_val = to_categorical(y_val)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:30:32.68025Z","iopub.execute_input":"2023-03-27T21:30:32.6806Z","iopub.status.idle":"2023-03-27T21:30:32.690448Z","shell.execute_reply.started":"2023-03-27T21:30:32.680564Z","shell.execute_reply":"2023-03-27T21:30:32.68907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reshape","metadata":{}},{"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.status.busy":"2023-03-27T21:30:32.693037Z","iopub.execute_input":"2023-03-27T21:30:32.69337Z","iopub.status.idle":"2023-03-27T21:30:32.707583Z","shell.execute_reply.started":"2023-03-27T21:30:32.693343Z","shell.execute_reply":"2023-03-27T21:30:32.706779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:31:24.335914Z","iopub.execute_input":"2023-03-27T21:31:24.336302Z","iopub.status.idle":"2023-03-27T21:31:24.34293Z","shell.execute_reply.started":"2023-03-27T21:31:24.336273Z","shell.execute_reply":"2023-03-27T21:31:24.341176Z"},"trusted":true},"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.status.busy":"2023-03-27T21:31:02.583531Z","iopub.execute_input":"2023-03-27T21:31:02.58458Z","iopub.status.idle":"2023-03-27T21:31:02.85986Z","shell.execute_reply.started":"2023-03-27T21:31:02.584549Z","shell.execute_reply":"2023-03-27T21:31:02.856105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:31:28.397878Z","iopub.execute_input":"2023-03-27T21:31:28.398212Z","iopub.status.idle":"2023-03-27T21:31:28.404536Z","shell.execute_reply.started":"2023-03-27T21:31:28.398185Z","shell.execute_reply":"2023-03-27T21:31:28.402915Z"},"trusted":true},"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.status.busy":"2023-03-27T21:31:28.684522Z","iopub.execute_input":"2023-03-27T21:31:28.68488Z","iopub.status.idle":"2023-03-27T21:31:28.731707Z","shell.execute_reply.started":"2023-03-27T21:31:28.684854Z","shell.execute_reply":"2023-03-27T21:31:28.73098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compile & Train Model","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:32:13.454049Z","iopub.execute_input":"2023-03-27T21:32:13.454418Z","iopub.status.idle":"2023-03-27T21:32:13.472212Z","shell.execute_reply.started":"2023-03-27T21:32:13.454388Z","shell.execute_reply":"2023-03-27T21:32:13.470748Z"},"trusted":true},"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.status.busy":"2023-03-27T21:32:20.891954Z","iopub.execute_input":"2023-03-27T21:32:20.892309Z","iopub.status.idle":"2023-03-27T21:46:26.073779Z","shell.execute_reply.started":"2023-03-27T21:32:20.892281Z","shell.execute_reply":"2023-03-27T21:46:26.072969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"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.status.busy":"2023-03-27T21:46:56.274024Z","iopub.execute_input":"2023-03-27T21:46:56.274379Z","iopub.status.idle":"2023-03-27T21:47:01.453229Z","shell.execute_reply.started":"2023-03-27T21:46:56.274352Z","shell.execute_reply":"2023-03-27T21:47:01.452114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test)\npredictions = np.argmax(predictions, axis=1)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-03-27T21:47:14.487663Z","iopub.execute_input":"2023-03-27T21:47:14.488014Z","iopub.status.idle":"2023-03-27T21:47:29.376677Z","shell.execute_reply.started":"2023-03-27T21:47:14.487989Z","shell.execute_reply":"2023-03-27T21:47:29.375391Z"},"trusted":true},"execution_count":null,"outputs":[]}]}