{"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":30918,"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":"2025-02-22T12:38:55.182257Z","iopub.execute_input":"2025-02-22T12:38:55.182487Z","iopub.status.idle":"2025-02-22T12:38:56.460333Z","shell.execute_reply.started":"2025-02-22T12:38:55.182464Z","shell.execute_reply":"2025-02-22T12:38:56.459127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Loading the training and test datasets for the digit recognition problem\n# The `train.csv` contains the labeled data (input features and corresponding labels) for training the model.\n# The `test.csv` contains the input features for testing the model, but without the labels (since these are to be predicted).\n# These datasets are being loaded into pandas DataFrames, allowing for easy manipulation and analysis of the data.\n\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')  # Load the training data from the CSV file\ntest  = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')   # Load the test data from the CSV file\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:39:24.874149Z","iopub.execute_input":"2025-02-22T12:39:24.874538Z","iopub.status.idle":"2025-02-22T12:39:29.974836Z","shell.execute_reply.started":"2025-02-22T12:39:24.874509Z","shell.execute_reply":"2025-02-22T12:39:29.973957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:39:32.234021Z","iopub.execute_input":"2025-02-22T12:39:32.234349Z","iopub.status.idle":"2025-02-22T12:39:32.271767Z","shell.execute_reply.started":"2025-02-22T12:39:32.234321Z","shell.execute_reply":"2025-02-22T12:39:32.269889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:39:44.165664Z","iopub.execute_input":"2025-02-22T12:39:44.166064Z","iopub.status.idle":"2025-02-22T12:39:44.172181Z","shell.execute_reply.started":"2025-02-22T12:39:44.166035Z","shell.execute_reply":"2025-02-22T12:39:44.171133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extracting the first image from the training dataset and reshaping it to its original 28x28 pixel structure\n# The `train.iloc[0, 1:]` extracts the first row from the dataset (index 0), excluding the first column (which is the label).\n# The data in the row corresponds to the pixel values of the first image in a flattened form (784 pixels).\n# We then use `reshape(28,28)` to convert this 1D array of 784 values back into a 28x28 2D array, representing the image.\n\nnumber = np.array(train.iloc[0,1:]).reshape(28,28)  # Reshaping the first image into a 28x28 2D array\n\n# Accessing and displaying the pixel value at row 10 (index 10) in the reshaped image (number)\n# This will return the intensity value of the 10th pixel in the 11th row (as index starts from 0) of the image.\n# Since the image is a 2D array of shape (28, 28), this will provide the grayscale value (0-255) for that particular pixel.\n\nnumber[10]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:47:21.365192Z","iopub.execute_input":"2025-02-22T12:47:21.365561Z","iopub.status.idle":"2025-02-22T12:47:21.374593Z","shell.execute_reply.started":"2025-02-22T12:47:21.365535Z","shell.execute_reply":"2025-02-22T12:47:21.373305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:47:27.241668Z","iopub.execute_input":"2025-02-22T12:47:27.242115Z","iopub.status.idle":"2025-02-22T12:47:27.246948Z","shell.execute_reply.started":"2025-02-22T12:47:27.242084Z","shell.execute_reply":"2025-02-22T12:47:27.245640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Displaying the reshaped image using matplotlib\n# `plt.imshow(number)` will visualize the 28x28 pixel image, where `number` is the 2D array representing the pixel values of the image.\n# This function interprets the values in `number` (grayscale pixel intensities) and displays them as an image.\n# The pixel intensities will be shown in a grayscale colormap by default, where black represents the lowest value (0) and white represents the highest value (255).\n\nplt.imshow(number)  # Visualize the 28x28 image using imshow from matplotlib","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:47:51.191337Z","iopub.execute_input":"2025-02-22T12:47:51.191663Z","iopub.status.idle":"2025-02-22T12:47:51.493041Z","shell.execute_reply.started":"2025-02-22T12:47:51.191638Z","shell.execute_reply":"2025-02-22T12:47:51.491971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extracting the fourth image from the training dataset and reshaping it to its original 28x28 pixel structure\n# The `train.iloc[3, 1:]` extracts the fourth row from the dataset (index 3), excluding the first column (which contains the label).\n# The data in this row corresponds to the pixel values of the fourth image in a flattened 1D form (784 values).\n# We then use `reshape(28, 28)` to convert this 1D array of 784 values back into a 28x28 2D array, representing the image.\n\nnumber4 = np.array(train.iloc[3, 1:]).reshape(28, 28)  # Reshaping the fourth image into a 28x28 2D array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:48:17.879733Z","iopub.execute_input":"2025-02-22T12:48:17.880234Z","iopub.status.idle":"2025-02-22T12:48:17.886840Z","shell.execute_reply.started":"2025-02-22T12:48:17.880199Z","shell.execute_reply":"2025-02-22T12:48:17.885561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(number4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:48:25.390365Z","iopub.execute_input":"2025-02-22T12:48:25.390756Z","iopub.status.idle":"2025-02-22T12:48:25.575993Z","shell.execute_reply.started":"2025-02-22T12:48:25.390727Z","shell.execute_reply":"2025-02-22T12:48:25.574611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extracting the pixel values of the fourth image in the dataset (excluding the label column)\n# `train.iloc[3, 1:]` selects the fourth row (index 3) of the dataset and excludes the first column, which contains the label (digit).\n# This gives us a 1D array of length 784, which represents the flattened pixel values of the 28x28 image.\n\narr = train.iloc[3, 1:]  # Extracting the flattened pixel values of the fourth image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:48:43.363134Z","iopub.execute_input":"2025-02-22T12:48:43.363493Z","iopub.status.idle":"2025-02-22T12:48:43.368808Z","shell.execute_reply.started":"2025-02-22T12:48:43.363467Z","shell.execute_reply":"2025-02-22T12:48:43.367159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plotting a histogram of the pixel values for the fourth image\n# `plt.hist(arr)` will create a histogram that shows the distribution of pixel intensity values in the flattened array `arr`.\n# Each pixel intensity is a value between 0 and 255, and the histogram will visualize how these intensity values are distributed across all 784 pixels in the image.\n# The x-axis represents pixel intensity values (0-255), while the y-axis shows the frequency (how many times each intensity value appears in the image).\n\nplt.hist(arr)  # Create and display a histogram of the pixel values for the fourth image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:00.474001Z","iopub.execute_input":"2025-02-22T12:49:00.474375Z","iopub.status.idle":"2025-02-22T12:49:00.670139Z","shell.execute_reply.started":"2025-02-22T12:49:00.474347Z","shell.execute_reply":"2025-02-22T12:49:00.669086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"max(arr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:07.592064Z","iopub.execute_input":"2025-02-22T12:49:07.592396Z","iopub.status.idle":"2025-02-22T12:49:07.599351Z","shell.execute_reply.started":"2025-02-22T12:49:07.592370Z","shell.execute_reply":"2025-02-22T12:49:07.597894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nmin(arr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:12.825746Z","iopub.execute_input":"2025-02-22T12:49:12.826316Z","iopub.status.idle":"2025-02-22T12:49:12.833623Z","shell.execute_reply.started":"2025-02-22T12:49:12.826274Z","shell.execute_reply":"2025-02-22T12:49:12.832599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.mean(arr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:20.696606Z","iopub.execute_input":"2025-02-22T12:49:20.697017Z","iopub.status.idle":"2025-02-22T12:49:20.704548Z","shell.execute_reply.started":"2025-02-22T12:49:20.696986Z","shell.execute_reply":"2025-02-22T12:49:20.703394Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Missing Values","metadata":{}},{"cell_type":"code","source":"\ntrain.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:34.176215Z","iopub.execute_input":"2025-02-22T12:49:34.176529Z","iopub.status.idle":"2025-02-22T12:49:34.220753Z","shell.execute_reply.started":"2025-02-22T12:49:34.176504Z","shell.execute_reply":"2025-02-22T12:49:34.219469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:40.608040Z","iopub.execute_input":"2025-02-22T12:49:40.608456Z","iopub.status.idle":"2025-02-22T12:49:40.948163Z","shell.execute_reply.started":"2025-02-22T12:49:40.608421Z","shell.execute_reply":"2025-02-22T12:49:40.946984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = x[:,1:]\nY_train = x[:,0]\nX_test = y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:49:47.266428Z","iopub.execute_input":"2025-02-22T12:49:47.266820Z","iopub.status.idle":"2025-02-22T12:49:47.271943Z","shell.execute_reply.started":"2025-02-22T12:49:47.266791Z","shell.execute_reply":"2025-02-22T12:49:47.270670Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"# Reshaping the training images for compatibility with Keras' ImageDataGenerator\n# The `X_train.reshape(-1, 28, 28, 1)` line reshapes the input data to have the shape required by Keras' deep learning models.\n# - `-1` indicates that the number of samples (images) will be inferred from the total number of elements.\n# - `28, 28` specifies that each image is 28x28 pixels in size.\n# - `1` represents that the images are grayscale (one color channel, unlike RGB images which have 3 channels).\n# The reshaped shape will be `(num_samples, 28, 28, 1)` where `num_samples` is the number of images in `X_train`.\n\nX_train = X_train.reshape(-1, 28, 28, 1)  # Reshaping the training data to (num_samples, 28, 28, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:50:24.540576Z","iopub.execute_input":"2025-02-22T12:50:24.541178Z","iopub.status.idle":"2025-02-22T12:50:24.546390Z","shell.execute_reply.started":"2025-02-22T12:50:24.541136Z","shell.execute_reply":"2025-02-22T12:50:24.544802Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Initializing the ImageDataGenerator with data augmentation parameters\n# `ImageDataGenerator` is used for real-time augmentation of the training images during model training.\n# The parameters applied here modify the images slightly to make the model more robust and prevent overfitting by simulating variations in the data:\n# - `rotation_range = 15`: Randomly rotates the image within a range of 15 degrees.\n# - `height_shift_range = 0.1`: Randomly shifts the image vertically by up to 10% of the total height (28 pixels).\n# - `zoom_range = 0.1`: Randomly zooms in or out of the image by up to 10%.\n# - `shear_range = 0.1`: Applies random shear transformations to the image (distortion based on shearing).\n# These transformations help create more variety in the training images and improve the model's generalization capability.\n\ndatagen = ImageDataGenerator(\n    rotation_range = 15,\n    height_shift_range = 0.1,\n    zoom_range  = 0.1,\n    shear_range = 0.1,\n)\n\n# Fitting the data augmentation transformations to the training dataset\n# `datagen.fit(X_train)` computes the necessary statistics (like mean and std) from the training data for the augmentation.\n# These statistics are required when performing certain transformations like normalization or feature-wise operations.\n\ndatagen.fit(X_train)  # Apply the data augmentation settings to the training data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:52:15.118672Z","iopub.execute_input":"2025-02-22T12:52:15.119138Z","iopub.status.idle":"2025-02-22T12:52:15.425032Z","shell.execute_reply.started":"2025-02-22T12:52:15.119106Z","shell.execute_reply":"2025-02-22T12:52:15.423643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reshaping the training data from 28x28x1 images to a 1D array format for model training\n# The original shape of `X_train` is (-1, 28, 28, 1), where each image is a 28x28 pixel grayscale image.\n# `X_train.reshape(-1, 784)` flattens each 28x28 image into a 1D array of 784 values (28 * 28 = 784), where each value corresponds to the pixel intensity.\n# The `-1` allows NumPy to automatically infer the number of samples, so the reshaped `X_train` will have the shape (num_samples, 784), where `num_samples` is the number of images.\n\nX_train = X_train.reshape(-1, 784)  # Flattening each 28x28 image into a 784-dimensional vector","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:52:25.115488Z","iopub.execute_input":"2025-02-22T12:52:25.115901Z","iopub.status.idle":"2025-02-22T12:52:25.120887Z","shell.execute_reply.started":"2025-02-22T12:52:25.115871Z","shell.execute_reply":"2025-02-22T12:52:25.119537Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"# Importing the KNeighborsClassifier from scikit-learn\n# KNeighborsClassifier is a machine learning algorithm used for classification tasks. It works based on the principle of \"k-nearest neighbors\".\n# In this algorithm, the label of a new data point is determined by the majority class among its 'k' nearest neighbors in the feature space.\n# The `KNeighborsClassifier` uses distance metrics (like Euclidean distance) to find the closest points and make predictions based on them.\n# It is a simple, yet effective, method for classification tasks, especially when there is no clear assumption about the underlying distribution of the data.\n\nfrom sklearn.neighbors import KNeighborsClassifier  # Importing the KNeighborsClassifier from scikit-learn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:53:03.716430Z","iopub.execute_input":"2025-02-22T12:53:03.716909Z","iopub.status.idle":"2025-02-22T12:53:04.516176Z","shell.execute_reply.started":"2025-02-22T12:53:03.716833Z","shell.execute_reply":"2025-02-22T12:53:04.515021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initializing the KNeighborsClassifier model with 5 neighbors\n# `KNeighborsClassifier(n_neighbors=5)` creates a KNN classifier with `k=5`, meaning the algorithm will consider the 5 closest neighbors of each data point to make predictions.\n# The choice of 5 is a common default value for `k`, but it can be fine-tuned depending on the data and the problem.\n# The model uses the distances between the data points to determine the majority class among these 5 nearest neighbors and assigns that class to the data point.\n\nknn = KNeighborsClassifier(n_neighbors=5)  # Create the KNN classifier with 5 neighbors\n\n# Fitting the KNN classifier to the training data\n# `knn.fit(X_train, Y_train)` trains the model using the training data `X_train` (features) and `Y_train` (labels).\n# The model will learn from the training data, finding patterns in the relationship between the features and the target labels.\n# After fitting, the model is ready to make predictions on new, unseen data.\n\nknn.fit(X_train, Y_train)  # Fit the KNN classifier to the training data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:53:29.076267Z","iopub.execute_input":"2025-02-22T12:53:29.077083Z","iopub.status.idle":"2025-02-22T12:53:29.296044Z","shell.execute_reply.started":"2025-02-22T12:53:29.077047Z","shell.execute_reply":"2025-02-22T12:53:29.294898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Making a prediction using the trained KNN model for the 60th test image\n# `knn.predict(X_test[60].reshape(1, -1))` predicts the class label for the 60th sample in the test set.\n# - `X_test[60]` selects the 60th image from the `X_test` dataset.\n# - `.reshape(1, -1)` reshapes the selected image from 2D (28x28) to a 1D array of 784 features.\n# - The KNN model uses the 5 nearest neighbors of this reshaped image to predict its class label.\n# The result is an array with a single predicted label, so we use `.item()` to extract the integer value.\n\nint(knn.predict(X_test[60].reshape(1, -1)).item())  # Predict the label for the 60th test image and convert it to an integer","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:58:05.341398Z","iopub.execute_input":"2025-02-22T12:58:05.341803Z","iopub.status.idle":"2025-02-22T12:58:05.579573Z","shell.execute_reply.started":"2025-02-22T12:58:05.341774Z","shell.execute_reply":"2025-02-22T12:58:05.578392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extracting and reshaping the 60th image from the test dataset\n# `test.iloc[60, :]` selects the 60th row (index 60) from the `test` dataset. This row contains the flattened pixel values for the image.\n# `.reshape(28, 28)` converts the 1D array of 784 values back into a 2D array of shape (28, 28), representing the original 28x28 pixel image.\n\nnum = np.array(test.iloc[60, :]).reshape(28, 28)  # Reshaping the 60th image in the test dataset into 28x28\n\n# Displaying the reshaped image using matplotlib's imshow\n# `plt.imshow(num)` visualizes the 28x28 pixel image, where `num` is the 2D array of pixel values.\n# The image will be displayed in grayscale by default, with pixel intensities represented from black (lowest) to white (highest).\n\nplt.imshow(num)  # Display the 60th test image as a 28x28 pixel image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-22T12:58:40.861810Z","iopub.execute_input":"2025-02-22T12:58:40.862334Z","iopub.status.idle":"2025-02-22T12:58:41.068699Z","shell.execute_reply.started":"2025-02-22T12:58:40.862296Z","shell.execute_reply":"2025-02-22T12:58:41.067665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}