{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30822,"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\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","trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:08:10.904942Z","iopub.execute_input":"2024-12-22T14:08:10.905175Z","iopub.status.idle":"2024-12-22T14:08:11.316404Z","shell.execute_reply.started":"2024-12-22T14:08:10.905152Z","shell.execute_reply":"2024-12-22T14:08:11.315332Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Data Manipulation and File Handling\nimport pandas as pd\nimport numpy as np\n\n# Visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Machine Learning\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score, confusion_matrix\n\n# Deep Learning (if using neural networks)\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.utils import to_categorical\n\n# Utility\nimport os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:08:37.133820Z","iopub.execute_input":"2024-12-22T14:08:37.134219Z","iopub.status.idle":"2024-12-22T14:08:46.563605Z","shell.execute_reply.started":"2024-12-22T14:08:37.134182Z","shell.execute_reply":"2024-12-22T14:08:46.562847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the training and test datasets\n# Replace 'train.csv' and 'test.csv' with the actual file paths\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:13:23.136378Z","iopub.execute_input":"2024-12-22T14:13:23.136715Z","iopub.status.idle":"2024-12-22T14:13:28.033626Z","shell.execute_reply.started":"2024-12-22T14:13:23.136690Z","shell.execute_reply":"2024-12-22T14:13:28.032730Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Checking the shape of the datasets to understand their size\n# 3. We use `shape` to check the number of rows and columns in both datasets.\nprint(\"Training data shape:\", train_data.shape)\nprint(\"Test data shape:\", test_data.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:14:18.602658Z","iopub.execute_input":"2024-12-22T14:14:18.602987Z","iopub.status.idle":"2024-12-22T14:14:18.609329Z","shell.execute_reply.started":"2024-12-22T14:14:18.602963Z","shell.execute_reply":"2024-12-22T14:14:18.608082Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preview the first few rows of the training dataset\nprint(train_data.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:14:36.039859Z","iopub.execute_input":"2024-12-22T14:14:36.040265Z","iopub.status.idle":"2024-12-22T14:14:36.055626Z","shell.execute_reply.started":"2024-12-22T14:14:36.040234Z","shell.execute_reply":"2024-12-22T14:14:36.054697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check for missing values in the training and test datasets\nprint(\"Missing values in training data:\", train_data.isnull().sum().sum())\nprint(\"Missing values in test data:\", test_data.isnull().sum().sum())\n\n# Explanation:\n# 1. Missing data can impact model performance, so we check for null values using `isnull().sum().sum()`.\n# 2. If missing values are found, they need to be handled appropriately.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:14:56.486666Z","iopub.execute_input":"2024-12-22T14:14:56.486983Z","iopub.status.idle":"2024-12-22T14:14:56.538477Z","shell.execute_reply.started":"2024-12-22T14:14:56.486958Z","shell.execute_reply":"2024-12-22T14:14:56.537411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Checking the distribution of the labels in the training dataset\nsns.countplot(x='label', data=train_data)\nplt.title(\"Distribution of Digits in Training Data\")\nplt.xlabel(\"Digits\")\nplt.ylabel(\"Count\")\nplt.show()\n\n# This plot shows the distribution of each digit (0-9) in the training dataset.\n# A balanced distribution indicates there are no significant imbalances among the classes.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:21:06.812639Z","iopub.execute_input":"2024-12-22T14:21:06.813051Z","iopub.status.idle":"2024-12-22T14:21:07.042053Z","shell.execute_reply.started":"2024-12-22T14:21:06.813021Z","shell.execute_reply":"2024-12-22T14:21:07.040969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualize sample images from the dataset\n# Function to plot multiple images\ndef plot_sample_images(data, labels, rows=3, cols=3):\n    fig, axes = plt.subplots(rows, cols, figsize=(10, 10))\n    for i, ax in enumerate(axes.flat):\n        image = data.iloc[i, 1:].values.reshape(28, 28)  # Reshape the flattened pixels into 28x28\n        ax.imshow(image, cmap='gray')\n        ax.set_title(f\"Label: {labels.iloc[i]}\")\n        ax.axis('off')\n    plt.tight_layout()\n    plt.show()\n\n# Plot sample images\nplot_sample_images(train_data, train_data['label'])\n\n# This function visualizes 28x28 pixel images to confirm that the data represents handwritten digits.\n# Labels are displayed to match the images with their respective categories.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:22:31.323160Z","iopub.execute_input":"2024-12-22T14:22:31.323594Z","iopub.status.idle":"2024-12-22T14:22:32.056074Z","shell.execute_reply.started":"2024-12-22T14:22:31.323566Z","shell.execute_reply":"2024-12-22T14:22:32.054994Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Analyzing pixel intensity distribution\npixel_data = train_data.iloc[:, 1:]  # Exclude the 'label' column\nsns.histplot(pixel_data.values.flatten(), bins=50, color='blue')\nplt.title(\"Pixel Intensity Distribution\")\nplt.xlabel(\"Pixel Intensity\")\nplt.ylabel(\"Frequency\")\nplt.show()\n\n# This histogram displays the frequency of pixel intensities (0-255).\n# It helps identify patterns, such as whether most pixels are dark (near 0) or light (near 255).","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:24:19.046718Z","iopub.execute_input":"2024-12-22T14:24:19.047104Z","iopub.status.idle":"2024-12-22T14:24:39.508944Z","shell.execute_reply.started":"2024-12-22T14:24:19.047076Z","shell.execute_reply":"2024-12-22T14:24:39.507954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check the range of pixel values in the training dataset\nmin_pixel_value = train_data.iloc[:, 1:].min().min()  # Exclude the label column\nmax_pixel_value = train_data.iloc[:, 1:].max().max()\n\nprint(f\"Minimum pixel value: {min_pixel_value}\")\nprint(f\"Maximum pixel value: {max_pixel_value}\")\n\n# If the pixel values range from 0 to 255, normalization is needed.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:33:10.545046Z","iopub.execute_input":"2024-12-22T14:33:10.545556Z","iopub.status.idle":"2024-12-22T14:33:10.601585Z","shell.execute_reply.started":"2024-12-22T14:33:10.545444Z","shell.execute_reply":"2024-12-22T14:33:10.600464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalizing the pixel values\nif max_pixel_value > 1:\n    train_data.iloc[:, 1:] = train_data.iloc[:, 1:] / 255.0  # Scale training pixel values\n    test_data.iloc[:, :] = test_data.iloc[:, :] / 255.0      # Scale test pixel values\n\n# We scale the values to a range between 0 and 1 by dividing by 255.\n\nprint(\"Pixel values normalized to [0, 1] range.\")\n\n# Verify normalization\nprint(\"New minimum pixel value in training data:\", train_data.iloc[:, 1:].min().min())\nprint(\"New maximum pixel value in training data:\", train_data.iloc[:, 1:].max().max())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:33:29.456816Z","iopub.execute_input":"2024-12-22T14:33:29.457147Z","iopub.status.idle":"2024-12-22T14:33:31.021589Z","shell.execute_reply.started":"2024-12-22T14:33:29.457123Z","shell.execute_reply":"2024-12-22T14:33:31.020645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Separating features (pixels) and target (label) from the training dataset\nX = train_data.iloc[:, 1:]  # All columns except the first column (label)\ny = train_data['label']     # The first column (label)\n\n# Spliting the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n\n# `test_size=0.2`: Allocates 20% of the data for validation.\n# `random_state=42`: Ensures reproducibility of the split.\n# 'stratify=y`: Ensures the class distribution in the validation set matches the training set.\n\n# Check the shapes of the resulting datasets\nprint(\"Training set shape (X_train):\", X_train.shape)\nprint(\"Validation set shape (X_val):\", X_val.shape)\nprint(\"Training labels shape (y_train):\", y_train.shape)\nprint(\"Validation labels shape (y_val):\", y_val.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:36:24.278499Z","iopub.execute_input":"2024-12-22T14:36:24.278887Z","iopub.status.idle":"2024-12-22T14:36:25.168726Z","shell.execute_reply.started":"2024-12-22T14:36:24.278860Z","shell.execute_reply":"2024-12-22T14:36:25.167691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.utils import to_categorical\n\n# - `Conv2D`: Extracts spatial features from the images.\n# - `MaxPooling2D`: Reduces dimensionality to prevent overfitting.\n# - `Dropout`: Prevents overfitting by randomly dropping neurons during training.\n# - `Dense`: Fully connected layers for classification.\n# - `adam`: Optimizer for efficient training.\n# - `categorical_crossentropy`: Loss function for multi-class classification.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:41:52.008963Z","iopub.execute_input":"2024-12-22T14:41:52.009320Z","iopub.status.idle":"2024-12-22T14:41:52.013636Z","shell.execute_reply.started":"2024-12-22T14:41:52.009293Z","shell.execute_reply":"2024-12-22T14:41:52.012546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reshape the data for CNN input\n\nX_train_cnn = X_train.values.reshape(-1, 28, 28, 1)  \n# Reshape into 28x28 images with 1 channel (grayscale)\n\nX_val_cnn = X_val.values.reshape(-1, 28, 28, 1)\ny_train_cnn = to_categorical(y_train, num_classes=10)  # One-hot encode labels\ny_val_cnn = to_categorical(y_val, num_classes=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:42:57.266763Z","iopub.execute_input":"2024-12-22T14:42:57.267120Z","iopub.status.idle":"2024-12-22T14:42:57.367243Z","shell.execute_reply.started":"2024-12-22T14:42:57.267094Z","shell.execute_reply":"2024-12-22T14:42:57.366296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build the CNN model\ncnn_model = Sequential([\n    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')  # 10 output classes for digits 0-9\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:43:15.732782Z","iopub.execute_input":"2024-12-22T14:43:15.733119Z","iopub.status.idle":"2024-12-22T14:43:15.887604Z","shell.execute_reply.started":"2024-12-22T14:43:15.733094Z","shell.execute_reply":"2024-12-22T14:43:15.886301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile the model\ncnn_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:43:34.913634Z","iopub.execute_input":"2024-12-22T14:43:34.913979Z","iopub.status.idle":"2024-12-22T14:43:34.927909Z","shell.execute_reply.started":"2024-12-22T14:43:34.913950Z","shell.execute_reply":"2024-12-22T14:43:34.926986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\ncnn_history = cnn_model.fit(\n    X_train_cnn, y_train_cnn,\n    validation_data=(X_val_cnn, y_val_cnn),\n    epochs=10, batch_size=64\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:43:50.490729Z","iopub.execute_input":"2024-12-22T14:43:50.491076Z","iopub.status.idle":"2024-12-22T14:46:26.969084Z","shell.execute_reply.started":"2024-12-22T14:43:50.491051Z","shell.execute_reply":"2024-12-22T14:46:26.967951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5. Evaluate the model\nval_loss, val_accuracy = cnn_model.evaluate(X_val_cnn, y_val_cnn)\nprint(\"Validation Accuracy with CNN:\", val_accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:47:14.453951Z","iopub.execute_input":"2024-12-22T14:47:14.454358Z","iopub.status.idle":"2024-12-22T14:47:15.839598Z","shell.execute_reply.started":"2024-12-22T14:47:14.454325Z","shell.execute_reply":"2024-12-22T14:47:15.838710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training and validation accuracy\nplt.figure(figsize=(12, 6))\nplt.plot(cnn_history.history['accuracy'], label='Training Accuracy')\nplt.plot(cnn_history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:48:49.582167Z","iopub.execute_input":"2024-12-22T14:48:49.582565Z","iopub.status.idle":"2024-12-22T14:48:49.824886Z","shell.execute_reply.started":"2024-12-22T14:48:49.582536Z","shell.execute_reply":"2024-12-22T14:48:49.823846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training and validation loss\nplt.figure(figsize=(12, 6))\nplt.plot(cnn_history.history['loss'], label='Training Loss')\nplt.plot(cnn_history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:48:51.665708Z","iopub.execute_input":"2024-12-22T14:48:51.666101Z","iopub.status.idle":"2024-12-22T14:48:51.905342Z","shell.execute_reply.started":"2024-12-22T14:48:51.666071Z","shell.execute_reply":"2024-12-22T14:48:51.904240Z"}},"outputs":[],"execution_count":null}]}