{"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\n# New Libraries!\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout\n\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-10-28T18:43:29.524261Z","iopub.execute_input":"2023-10-28T18:43:29.524692Z","iopub.status.idle":"2023-10-28T18:43:39.964741Z","shell.execute_reply.started":"2023-10-28T18:43:29.524656Z","shell.execute_reply":"2023-10-28T18:43:39.963127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read the Data:","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T18:45:22.558970Z","iopub.execute_input":"2023-10-28T18:45:22.559464Z","iopub.status.idle":"2023-10-28T18:45:25.424503Z","shell.execute_reply.started":"2023-10-28T18:45:22.559426Z","shell.execute_reply":"2023-10-28T18:45:25.423299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-28T18:46:50.771498Z","iopub.execute_input":"2023-10-28T18:46:50.772130Z","iopub.status.idle":"2023-10-28T18:46:50.778440Z","shell.execute_reply.started":"2023-10-28T18:46:50.772099Z","shell.execute_reply":"2023-10-28T18:46:50.777462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split into train, test, validation datasets","metadata":{}},{"cell_type":"code","source":"train = train_data[:33600]\nval = train_data[33600:37800]\ntest = train_data[37800:42000]\n\nprint(f\"Train dataset shape {train.shape}\")\nprint(f\"Validation dataset shape {val.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-28T18:50:30.423933Z","iopub.execute_input":"2023-10-28T18:50:30.424353Z","iopub.status.idle":"2023-10-28T18:50:30.432988Z","shell.execute_reply.started":"2023-10-28T18:50:30.424324Z","shell.execute_reply":"2023-10-28T18:50:30.431701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split the datasets into X and y","metadata":{}},{"cell_type":"code","source":"X_train = train.drop(\"label\", axis=1)  # Reading the images by pixels and not including the labels\ny_train = train[\"label\"]\n\nX_val = val.drop(\"label\", axis=1)  # Reading the images by pixels and not including the labels\ny_val = val[\"label\"]\n\nX_test = test.drop(\"label\", axis=1)  # Reading the images by pixels and not including the labels\ny_test = test[\"label\"]\n","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:12:44.088797Z","iopub.execute_input":"2023-10-28T19:12:44.089196Z","iopub.status.idle":"2023-10-28T19:12:44.155716Z","shell.execute_reply.started":"2023-10-28T19:12:44.089165Z","shell.execute_reply":"2023-10-28T19:12:44.154705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare the Model","metadata":{}},{"cell_type":"code","source":"# Keras sequential model with a list of layers as arguments\n\nmodel = Sequential([\n    # input layer\n    Input(shape=(784)),\n    \n    # hidden layers\n    Dense(16, activation=\"relu\"),\n    BatchNormalization(),\n    \n    Dense(16, activation=\"relu\"),\n    BatchNormalization(),\n    \n    Dense(16, activation=\"relu\"),\n    BatchNormalization(),\n    \n    Dropout(0.1),\n    \n    # Output layer\n    Dense(10, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:04:23.019130Z","iopub.execute_input":"2023-10-28T19:04:23.019525Z","iopub.status.idle":"2023-10-28T19:04:23.154524Z","shell.execute_reply.started":"2023-10-28T19:04:23.019500Z","shell.execute_reply":"2023-10-28T19:04:23.152552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:04:37.449129Z","iopub.execute_input":"2023-10-28T19:04:37.449517Z","iopub.status.idle":"2023-10-28T19:04:37.483832Z","shell.execute_reply.started":"2023-10-28T19:04:37.449488Z","shell.execute_reply":"2023-10-28T19:04:37.482685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=\"adam\", \n    loss=\"sparse_categorical_crossentropy\", \n    metrics=[\"accuracy\"]\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:10:24.219952Z","iopub.execute_input":"2023-10-28T19:10:24.220390Z","iopub.status.idle":"2023-10-28T19:10:24.233622Z","shell.execute_reply.started":"2023-10-28T19:10:24.220358Z","shell.execute_reply":"2023-10-28T19:10:24.231961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the data\n\nmodel.fit(\n    X_train,\n    y_train,\n    validation_data =(X_val, y_val),\n    epochs=10,\n    batch_size=128\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:12:57.065789Z","iopub.execute_input":"2023-10-28T19:12:57.066208Z","iopub.status.idle":"2023-10-28T19:13:12.575678Z","shell.execute_reply.started":"2023-10-28T19:12:57.066177Z","shell.execute_reply":"2023-10-28T19:13:12.574303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction = model.predict(X_test)\nanswer_label = test_prediction.argmax(axis=1)\n\nanswer_label\n\nfor i in range(25):\n    print(f\"Real value: {y_test.iloc[i]} vs Predicted value: {answer_label[i]}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:18:31.762091Z","iopub.execute_input":"2023-10-28T19:18:31.762477Z","iopub.status.idle":"2023-10-28T19:18:32.178780Z","shell.execute_reply.started":"2023-10-28T19:18:31.762448Z","shell.execute_reply":"2023-10-28T19:18:32.176925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = len(y_test)\ncorrect = 0\nwrong = 0\n\nfor i in range(total):\n    if y_test.iloc[i] == answer_label[i]:\n        correct += 1\n    else:\n        wrong += 1\n\nprint(correct, wrong, f'{int(correct/total *100)}% correct')","metadata":{"execution":{"iopub.status.busy":"2023-10-28T19:20:12.666652Z","iopub.execute_input":"2023-10-28T19:20:12.667064Z","iopub.status.idle":"2023-10-28T19:20:12.705365Z","shell.execute_reply.started":"2023-10-28T19:20:12.667033Z","shell.execute_reply":"2023-10-28T19:20:12.704408Z"},"trusted":true},"execution_count":null,"outputs":[]}]}