{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import lib\nimport numpy as np\nimport pandas as pd\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nimport tensorflow\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, ReLU\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.utils import plot_model","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:11.947595Z","iopub.execute_input":"2023-12-10T04:03:11.948446Z","iopub.status.idle":"2023-12-10T04:03:11.955264Z","shell.execute_reply.started":"2023-12-10T04:03:11.948412Z","shell.execute_reply":"2023-12-10T04:03:11.954080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Processing","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ndf_submit = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:11.957097Z","iopub.execute_input":"2023-12-10T04:03:11.957793Z","iopub.status.idle":"2023-12-10T04:03:16.036132Z","shell.execute_reply.started":"2023-12-10T04:03:11.957756Z","shell.execute_reply":"2023-12-10T04:03:16.035284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.037611Z","iopub.execute_input":"2023-12-10T04:03:16.037923Z","iopub.status.idle":"2023-12-10T04:03:16.055921Z","shell.execute_reply.started":"2023-12-10T04:03:16.037896Z","shell.execute_reply":"2023-12-10T04:03:16.054771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.057220Z","iopub.execute_input":"2023-12-10T04:03:16.057569Z","iopub.status.idle":"2023-12-10T04:03:16.077328Z","shell.execute_reply.started":"2023-12-10T04:03:16.057537Z","shell.execute_reply":"2023-12-10T04:03:16.076285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submit.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.080022Z","iopub.execute_input":"2023-12-10T04:03:16.080399Z","iopub.status.idle":"2023-12-10T04:03:16.088685Z","shell.execute_reply.started":"2023-12-10T04:03:16.080364Z","shell.execute_reply":"2023-12-10T04:03:16.087710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop(columns=['label']).values\ny = df_train[['label']].values","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.090102Z","iopub.execute_input":"2023-12-10T04:03:16.090417Z","iopub.status.idle":"2023-12-10T04:03:16.176931Z","shell.execute_reply.started":"2023-12-10T04:03:16.090389Z","shell.execute_reply":"2023-12-10T04:03:16.176038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.178766Z","iopub.execute_input":"2023-12-10T04:03:16.179184Z","iopub.status.idle":"2023-12-10T04:03:16.185641Z","shell.execute_reply.started":"2023-12-10T04:03:16.179146Z","shell.execute_reply":"2023-12-10T04:03:16.184738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.186993Z","iopub.execute_input":"2023-12-10T04:03:16.187323Z","iopub.status.idle":"2023-12-10T04:03:16.195198Z","shell.execute_reply.started":"2023-12-10T04:03:16.187294Z","shell.execute_reply":"2023-12-10T04:03:16.194192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scaling data bringing the values between 0 to 1\nX = X/255","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.196359Z","iopub.execute_input":"2023-12-10T04:03:16.196715Z","iopub.status.idle":"2023-12-10T04:03:16.308086Z","shell.execute_reply.started":"2023-12-10T04:03:16.196680Z","shell.execute_reply":"2023-12-10T04:03:16.307002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape images to 28x28\nX = X.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.309376Z","iopub.execute_input":"2023-12-10T04:03:16.309653Z","iopub.status.idle":"2023-12-10T04:03:16.314477Z","shell.execute_reply.started":"2023-12-10T04:03:16.309629Z","shell.execute_reply":"2023-12-10T04:03:16.313295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=df_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.318088Z","iopub.execute_input":"2023-12-10T04:03:16.318415Z","iopub.status.idle":"2023-12-10T04:03:16.324424Z","shell.execute_reply.started":"2023-12-10T04:03:16.318389Z","shell.execute_reply":"2023-12-10T04:03:16.323393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use OneHotEncoder to encode target column\ny_e = to_categorical(y, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.325709Z","iopub.execute_input":"2023-12-10T04:03:16.326072Z","iopub.status.idle":"2023-12-10T04:03:16.335070Z","shell.execute_reply.started":"2023-12-10T04:03:16.326043Z","shell.execute_reply":"2023-12-10T04:03:16.334264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization","metadata":{}},{"cell_type":"code","source":"# Bar chart with labels and color gradient\n\nunique_train, counts_train = np.unique(y, return_counts=True)\n\nsns.barplot(x=unique_train, y=counts_train, palette=\"viridis\")\n\n# labeling the pliot\nplt.xlabel('Label')\nplt.ylabel('Count')\nplt.title('Train Data')\n\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.336223Z","iopub.execute_input":"2023-12-10T04:03:16.336523Z","iopub.status.idle":"2023-12-10T04:03:16.562657Z","shell.execute_reply.started":"2023-12-10T04:03:16.336489Z","shell.execute_reply":"2023-12-10T04:03:16.561752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data seems to be distributes evenly without any Bias in the data","metadata":{}},{"cell_type":"code","source":"first_9_digits = X[0:9]\n# Plotting a images\nfig, ax = plt.subplots(3,3,figsize=(10,6))\n\nax[0,0].imshow(first_9_digits[0], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[0,1].imshow(first_9_digits[1], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[0,2].imshow(first_9_digits[2], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[1,0].imshow(first_9_digits[3], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[1,1].imshow(first_9_digits[4], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[1,2].imshow(first_9_digits[5], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[2,0].imshow(first_9_digits[6], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[2,1].imshow(first_9_digits[7], cmap = matplotlib.cm.binary, interpolation = 'nearest')\nax[2,2].imshow(first_9_digits[8], cmap = matplotlib.cm.binary, interpolation = 'nearest')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:16.563874Z","iopub.execute_input":"2023-12-10T04:03:16.564144Z","iopub.status.idle":"2023-12-10T04:03:17.730478Z","shell.execute_reply.started":"2023-12-10T04:03:16.564120Z","shell.execute_reply":"2023-12-10T04:03:17.729494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # CNN\n\n Defines a Convolutional Neural Network (CNN) for classification","metadata":{}},{"cell_type":"code","source":"# CNN Model with layers\ncnnModel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # Convolutional layer with 32 output\n\n    Conv2D(32, (3, 3), activation='relu'), # Convolutional layer with 32 output channels\n\n    MaxPooling2D((2,2)), # Max pooling layer to reduce the spatial dimensions\n\n    Conv2D(64, (3, 3), activation='relu'),  # Convolutional layer with 64 output channels\n\n    Conv2D(64, (3, 3), activation='relu'),  # Another convolutional layer with 64 output channels\n\n    MaxPooling2D((2,2)),  # Max pooling layer to reduce the spatial dimensions\n\n    Flatten(), # Flatten the output for input to fully connected layers\n\n    Dropout(0.5),  # Dropout layer to prevent overfitting\n\n    Dense(512, activation='relu'),  # Another fully connected layer with 512 output units\n\n    Dropout(0.5), # Dropout layer to prevent overfitting\n\n    Dense(10, activation='softmax')  # Final fully connected layer with 10 output units (corresponding to classes)\n\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:17.731531Z","iopub.execute_input":"2023-12-10T04:03:17.731825Z","iopub.status.idle":"2023-12-10T04:03:17.821755Z","shell.execute_reply.started":"2023-12-10T04:03:17.731799Z","shell.execute_reply":"2023-12-10T04:03:17.821013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Working\n\n1. **Convolutional Layers**:\n   - `Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))`: First convolutional layer with 32 filters, each using a 3x3 kernel, ReLU activation function, and an input shape of (28, 28, 1) representing grayscale images.\n   - `Conv2D(32, (3, 3), activation='relu')`: Second convolutional layer with 32 filters and ReLU activation.\n   - `Conv2D(64, (3, 3), activation='relu')`: Third convolutional layer with 64 filters and ReLU activation.\n   - `Conv2D(64, (3, 3), activation='relu')`: Fourth convolutional layer with 64 filters and ReLU activation.\n\n2. **Pooling Layers**:\n   - `MaxPooling2D((2, 2))`: Max pooling layers to reduce spatial dimensions by half after certain convolutional layers.\n\n3. **Flatten Layer**:\n   - `Flatten()`: Layer to flatten the output of the convolutional layers into a 1D array for input to the fully connected layers.\n\n4. **Dropout Layers**:\n   - `Dropout(0.5)`: Dropout layers with a dropout rate of 0.5, intended to prevent overfitting by randomly dropping 50% of input units during training.\n\n5. **Dense (Fully Connected) Layers**:\n   - `Dense(512, activation='relu')`: Fully connected layer with 512 neurons and ReLU activation.\n   - `Dense(10, activation='softmax')`: Final fully connected layer with 10 neurons, representing classes in the dataset, and using softmax activation for multi-class classification.","metadata":{}},{"cell_type":"code","source":"# specifying optimizer, loss function, and metrics\ncnnModel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:17.822752Z","iopub.execute_input":"2023-12-10T04:03:17.823011Z","iopub.status.idle":"2023-12-10T04:03:17.834080Z","shell.execute_reply.started":"2023-12-10T04:03:17.822987Z","shell.execute_reply":"2023-12-10T04:03:17.833279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(cnnModel, to_file='model_graph.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:17.835360Z","iopub.execute_input":"2023-12-10T04:03:17.836179Z","iopub.status.idle":"2023-12-10T04:03:17.943483Z","shell.execute_reply.started":"2023-12-10T04:03:17.836144Z","shell.execute_reply":"2023-12-10T04:03:17.942613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelHistory = cnnModel.fit(X, y_e, epochs=10, batch_size=16, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:03:17.944621Z","iopub.execute_input":"2023-12-10T04:03:17.944901Z","iopub.status.idle":"2023-12-10T04:04:54.987179Z","shell.execute_reply.started":"2023-12-10T04:03:17.944875Z","shell.execute_reply":"2023-12-10T04:04:54.986400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training accuracy and loss\nplt.plot(modelHistory.history['accuracy'])\nplt.plot(modelHistory.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\nplt.plot(modelHistory.history['loss'])\nplt.plot(modelHistory.history['val_loss'])\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Validation'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:04:54.988631Z","iopub.execute_input":"2023-12-10T04:04:54.989322Z","iopub.status.idle":"2023-12-10T04:04:55.466445Z","shell.execute_reply.started":"2023-12-10T04:04:54.989282Z","shell.execute_reply":"2023-12-10T04:04:55.465469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = cnnModel.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:04:55.467833Z","iopub.execute_input":"2023-12-10T04:04:55.468133Z","iopub.status.idle":"2023-12-10T04:04:57.885038Z","shell.execute_reply.started":"2023-12-10T04:04:55.468101Z","shell.execute_reply":"2023-12-10T04:04:57.884192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(results)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:04:57.886181Z","iopub.execute_input":"2023-12-10T04:04:57.886505Z","iopub.status.idle":"2023-12-10T04:04:57.892695Z","shell.execute_reply.started":"2023-12-10T04:04:57.886478Z","shell.execute_reply":"2023-12-10T04:04:57.891847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.argmax(results, axis=1)\nlabels","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:04:57.894139Z","iopub.execute_input":"2023-12-10T04:04:57.894872Z","iopub.status.idle":"2023-12-10T04:04:57.904478Z","shell.execute_reply.started":"2023-12-10T04:04:57.894836Z","shell.execute_reply":"2023-12-10T04:04:57.903508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result = pd.DataFrame({'Label': labels}, index=np.arange(1, len(labels) + 1))\ndf_result","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:10:03.066384Z","iopub.execute_input":"2023-12-10T04:10:03.066796Z","iopub.status.idle":"2023-12-10T04:10:03.078593Z","shell.execute_reply.started":"2023-12-10T04:10:03.066766Z","shell.execute_reply":"2023-12-10T04:10:03.077611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result.index.name = 'ImageId'","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:10:05.225321Z","iopub.execute_input":"2023-12-10T04:10:05.225704Z","iopub.status.idle":"2023-12-10T04:10:05.230426Z","shell.execute_reply.started":"2023-12-10T04:10:05.225667Z","shell.execute_reply":"2023-12-10T04:10:05.229272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:10:06.483407Z","iopub.execute_input":"2023-12-10T04:10:06.483786Z","iopub.status.idle":"2023-12-10T04:10:06.492760Z","shell.execute_reply.started":"2023-12-10T04:10:06.483757Z","shell.execute_reply":"2023-12-10T04:10:06.491524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result.to_csv('submission_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-10T04:10:16.286508Z","iopub.execute_input":"2023-12-10T04:10:16.286884Z","iopub.status.idle":"2023-12-10T04:10:16.355085Z","shell.execute_reply.started":"2023-12-10T04:10:16.286855Z","shell.execute_reply":"2023-12-10T04:10:16.354275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}