{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-19T10:03:16.999623Z","iopub.execute_input":"2023-05-19T10:03:17.000052Z","iopub.status.idle":"2023-05-19T10:03:17.005054Z","shell.execute_reply.started":"2023-05-19T10:03:17.000016Z","shell.execute_reply":"2023-05-19T10:03:17.004069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digit_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndigit_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:10:44.679938Z","iopub.execute_input":"2023-05-19T10:10:44.680586Z","iopub.status.idle":"2023-05-19T10:10:50.596910Z","shell.execute_reply.started":"2023-05-19T10:10:44.680554Z","shell.execute_reply":"2023-05-19T10:10:50.595455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digit_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:18:19.987836Z","iopub.execute_input":"2023-05-19T10:18:19.988240Z","iopub.status.idle":"2023-05-19T10:18:20.007554Z","shell.execute_reply.started":"2023-05-19T10:18:19.988208Z","shell.execute_reply":"2023-05-19T10:18:20.006389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns \n\n# Get the label counts from the DataFrame\nlabel_counts = digit_train['label'].value_counts()\n\n# Create a bar chart\nplt.bar(label_counts.index, label_counts.values)\n\n# Set labels and title\nplt.xlabel('Label')\nplt.ylabel('Count')\nplt.style.use('Solarize_Light2')\nplt.title('Distribution of Labels')\n\n# Set x-axis ticks\nplt.xticks(list(label_counts.index))\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:04:54.852665Z","iopub.execute_input":"2023-05-19T10:04:54.853053Z","iopub.status.idle":"2023-05-19T10:04:55.914753Z","shell.execute_reply.started":"2023-05-19T10:04:54.853025Z","shell.execute_reply":"2023-05-19T10:04:55.913629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(df,digit_num,visualize=False,is_test=False):\n    raw_data = df.loc[digit_num,]\n    if is_test == False:\n        label = raw_data[0]\n        filtred_image = raw_data[1:]\n    else:\n        filtred_image = raw_data\n    reshaped_image = filtred_image.values.reshape((28,28))\n    if visualize==True:\n        plt.imshow(reshaped_image, cmap='gray')\n        if is_test == False:\n            plt.title(f'Label: {label}')\n        plt.show()\n    else:\n        return reshaped_image, label","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:13:23.086100Z","iopub.execute_input":"2023-05-19T11:13:23.089024Z","iopub.status.idle":"2023-05-19T11:13:23.100614Z","shell.execute_reply.started":"2023-05-19T11:13:23.088945Z","shell.execute_reply":"2023-05-19T11:13:23.099004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(digit_train,1,visualize=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:13:23.483731Z","iopub.execute_input":"2023-05-19T11:13:23.484129Z","iopub.status.idle":"2023-05-19T11:13:23.745101Z","shell.execute_reply.started":"2023-05-19T11:13:23.484101Z","shell.execute_reply":"2023-05-19T11:13:23.744249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transformer(df,is_test=False):\n    if is_test== False:\n        y= df[\"label\"]\n        X = df.loc[:,\"pixel0\":\"pixel783\"]\n        X = X.apply(lambda x : x/255)\n        X = X.values.reshape(-1, 28, 28, 1)\n        return X, y\n    else:\n        X = df.apply(lambda x: x/255)\n        X = X.values.reshape(-1, 28, 28, 1)\n        return X","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:30:52.068982Z","iopub.execute_input":"2023-05-19T10:30:52.069360Z","iopub.status.idle":"2023-05-19T10:30:52.079479Z","shell.execute_reply.started":"2023-05-19T10:30:52.069331Z","shell.execute_reply":"2023-05-19T10:30:52.078006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = transformer(digit_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:37:36.879006Z","iopub.execute_input":"2023-05-19T10:37:36.879461Z","iopub.status.idle":"2023-05-19T10:37:37.232935Z","shell.execute_reply.started":"2023-05-19T10:37:36.879422Z","shell.execute_reply":"2023-05-19T10:37:37.231768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = transformer(digit_test,is_test=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:30:39.756695Z","iopub.execute_input":"2023-05-19T10:30:39.757155Z","iopub.status.idle":"2023-05-19T10:30:40.079164Z","shell.execute_reply.started":"2023-05-19T10:30:39.757119Z","shell.execute_reply":"2023-05-19T10:30:40.077721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T10:37:41.736429Z","iopub.execute_input":"2023-05-19T10:37:41.737278Z","iopub.status.idle":"2023-05-19T10:37:42.156683Z","shell.execute_reply.started":"2023-05-19T10:37:41.737240Z","shell.execute_reply":"2023-05-19T10:37:42.155458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epoch, logs={}):\n        if(logs.get('loss')<0.02):\n            print(\"\\nLoss is low so cancelling training!\")\n            self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:00:24.117065Z","iopub.execute_input":"2023-05-19T11:00:24.117485Z","iopub.status.idle":"2023-05-19T11:00:24.124984Z","shell.execute_reply.started":"2023-05-19T11:00:24.117456Z","shell.execute_reply":"2023-05-19T11:00:24.123336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = myCallback()\nmodel = tf.keras.models.Sequential([\ntf.keras.layers.Conv2D(64, (3,3), activation='relu',\ninput_shape=(28, 28, 1)),\ntf.keras.layers.MaxPooling2D(2, 2),\ntf.keras.layers.Conv2D(64, (3,3), activation='relu'),\ntf.keras.layers.MaxPooling2D(2,2),\ntf.keras.layers.Flatten(),\ntf.keras.layers.Dense(128, activation='relu'),\ntf.keras.layers.Dropout(0.2),\ntf.keras.layers.Dense(10, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:00:24.818824Z","iopub.execute_input":"2023-05-19T11:00:24.819244Z","iopub.status.idle":"2023-05-19T11:00:24.906701Z","shell.execute_reply.started":"2023-05-19T11:00:24.819203Z","shell.execute_reply":"2023-05-19T11:00:24.905347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:00:25.328687Z","iopub.execute_input":"2023-05-19T11:00:25.329065Z","iopub.status.idle":"2023-05-19T11:00:25.364021Z","shell.execute_reply.started":"2023-05-19T11:00:25.329038Z","shell.execute_reply":"2023-05-19T11:00:25.362723Z"},"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-05-19T11:00:25.791645Z","iopub.execute_input":"2023-05-19T11:00:25.792056Z","iopub.status.idle":"2023-05-19T11:00:25.806194Z","shell.execute_reply.started":"2023-05-19T11:00:25.792023Z","shell.execute_reply":"2023-05-19T11:00:25.805199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    x_train,\n    y_train,\n    epochs=20,\n    validation_data=(X_val, y_val),\n    callbacks=[callbacks]\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:00:27.492215Z","iopub.execute_input":"2023-05-19T11:00:27.492680Z","iopub.status.idle":"2023-05-19T11:03:59.531039Z","shell.execute_reply.started":"2023-05-19T11:00:27.492644Z","shell.execute_reply":"2023-05-19T11:03:59.529628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform predictions for all rows of X_test at once\npredictions = model.predict(X_test, verbose=0)\n\n# Get the predicted class labels by finding the indices of the maximum values\npredicted_labels = np.argmax(predictions, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:04:10.236696Z","iopub.execute_input":"2023-05-19T11:04:10.239287Z","iopub.status.idle":"2023-05-19T11:04:18.141589Z","shell.execute_reply.started":"2023-05-19T11:04:10.239242Z","shell.execute_reply":"2023-05-19T11:04:18.140070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_row_reshaper(df, row_number, visualize=False, is_test = False):\n    '''\n        Parameters:\n            df --> Pandas dataframe type\n            row_number --> row index (For example: 0, 1, 2, 3, etc.)\n            visualize --> If True then displays the raw data into 28 by 28 pixels image.\n                          If False then returns '28 by 28 pixels reshaped image data' and 'corresponding label'\n    '''\n    raw_image_data = df.loc[row_number,] #Taking user given image from the dataframe\n    \n    if is_test == False:\n        image_label = raw_image_data[0] #This is the label of the current particular image\n        filtered_image_data = raw_image_data[1:] #taking data after the first value as the first value is label\n    else:\n        filtered_image_data = raw_image_data\n        \n    reshaped_image_data = filtered_image_data.values.reshape((28,28)) #Reshaping the image into 28 by 28 pixes\n    \n    if visualize==True:\n        plt.imshow(reshaped_image_data, cmap='gray')\n        if is_test == False:\n            plt.title(f'Label: {image_label}')\n        plt.show()\n    else:\n        return reshaped_image_data, image_label","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:10:56.741081Z","iopub.execute_input":"2023-05-19T11:10:56.742091Z","iopub.status.idle":"2023-05-19T11:10:56.749821Z","shell.execute_reply.started":"2023-05-19T11:10:56.742055Z","shell.execute_reply":"2023-05-19T11:10:56.748877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_model(row):\n    image_row_reshaper(digit_test, row,visualize=True, is_test = True)\n    print(f'Predicted Label: {predicted_labels[row]}')","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:10:57.489467Z","iopub.execute_input":"2023-05-19T11:10:57.490096Z","iopub.status.idle":"2023-05-19T11:10:57.494851Z","shell.execute_reply.started":"2023-05-19T11:10:57.490064Z","shell.execute_reply":"2023-05-19T11:10:57.493884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_model(77)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:11:14.930499Z","iopub.execute_input":"2023-05-19T11:11:14.930949Z","iopub.status.idle":"2023-05-19T11:11:15.160112Z","shell.execute_reply.started":"2023-05-19T11:11:14.930916Z","shell.execute_reply":"2023-05-19T11:11:15.158850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:26:50.020272Z","iopub.execute_input":"2023-05-19T11:26:50.020951Z","iopub.status.idle":"2023-05-19T11:26:50.038092Z","shell.execute_reply.started":"2023-05-19T11:26:50.020910Z","shell.execute_reply":"2023-05-19T11:26:50.036977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = sample_sub.drop('Label', axis=1)\nsample_sub['Label'] = list(predicted_labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:27:33.780287Z","iopub.execute_input":"2023-05-19T11:27:33.780699Z","iopub.status.idle":"2023-05-19T11:27:33.934687Z","shell.execute_reply.started":"2023-05-19T11:27:33.780668Z","shell.execute_reply":"2023-05-19T11:27:33.933438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:27:38.336653Z","iopub.execute_input":"2023-05-19T11:27:38.337075Z","iopub.status.idle":"2023-05-19T11:27:38.350531Z","shell.execute_reply.started":"2023-05-19T11:27:38.337041Z","shell.execute_reply":"2023-05-19T11:27:38.349498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.to_csv('./submission.csv', index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T11:28:22.479603Z","iopub.execute_input":"2023-05-19T11:28:22.480026Z","iopub.status.idle":"2023-05-19T11:28:22.557445Z","shell.execute_reply.started":"2023-05-19T11:28:22.479991Z","shell.execute_reply":"2023-05-19T11:28:22.556130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}