{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.activations import linear, relu, sigmoid\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nimport matplotlib.pyplot as plt\n\nimport logging\nlogging.getLogger(\"tensorflow\").setLevel(logging.ERROR)\ntf.autograph.set_verbosity(0)\n\nnp.set_printoptions(precision=2)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-20T18:03:00.722745Z","iopub.execute_input":"2024-06-20T18:03:00.723231Z","iopub.status.idle":"2024-06-20T18:03:00.731364Z","shell.execute_reply.started":"2024-06-20T18:03:00.723192Z","shell.execute_reply":"2024-06-20T18:03:00.730237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Read dataset","metadata":{}},{"cell_type":"code","source":"train_dataset = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_dataset = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:33:19.466943Z","iopub.execute_input":"2024-06-20T17:33:19.468173Z","iopub.status.idle":"2024-06-20T17:33:23.99772Z","shell.execute_reply.started":"2024-06-20T17:33:19.468105Z","shell.execute_reply":"2024-06-20T17:33:23.99665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.columns","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:38:30.850751Z","iopub.execute_input":"2024-06-20T17:38:30.851204Z","iopub.status.idle":"2024-06-20T17:38:30.859432Z","shell.execute_reply.started":"2024-06-20T17:38:30.85114Z","shell.execute_reply":"2024-06-20T17:38:30.85811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features[1:]","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:37:35.023532Z","iopub.execute_input":"2024-06-20T17:37:35.024008Z","iopub.status.idle":"2024-06-20T17:37:35.032821Z","shell.execute_reply.started":"2024-06-20T17:37:35.023969Z","shell.execute_reply":"2024-06-20T17:37:35.031656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_dataset[features[1:]].values\nX.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:39:50.627779Z","iopub.execute_input":"2024-06-20T17:39:50.628115Z","iopub.status.idle":"2024-06-20T17:39:50.755674Z","shell.execute_reply.started":"2024-06-20T17:39:50.628086Z","shell.execute_reply":"2024-06-20T17:39:50.754583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_dataset[features[0]].values\ny.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:40:05.780996Z","iopub.execute_input":"2024-06-20T17:40:05.781901Z","iopub.status.idle":"2024-06-20T17:40:05.789296Z","shell.execute_reply.started":"2024-06-20T17:40:05.781864Z","shell.execute_reply":"2024-06-20T17:40:05.788225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Separate features columns from label column function ","metadata":{}},{"cell_type":"code","source":"def separate_features_and_label(dataset):\n    features = dataset.columns\n    X = dataset[features[1:]].values\n    y = dataset[features[0]].values\n    \n    return X, y","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:40:15.558121Z","iopub.execute_input":"2024-06-20T17:40:15.558618Z","iopub.status.idle":"2024-06-20T17:40:15.565504Z","shell.execute_reply.started":"2024-06-20T17:40:15.558586Z","shell.execute_reply":"2024-06-20T17:40:15.563918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y = separate_features_and_label(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:41:07.856427Z","iopub.execute_input":"2024-06-20T17:41:07.856909Z","iopub.status.idle":"2024-06-20T17:41:08.037753Z","shell.execute_reply.started":"2024-06-20T17:41:07.856873Z","shell.execute_reply":"2024-06-20T17:41:08.036417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape , type(X)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:58:40.155778Z","iopub.execute_input":"2024-06-20T17:58:40.156409Z","iopub.status.idle":"2024-06-20T17:58:40.164454Z","shell.execute_reply.started":"2024-06-20T17:58:40.156365Z","shell.execute_reply":"2024-06-20T17:58:40.162951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T17:41:15.232731Z","iopub.execute_input":"2024-06-20T17:41:15.233129Z","iopub.status.idle":"2024-06-20T17:41:15.241083Z","shell.execute_reply.started":"2024-06-20T17:41:15.233099Z","shell.execute_reply":"2024-06-20T17:41:15.239902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split the training examples into training and validation ","metadata":{}},{"cell_type":"code","source":"int(np.ceil((1-0.25) * len(X)))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:04:41.485194Z","iopub.execute_input":"2024-06-20T18:04:41.485663Z","iopub.status.idle":"2024-06-20T18:04:41.494671Z","shell.execute_reply.started":"2024-06-20T18:04:41.48563Z","shell.execute_reply":"2024-06-20T18:04:41.492974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_cv_split(X, y, cv_size):\n    \"\"\"\n    X: the array which contains the feature columns\n    y: the array which contains the label columns\n    cv_size: the percentage of splitting the dataset\n    \n    return: X_train, X_cv, y_train, y_cv \n    \n    \"\"\"\n    no_of_training = int(np.ceil((1-cv_size) * len(X)))\n    \n    X_train = X[0:no_of_training, :]\n    y_train = y[0:no_of_training]\n    \n    X_cv = X[no_of_training:, :]\n    y_cv = y[no_of_training:]\n    \n    return X_train, X_cv, y_train, y_cv","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:04:49.622292Z","iopub.execute_input":"2024-06-20T18:04:49.623484Z","iopub.status.idle":"2024-06-20T18:04:49.631781Z","shell.execute_reply.started":"2024-06-20T18:04:49.623433Z","shell.execute_reply":"2024-06-20T18:04:49.630253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_cv, y_train, y_cv = train_cv_split(X, y, 0.25)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:04:50.032033Z","iopub.execute_input":"2024-06-20T18:04:50.032956Z","iopub.status.idle":"2024-06-20T18:04:50.039108Z","shell.execute_reply.started":"2024-06-20T18:04:50.032911Z","shell.execute_reply":"2024-06-20T18:04:50.037622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:05:15.741944Z","iopub.execute_input":"2024-06-20T18:05:15.742375Z","iopub.status.idle":"2024-06-20T18:05:15.750609Z","shell.execute_reply.started":"2024-06-20T18:05:15.742344Z","shell.execute_reply":"2024-06-20T18:05:15.749076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_cv.shape, y_cv.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:05:20.719738Z","iopub.execute_input":"2024-06-20T18:05:20.720112Z","iopub.status.idle":"2024-06-20T18:05:20.727323Z","shell.execute_reply.started":"2024-06-20T18:05:20.720084Z","shell.execute_reply":"2024-06-20T18:05:20.726051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:07:48.870491Z","iopub.execute_input":"2024-06-20T18:07:48.870859Z","iopub.status.idle":"2024-06-20T18:07:48.877972Z","shell.execute_reply.started":"2024-06-20T18:07:48.870831Z","shell.execute_reply":"2024-06-20T18:07:48.876821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:09:14.002226Z","iopub.execute_input":"2024-06-20T18:09:14.002722Z","iopub.status.idle":"2024-06-20T18:09:14.01322Z","shell.execute_reply.started":"2024-06-20T18:09:14.002686Z","shell.execute_reply":"2024-06-20T18:09:14.011724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build the model","metadata":{}},{"cell_type":"code","source":"tf.random.set_seed(1234) # for consistent results\nmodel = Sequential(\n    [               \n        ### START CODE HERE ### \n        tf.keras.Input(shape=(784,)), \n        Dense(units=25, activation='relu', name='L1'),\n        Dense(units=15, activation='relu', name='L2'),\n        Dense(units=10, activation='linear', name='L3'),\n        \n        \n        ### END CODE HERE ### \n    ], name = \"my_model\" \n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:12:07.297289Z","iopub.execute_input":"2024-06-20T18:12:07.298435Z","iopub.status.idle":"2024-06-20T18:12:07.361239Z","shell.execute_reply.started":"2024-06-20T18:12:07.298389Z","shell.execute_reply":"2024-06-20T18:12:07.359687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:12:08.25977Z","iopub.execute_input":"2024-06-20T18:12:08.26028Z","iopub.status.idle":"2024-06-20T18:12:08.284554Z","shell.execute_reply.started":"2024-06-20T18:12:08.26024Z","shell.execute_reply":"2024-06-20T18:12:08.283498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    batch_size=64,\n    epochs=40,\n    validation_data=(X_cv, y_cv),\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:12:09.658758Z","iopub.execute_input":"2024-06-20T18:12:09.659149Z","iopub.status.idle":"2024-06-20T18:12:56.772318Z","shell.execute_reply.started":"2024-06-20T18:12:09.659119Z","shell.execute_reply":"2024-06-20T18:12:56.771207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the results","metadata":{}},{"cell_type":"code","source":"acc = history.history['sparse_categorical_accuracy']\nval_acc = history.history['val_sparse_categorical_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(40)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:16:03.76245Z","iopub.execute_input":"2024-06-20T18:16:03.762941Z","iopub.status.idle":"2024-06-20T18:16:04.273164Z","shell.execute_reply.started":"2024-06-20T18:16:03.762906Z","shell.execute_reply":"2024-06-20T18:16:04.272032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluate the model","metadata":{}},{"cell_type":"code","source":"test_features = test_dataset.columns","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:18:15.919242Z","iopub.execute_input":"2024-06-20T18:18:15.919701Z","iopub.status.idle":"2024-06-20T18:18:15.925661Z","shell.execute_reply.started":"2024-06-20T18:18:15.919668Z","shell.execute_reply":"2024-06-20T18:18:15.924063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(test_features)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:18:26.660612Z","iopub.execute_input":"2024-06-20T18:18:26.661617Z","iopub.status.idle":"2024-06-20T18:18:26.66887Z","shell.execute_reply.started":"2024-06-20T18:18:26.661577Z","shell.execute_reply":"2024-06-20T18:18:26.6675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_dataset[test_features[::]].values","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:18:55.314445Z","iopub.execute_input":"2024-06-20T18:18:55.314799Z","iopub.status.idle":"2024-06-20T18:18:55.386142Z","shell.execute_reply.started":"2024-06-20T18:18:55.314762Z","shell.execute_reply":"2024-06-20T18:18:55.384993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:19:01.216779Z","iopub.execute_input":"2024-06-20T18:19:01.217168Z","iopub.status.idle":"2024-06-20T18:19:01.224328Z","shell.execute_reply.started":"2024-06-20T18:19:01.217123Z","shell.execute_reply":"2024-06-20T18:19:01.223075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:20:43.24136Z","iopub.execute_input":"2024-06-20T18:20:43.241798Z","iopub.status.idle":"2024-06-20T18:20:45.056117Z","shell.execute_reply.started":"2024-06-20T18:20:43.241767Z","shell.execute_reply":"2024-06-20T18:20:45.055005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:21:51.276006Z","iopub.execute_input":"2024-06-20T18:21:51.276503Z","iopub.status.idle":"2024-06-20T18:21:51.287266Z","shell.execute_reply.started":"2024-06-20T18:21:51.276465Z","shell.execute_reply":"2024-06-20T18:21:51.285992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction[1]","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:22:48.105511Z","iopub.execute_input":"2024-06-20T18:22:48.106003Z","iopub.status.idle":"2024-06-20T18:22:48.113644Z","shell.execute_reply.started":"2024-06-20T18:22:48.105967Z","shell.execute_reply":"2024-06-20T18:22:48.112507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat = []\nfor i in range(len(prediction)):\n    prediction_p = tf.nn.softmax(prediction[i])\n    yhat.append(np.argmax(prediction_p))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:24:05.708132Z","iopub.execute_input":"2024-06-20T18:24:05.708626Z","iopub.status.idle":"2024-06-20T18:24:08.803228Z","shell.execute_reply.started":"2024-06-20T18:24:05.70859Z","shell.execute_reply":"2024-06-20T18:24:08.802056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\n\nwith open('/kaggle/working/file.csv', 'w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerow(yhat)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T18:27:16.316903Z","iopub.execute_input":"2024-06-20T18:27:16.317381Z","iopub.status.idle":"2024-06-20T18:27:16.32841Z","shell.execute_reply.started":"2024-06-20T18:27:16.317346Z","shell.execute_reply":"2024-06-20T18:27:16.327233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}