{"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\nimport matplotlib.pyplot as plt # visualisation\nimport seaborn as sns # visualisation","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-06T17:56:00.442130Z","iopub.execute_input":"2023-08-06T17:56:00.442962Z","iopub.status.idle":"2023-08-06T17:56:02.183174Z","shell.execute_reply.started":"2023-08-06T17:56:00.442917Z","shell.execute_reply":"2023-08-06T17:56:02.181930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.decomposition import PCA\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score, classification_report, ConfusionMatrixDisplay","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:02.185579Z","iopub.execute_input":"2023-08-06T17:56:02.186055Z","iopub.status.idle":"2023-08-06T17:56:02.703484Z","shell.execute_reply.started":"2023-08-06T17:56:02.185996Z","shell.execute_reply":"2023-08-06T17:56:02.702241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the training and test datasets from CSV files\n\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:02.705327Z","iopub.execute_input":"2023-08-06T17:56:02.705741Z","iopub.status.idle":"2023-08-06T17:56:09.507635Z","shell.execute_reply.started":"2023-08-06T17:56:02.705706Z","shell.execute_reply":"2023-08-06T17:56:09.506763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first few rows of the training dataset to get a glimpse of the data\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.510707Z","iopub.execute_input":"2023-08-06T17:56:09.511189Z","iopub.status.idle":"2023-08-06T17:56:09.542221Z","shell.execute_reply.started":"2023-08-06T17:56:09.511148Z","shell.execute_reply":"2023-08-06T17:56:09.541098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first few rows of the test dataset to get a glimpse of the data\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.544046Z","iopub.execute_input":"2023-08-06T17:56:09.544479Z","iopub.status.idle":"2023-08-06T17:56:09.565160Z","shell.execute_reply.started":"2023-08-06T17:56:09.544439Z","shell.execute_reply":"2023-08-06T17:56:09.563806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the count of each digit in the training dataset\ndigit_counts = train_data['label'].value_counts()\ndigit_counts","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.567248Z","iopub.execute_input":"2023-08-06T17:56:09.568003Z","iopub.status.idle":"2023-08-06T17:56:09.587601Z","shell.execute_reply.started":"2023-08-06T17:56:09.567957Z","shell.execute_reply":"2023-08-06T17:56:09.586413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for any missing (NaN) values in the training dataset\ntrain_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.589142Z","iopub.execute_input":"2023-08-06T17:56:09.589568Z","iopub.status.idle":"2023-08-06T17:56:09.630817Z","shell.execute_reply.started":"2023-08-06T17:56:09.589530Z","shell.execute_reply":"2023-08-06T17:56:09.629688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for any missing values in the test data\ntest_data.isnull().sum()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.632262Z","iopub.execute_input":"2023-08-06T17:56:09.632643Z","iopub.status.idle":"2023-08-06T17:56:09.659796Z","shell.execute_reply.started":"2023-08-06T17:56:09.632573Z","shell.execute_reply":"2023-08-06T17:56:09.658727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the shape of the training data and test data\nprint(train_data.shape)\nprint(test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.661236Z","iopub.execute_input":"2023-08-06T17:56:09.661849Z","iopub.status.idle":"2023-08-06T17:56:09.666921Z","shell.execute_reply.started":"2023-08-06T17:56:09.661815Z","shell.execute_reply":"2023-08-06T17:56:09.665868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the digit distribution\n\nplt.figure(figsize=(8, 6))\nsns.barplot(x=digit_counts.index, y=digit_counts.values, palette='viridis')\nplt.xlabel('Digits')\nplt.ylabel('Count')\nplt.title('Distribution of Digits in Training Data')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:09.672496Z","iopub.execute_input":"2023-08-06T17:56:09.672919Z","iopub.status.idle":"2023-08-06T17:56:10.028257Z","shell.execute_reply.started":"2023-08-06T17:56:09.672879Z","shell.execute_reply":"2023-08-06T17:56:10.027372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize some sample images\n# This code selects the first 9 images from the training dataset and displays them as a 3x3 grid of grayscale images.\n\nsample_images = train_data.drop(columns=['label']).values[:9]\nsample_labels = train_data['label'].values[:9]\n\nplt.figure(figsize=(8, 8))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(sample_images[i].reshape(28, 28), cmap='gray')\n    plt.title(f'Digit: {sample_labels[i]}')\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:10.029331Z","iopub.execute_input":"2023-08-06T17:56:10.030161Z","iopub.status.idle":"2023-08-06T17:56:11.055021Z","shell.execute_reply.started":"2023-08-06T17:56:10.030128Z","shell.execute_reply":"2023-08-06T17:56:11.053813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize random images from each digit class\n\nplt.figure(figsize=(10, 6))\nfor digit in range(10):\n    digit_data = train_data[train_data['label'] == digit].drop(columns=['label']).values\n    random_image = digit_data[np.random.randint(0, digit_data.shape[0])]\n    plt.subplot(2, 5, digit + 1)\n    plt.imshow(random_image.reshape(28, 28), cmap='gray')\n    plt.title(f'Digit: {digit}')\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:11.056779Z","iopub.execute_input":"2023-08-06T17:56:11.057352Z","iopub.status.idle":"2023-08-06T17:56:12.506439Z","shell.execute_reply.started":"2023-08-06T17:56:11.057311Z","shell.execute_reply":"2023-08-06T17:56:12.505216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the summary statistics of the pixel values\n\npixel_columns = train_data.columns[1:]  # Excluding the 'label' column\ntrain_data[pixel_columns].describe()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:12.508053Z","iopub.execute_input":"2023-08-06T17:56:12.508724Z","iopub.status.idle":"2023-08-06T17:56:15.141069Z","shell.execute_reply.started":"2023-08-06T17:56:12.508690Z","shell.execute_reply":"2023-08-06T17:56:15.139931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize the distribution of pixel intensities for a random digit\n\nrandom_digit = np.random.randint(0, 10)\nplt.figure(figsize=(10, 6))\nsns.histplot(train_data[train_data['label'] == random_digit].drop(columns=['label']).values.flatten(), kde=True, color='b')\nplt.xlabel('Pixel Intensity')\nplt.ylabel('Frequency')\nplt.title(f'Pixel Intensity Distribution for Digit {random_digit}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:15.143895Z","iopub.execute_input":"2023-08-06T17:56:15.144951Z","iopub.status.idle":"2023-08-06T17:56:32.781554Z","shell.execute_reply.started":"2023-08-06T17:56:15.144917Z","shell.execute_reply":"2023-08-06T17:56:32.780321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Investigate the relationship between specific pixels and the digit labels\n\npixels_to_investigate = [100, 200, 300, 400, 500, 600, 750]\nplt.figure(figsize=(12, 8))\nfor i, pixel_idx in enumerate(pixels_to_investigate):\n    plt.subplot(2, 4, i + 1)\n    sns.barplot(x='label', y=f'pixel{pixel_idx}', data=train_data, palette='viridis')\n    plt.xlabel('Digit')\n    plt.ylabel(f'Pixel {pixel_idx} Intensity')\n    plt.title(f'Relationship: Pixel {pixel_idx} vs. Digit')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:32.782878Z","iopub.execute_input":"2023-08-06T17:56:32.783311Z","iopub.status.idle":"2023-08-06T17:56:40.683243Z","shell.execute_reply.started":"2023-08-06T17:56:32.783280Z","shell.execute_reply":"2023-08-06T17:56:40.682254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the training data: separate labels from pixel values\n\nX_train = train_data.drop(columns=['label'])\ny_train = train_data['label']","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:40.684972Z","iopub.execute_input":"2023-08-06T17:56:40.685370Z","iopub.status.idle":"2023-08-06T17:56:40.770261Z","shell.execute_reply.started":"2023-08-06T17:56:40.685337Z","shell.execute_reply":"2023-08-06T17:56:40.768931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the training data into training and validation sets\n\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:40.771842Z","iopub.execute_input":"2023-08-06T17:56:40.772288Z","iopub.status.idle":"2023-08-06T17:56:40.989646Z","shell.execute_reply.started":"2023-08-06T17:56:40.772249Z","shell.execute_reply":"2023-08-06T17:56:40.988601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform PCA (Principal Component Analysis) on the training and validation data to reduce the dimensionality of the data.\n# It fits PCA on the training data and transforms both X_train and X_val to reduced components.\n# The number of components used is set to 100.\n\npca = PCA(n_components=100, random_state=0)\npca.fit(X_train)\nX_train_pca = pca.transform(X_train)\nX_val_pca = pca.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:40.990818Z","iopub.execute_input":"2023-08-06T17:56:40.991125Z","iopub.status.idle":"2023-08-06T17:56:44.974039Z","shell.execute_reply.started":"2023-08-06T17:56:40.991098Z","shell.execute_reply":"2023-08-06T17:56:44.972296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a Support Vector Machine (SVM) model and fit it to the training data\n\nsvc_mod = SVC(random_state=0)\nsvc_mod.fit(X_train_pca, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:56:44.976841Z","iopub.execute_input":"2023-08-06T17:56:44.978024Z","iopub.status.idle":"2023-08-06T17:57:04.191905Z","shell.execute_reply.started":"2023-08-06T17:56:44.977948Z","shell.execute_reply":"2023-08-06T17:57:04.190788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the validation set and calculate accuracy (Here accuracy score is calculated)\n\ny_val_pred = svc_mod.predict(X_val_pca)\nacc = accuracy_score(y_val, y_val_pred)\nprint(f\"Accuracy Score: {acc}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:57:04.193846Z","iopub.execute_input":"2023-08-06T17:57:04.194656Z","iopub.status.idle":"2023-08-06T17:57:12.643302Z","shell.execute_reply.started":"2023-08-06T17:57:04.194612Z","shell.execute_reply":"2023-08-06T17:57:12.642383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate the classification report\n\ncr = classification_report(y_val, y_val_pred)\nprint(f\"Classification Report: \\n{cr}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:57:12.644558Z","iopub.execute_input":"2023-08-06T17:57:12.645430Z","iopub.status.idle":"2023-08-06T17:57:12.677906Z","shell.execute_reply.started":"2023-08-06T17:57:12.645395Z","shell.execute_reply":"2023-08-06T17:57:12.676874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate and plot the confusion matrix for the SVM model\n\nplt.figure()\ncm_display = ConfusionMatrixDisplay.from_predictions(y_val, y_val_pred, cmap=\"Blues\")\nplt.title(\"SVM Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:57:12.679071Z","iopub.execute_input":"2023-08-06T17:57:12.679367Z","iopub.status.idle":"2023-08-06T17:57:13.239034Z","shell.execute_reply.started":"2023-08-06T17:57:12.679341Z","shell.execute_reply":"2023-08-06T17:57:13.238210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply PCA to the test data using the same number of components as in the training data\nX_test_pca = pca.transform(test_data)\n\n# Make predictions on the test set\ntest_predictions = svc_mod.predict(X_test_pca)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:57:13.240252Z","iopub.execute_input":"2023-08-06T17:57:13.240740Z","iopub.status.idle":"2023-08-06T17:57:41.477211Z","shell.execute_reply.started":"2023-08-06T17:57:13.240702Z","shell.execute_reply":"2023-08-06T17:57:41.475992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dataframe with predicted values \nsubmission = pd.DataFrame({\"ImageId\": range(1, len(X_test_pca) + 1), \"Label\": test_predictions})\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-08-06T17:57:41.478377Z","iopub.execute_input":"2023-08-06T17:57:41.478695Z","iopub.status.idle":"2023-08-06T17:57:41.491729Z","shell.execute_reply.started":"2023-08-06T17:57:41.478666Z","shell.execute_reply":"2023-08-06T17:57:41.490872Z"},"trusted":true},"execution_count":null,"outputs":[]}]}