{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# MNIST HANDWRITTEN DIGIT RECOGNITION THROUGH ML AND DL ANALYSIS ","metadata":{}},{"cell_type":"markdown","source":"\nThe data files train.csv and test.csv consist of grayscale images depicting hand-drawn digits ranging from zero to nine.\n\nEach image is represented as a 28x28 grid, totaling 784 pixels. Each pixel is assigned a single value indicating its brightness, with higher values indicating darker shades. These pixel values range from 0 to 255, inclusive.\n\nThe training dataset (train.csv) contains 785 columns. The first column, labeled \"label,\" represents the digit drawn by the user, while the remaining columns contain the pixel values of the corresponding image.\n\nIn the other way can load the dataset by tensorflow and keras package.Through the following code\n\nimport tensorflow as tf\n\nfrom tensorflow.keras.datasets import mnist\n","metadata":{}},{"cell_type":"markdown","source":"# Import Necessary Library ","metadata":{}},{"cell_type":"code","source":"# Import necessary libraries\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import datasets\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:23.628438Z","iopub.execute_input":"2024-02-16T02:47:23.628746Z","iopub.status.idle":"2024-02-16T02:47:24.542942Z","shell.execute_reply.started":"2024-02-16T02:47:23.628716Z","shell.execute_reply":"2024-02-16T02:47:24.541909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:24.544549Z","iopub.execute_input":"2024-02-16T02:47:24.545152Z","iopub.status.idle":"2024-02-16T02:47:29.946005Z","shell.execute_reply.started":"2024-02-16T02:47:24.545111Z","shell.execute_reply":"2024-02-16T02:47:29.944815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:29.947406Z","iopub.execute_input":"2024-02-16T02:47:29.947732Z","iopub.status.idle":"2024-02-16T02:47:29.970461Z","shell.execute_reply.started":"2024-02-16T02:47:29.947705Z","shell.execute_reply":"2024-02-16T02:47:29.969547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:29.973176Z","iopub.execute_input":"2024-02-16T02:47:29.973495Z","iopub.status.idle":"2024-02-16T02:47:29.981428Z","shell.execute_reply.started":"2024-02-16T02:47:29.973468Z","shell.execute_reply":"2024-02-16T02:47:29.980562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:29.982486Z","iopub.execute_input":"2024-02-16T02:47:29.982800Z","iopub.status.idle":"2024-02-16T02:47:29.991321Z","shell.execute_reply.started":"2024-02-16T02:47:29.982773Z","shell.execute_reply":"2024-02-16T02:47:29.990495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:29.992489Z","iopub.execute_input":"2024-02-16T02:47:29.992892Z","iopub.status.idle":"2024-02-16T02:47:32.145979Z","shell.execute_reply.started":"2024-02-16T02:47:29.992864Z","shell.execute_reply":"2024-02-16T02:47:32.145010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:32.147394Z","iopub.execute_input":"2024-02-16T02:47:32.147683Z","iopub.status.idle":"2024-02-16T02:47:32.165509Z","shell.execute_reply.started":"2024-02-16T02:47:32.147658Z","shell.execute_reply":"2024-02-16T02:47:32.164364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:32.168647Z","iopub.execute_input":"2024-02-16T02:47:32.169040Z","iopub.status.idle":"2024-02-16T02:47:32.176246Z","shell.execute_reply.started":"2024-02-16T02:47:32.169002Z","shell.execute_reply":"2024-02-16T02:47:32.175138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.columns","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:32.177811Z","iopub.execute_input":"2024-02-16T02:47:32.178393Z","iopub.status.idle":"2024-02-16T02:47:32.185922Z","shell.execute_reply.started":"2024-02-16T02:47:32.178357Z","shell.execute_reply":"2024-02-16T02:47:32.185077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.describe()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:32.187027Z","iopub.execute_input":"2024-02-16T02:47:32.187456Z","iopub.status.idle":"2024-02-16T02:47:34.097169Z","shell.execute_reply.started":"2024-02-16T02:47:32.187429Z","shell.execute_reply":"2024-02-16T02:47:34.096038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking the Null Value / Wrangling ","metadata":{}},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:34.098618Z","iopub.execute_input":"2024-02-16T02:47:34.098969Z","iopub.status.idle":"2024-02-16T02:47:34.138113Z","shell.execute_reply.started":"2024-02-16T02:47:34.098941Z","shell.execute_reply":"2024-02-16T02:47:34.137324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:34.139971Z","iopub.execute_input":"2024-02-16T02:47:34.140280Z","iopub.status.idle":"2024-02-16T02:47:34.166971Z","shell.execute_reply.started":"2024-02-16T02:47:34.140254Z","shell.execute_reply":"2024-02-16T02:47:34.165930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading the Data ","metadata":{}},{"cell_type":"code","source":"def sample_images_data(data, imagerows, imagecols):\n    # An empty list to collect some samples\n    sample_images = []\n    sample_labels = []\n\n    # Iterate over the unique labels in the data\n    for label in data['label'].unique():\n        # Get four samples for each category\n        samples = data[data[\"label\"] == label].head(4)\n        # Append the samples to the samples list\n        for j, sample in enumerate(samples.values):\n            # Convert the pixel values to an image array\n            img = np.array(sample[1:]).reshape(imagerows, imagecols)\n            sample_images.append(img)\n            sample_labels.append(sample[0])  # Assuming label is in the first column\n\n    print(\"Total number of sample images to plot:\", len(sample_images))\n    return sample_images, sample_labels","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:34.172468Z","iopub.execute_input":"2024-02-16T02:47:34.173452Z","iopub.status.idle":"2024-02-16T02:47:34.181668Z","shell.execute_reply.started":"2024-02-16T02:47:34.173408Z","shell.execute_reply":"2024-02-16T02:47:34.180879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagerows = 28  # Number of rows in each image\nimagecols = 28  # Number of columns in each image\ntrain_sample_images, train_sample_labels = sample_images_data(df_train, imagerows, imagecols)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:34.183162Z","iopub.execute_input":"2024-02-16T02:47:34.184115Z","iopub.status.idle":"2024-02-16T02:47:34.561371Z","shell.execute_reply.started":"2024-02-16T02:47:34.184083Z","shell.execute_reply":"2024-02-16T02:47:34.560218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample_images(data_sample_images, data_sample_labels, cmap=\"Blues\"):\n    # Plot the sample images now\n    f, ax = plt.subplots(5, 8, figsize=(16, 10))\n\n    for i, img in enumerate(data_sample_images):\n        ax[i // 8, i % 8].imshow(img, cmap=cmap)\n        ax[i // 8, i % 8].axis('off')\n        ax[i // 8, i % 8].set_title(data_sample_labels[i])  # Updated this line\n    plt.show()\n\nplot_sample_images(train_sample_images, train_sample_labels, \"Blues\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:34.562617Z","iopub.execute_input":"2024-02-16T02:47:34.563682Z","iopub.status.idle":"2024-02-16T02:47:37.038677Z","shell.execute_reply.started":"2024-02-16T02:47:34.563648Z","shell.execute_reply":"2024-02-16T02:47:37.037433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Load data\ntrain_data = df_train\ntest_data = df_test\n\n# Extract images and labels (from training data)\ntrain_images = train_data.drop(\"label\", axis=1).values.reshape(-1, 28, 28)\ntrain_labels = train_data[\"label\"].to_numpy()\n\n# Normalize pixel values\ntrain_images = train_images / 255.0\n\n# Preprocess test data similarly\ntest_images = test_data.values.reshape(-1, 28, 28) / 255.0\n\n# Split training data (optional for validation)\n# ... implement splitting as needed ...\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:37.040218Z","iopub.execute_input":"2024-02-16T02:47:37.040563Z","iopub.status.idle":"2024-02-16T02:47:37.408258Z","shell.execute_reply.started":"2024-02-16T02:47:37.040534Z","shell.execute_reply":"2024-02-16T02:47:37.407066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_images.show()\nimport matplotlib.pyplot as plt\n\n# Select a few images to visualize (adjust indices as needed)\nimages_to_show = train_images[:10]  # Show the first 10 images\n\n# Create a grid of subplots\nfig, axes = plt.subplots(nrows=2, ncols=5, figsize=(10, 4))\n\n# Iterate through images and plot them\nfor i, image in enumerate(images_to_show):\n    ax = axes.flat[i]  # Get the corresponding subplot axis\n    ax.imshow(image, cmap=\"gray\")  # Display image in grayscale\n    ax.axis(\"off\")  # Turn off axis labels and ticks\n\nplt.tight_layout()  # Adjust layout for better spacing\nplt.show()  # Display the plot\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:37.409964Z","iopub.execute_input":"2024-02-16T02:47:37.410511Z","iopub.status.idle":"2024-02-16T02:47:37.760428Z","shell.execute_reply.started":"2024-02-16T02:47:37.410470Z","shell.execute_reply":"2024-02-16T02:47:37.759303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting and preparing of the data ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.applications import VGG16, VGG19, InceptionV3\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import to_categorical\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:37.761993Z","iopub.execute_input":"2024-02-16T02:47:37.762819Z","iopub.status.idle":"2024-02-16T02:47:41.929253Z","shell.execute_reply.started":"2024-02-16T02:47:37.762784Z","shell.execute_reply":"2024-02-16T02:47:41.927935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming your data is in a pandas DataFrame named 'df'\n\n# Prepare the data\nX = df_train.drop('label', axis=1).values.reshape(-1, 28, 28, 1)  # Reshape pixel data for CNN\ny = to_categorical(df_train['label'])  # Convert labels to categorical\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:41.930846Z","iopub.execute_input":"2024-02-16T02:47:41.931843Z","iopub.status.idle":"2024-02-16T02:47:42.067996Z","shell.execute_reply.started":"2024-02-16T02:47:41.931805Z","shell.execute_reply":"2024-02-16T02:47:42.066759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:42.069610Z","iopub.execute_input":"2024-02-16T02:47:42.070000Z","iopub.status.idle":"2024-02-16T02:47:42.691050Z","shell.execute_reply.started":"2024-02-16T02:47:42.069966Z","shell.execute_reply":"2024-02-16T02:47:42.689779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n\n# 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    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')\n])\n\ncnn_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:42.693198Z","iopub.execute_input":"2024-02-16T02:47:42.693610Z","iopub.status.idle":"2024-02-16T02:47:42.829413Z","shell.execute_reply.started":"2024-02-16T02:47:42.693558Z","shell.execute_reply":"2024-02-16T02:47:42.828097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:42.832549Z","iopub.execute_input":"2024-02-16T02:47:42.833012Z","iopub.status.idle":"2024-02-16T02:47:42.857388Z","shell.execute_reply.started":"2024-02-16T02:47:42.832968Z","shell.execute_reply":"2024-02-16T02:47:42.856345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.fit(X_train, y_train, epochs=3, batch_size=32, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:47:42.859126Z","iopub.execute_input":"2024-02-16T02:47:42.859515Z","iopub.status.idle":"2024-02-16T02:48:24.934530Z","shell.execute_reply.started":"2024-02-16T02:47:42.859484Z","shell.execute_reply":"2024-02-16T02:48:24.933327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_cnn, accuracy = cnn_model.evaluate(X_test, y_test)\n# print('the loss and accurcay is', accuracy)\n\n# # Evaluate the model\n# loss_ddnn, accuracy_ddnn = model_ddnn.evaluate(X_test, y_test)\nprint(f'CNN: Loss: {loss_cnn}, Accuracy: {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:24.936261Z","iopub.execute_input":"2024-02-16T02:48:24.936757Z","iopub.status.idle":"2024-02-16T02:48:26.517626Z","shell.execute_reply.started":"2024-02-16T02:48:24.936715Z","shell.execute_reply":"2024-02-16T02:48:26.516340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNN ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.metrics import accuracy_score\nimport pickle","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:26.519772Z","iopub.execute_input":"2024-02-16T02:48:26.520271Z","iopub.status.idle":"2024-02-16T02:48:26.576091Z","shell.execute_reply.started":"2024-02-16T02:48:26.520226Z","shell.execute_reply":"2024-02-16T02:48:26.575115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape X_train to flatten the height, width, and channels dimensions into a single dimension\nX_train_flattened = X_train.reshape(X_train.shape[0], -1)\n\n# Similarly, reshape X_test\nX_test_flattened = X_test.reshape(X_test.shape[0], -1)\n\n# K-Nearest Neighbors (KNN) Classifier\nknn_classifier = KNeighborsClassifier(n_neighbors=5)\nknn_classifier.fit(X_train_flattened, y_train)\ny_pred_knn = knn_classifier.predict(X_test_flattened)\naccuracy_knn = accuracy_score(y_test, y_pred_knn)\nprint(\"KNN Classifier Accuracy:\", accuracy_knn)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:26.577617Z","iopub.execute_input":"2024-02-16T02:48:26.578616Z","iopub.status.idle":"2024-02-16T02:48:44.890225Z","shell.execute_reply.started":"2024-02-16T02:48:26.578571Z","shell.execute_reply":"2024-02-16T02:48:44.889105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MLP ","metadata":{}},{"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:44.891769Z","iopub.execute_input":"2024-02-16T02:48:44.892229Z","iopub.status.idle":"2024-02-16T02:48:44.899849Z","shell.execute_reply.started":"2024-02-16T02:48:44.892196Z","shell.execute_reply":"2024-02-16T02:48:44.899032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create an MLP classifier with two hidden layers with 100 neurons in each layer\nclf = MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=1000)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:44.901268Z","iopub.execute_input":"2024-02-16T02:48:44.901921Z","iopub.status.idle":"2024-02-16T02:48:44.909356Z","shell.execute_reply.started":"2024-02-16T02:48:44.901890Z","shell.execute_reply":"2024-02-16T02:48:44.908341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Flatten the input data\nX_train_flattened = X_train.reshape(X_train.shape[0], -1)\n\n# Now X_train_flattened has shape (n_samples, n_features)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:44.910625Z","iopub.execute_input":"2024-02-16T02:48:44.910939Z","iopub.status.idle":"2024-02-16T02:48:44.918881Z","shell.execute_reply.started":"2024-02-16T02:48:44.910912Z","shell.execute_reply":"2024-02-16T02:48:44.918110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the MLPClassifier with the preprocessed input data\nclf.fit(X_train_flattened, y_train)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:48:44.920550Z","iopub.execute_input":"2024-02-16T02:48:44.921260Z","iopub.status.idle":"2024-02-16T02:50:39.318107Z","shell.execute_reply.started":"2024-02-16T02:48:44.921221Z","shell.execute_reply":"2024-02-16T02:50:39.316507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape X_test to be two-dimensional\nX_test_reshaped = X_test.reshape(X_test.shape[0], -1)\n\n# Make predictions on the testing data\ny_pred = clf.predict(X_test_reshaped)\n\n# Calculate the accuracy of the model\naccuracy = accuracy_score(y_test, y_pred)\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:50:39.320722Z","iopub.execute_input":"2024-02-16T02:50:39.321629Z","iopub.status.idle":"2024-02-16T02:50:39.439449Z","shell.execute_reply.started":"2024-02-16T02:50:39.321570Z","shell.execute_reply":"2024-02-16T02:50:39.437844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:50:39.441978Z","iopub.execute_input":"2024-02-16T02:50:39.443021Z","iopub.status.idle":"2024-02-16T02:50:39.510966Z","shell.execute_reply.started":"2024-02-16T02:50:39.442942Z","shell.execute_reply":"2024-02-16T02:50:39.509270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test.reshape(X_test.shape[0], -1)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T02:50:39.513615Z","iopub.execute_input":"2024-02-16T02:50:39.514769Z","iopub.status.idle":"2024-02-16T02:50:39.522970Z","shell.execute_reply.started":"2024-02-16T02:50:39.514706Z","shell.execute_reply":"2024-02-16T02:50:39.521518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Reshape the input data to have a 2D shape\nX_train_reshaped = X_train.reshape(X_train.shape[0], -1)\nX_test_reshaped = X_test.reshape(X_test.shape[0], -1)\n\n# Initialize Random Forest Classifier\nrf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)\n\n# Train Random Forest Classifier\nrf_classifier.fit(X_train_reshaped, y_train)\n\n# Make predictions on the testing data\ny_pred_rf = rf_classifier.predict(X_test_reshaped)\n\n# Calculate accuracy for Random Forest\naccuracy_rf = accuracy_score(y_test, y_pred_rf)\nprint(\"Random Forest Accuracy:\", accuracy_rf)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:02:44.858032Z","iopub.execute_input":"2024-02-16T03:02:44.860369Z","iopub.status.idle":"2024-02-16T03:03:38.955602Z","shell.execute_reply.started":"2024-02-16T03:02:44.860295Z","shell.execute_reply":"2024-02-16T03:03:38.954265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Decision Tree Classifier","metadata":{}},{"cell_type":"code","source":"dt_classifier = DecisionTreeClassifier(random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:37:20.710680Z","iopub.execute_input":"2024-02-16T03:37:20.712197Z","iopub.status.idle":"2024-02-16T03:37:20.718417Z","shell.execute_reply.started":"2024-02-16T03:37:20.712118Z","shell.execute_reply":"2024-02-16T03:37:20.717525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Reshape the input data to have a 2D shape\nX_train_reshaped = X_train.reshape(X_train.shape[0], -1)\nX_test_reshaped = X_test.reshape(X_test.shape[0], -1)\n\n# Initialize Decision Tree Classifier\ndt_classifier = DecisionTreeClassifier(random_state=42)\n\n# Train Decision Tree Classifier\ndt_classifier.fit(X_train_reshaped, y_train)\n\n# Make predictions on the testing data\ny_pred_dt = dt_classifier.predict(X_test_reshaped)\n\n# Calculate accuracy for Decision Tree\naccuracy_dt = accuracy_score(y_test, y_pred_dt)\nprint(\"Decision Tree Accuracy:\", accuracy_dt)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:38:21.814795Z","iopub.execute_input":"2024-02-16T03:38:21.815480Z","iopub.status.idle":"2024-02-16T03:38:42.812149Z","shell.execute_reply.started":"2024-02-16T03:38:21.815433Z","shell.execute_reply":"2024-02-16T03:38:42.810935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\n# Load the test data\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_data.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:38:56.952957Z","iopub.execute_input":"2024-02-16T03:38:56.953495Z","iopub.status.idle":"2024-02-16T03:38:59.132140Z","shell.execute_reply.started":"2024-02-16T03:38:56.953452Z","shell.execute_reply":"2024-02-16T03:38:59.130970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set\ny_test_pred = clf.predict(test_data)\n\n# Ensure y_test_pred is 1-dimensional\ny_test_pred = np.argmax(y_test_pred, axis=1)\n\n# Generate a submission file\nsubmission = pd.DataFrame({'ImageId': range(1, len(y_test_pred)+1), 'Label': y_test_pred})\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:39:02.466102Z","iopub.execute_input":"2024-02-16T03:39:02.466595Z","iopub.status.idle":"2024-02-16T03:39:02.883115Z","shell.execute_reply.started":"2024-02-16T03:39:02.466561Z","shell.execute_reply":"2024-02-16T03:39:02.881402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}