{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","execution":{"iopub.status.busy":"2024-04-15T06:46:20.732865Z","iopub.execute_input":"2024-04-15T06:46:20.733212Z","iopub.status.idle":"2024-04-15T06:46:20.743316Z","shell.execute_reply.started":"2024-04-15T06:46:20.733183Z","shell.execute_reply":"2024-04-15T06:46:20.742213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# visit my blog:\n# http://mahabytes.com/2024/03/30/used-car-value-prediction-using-ai/****","metadata":{}},{"cell_type":"markdown","source":"# **Preparation**","metadata":{}},{"cell_type":"code","source":"# load the mnist training and test data CSV \n\ndata =pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n\nprint (\"the shape of data is \", data.shape )","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:46:20.747670Z","iopub.execute_input":"2024-04-15T06:46:20.747952Z","iopub.status.idle":"2024-04-15T06:46:24.591446Z","shell.execute_reply.started":"2024-04-15T06:46:20.747927Z","shell.execute_reply":"2024-04-15T06:46:24.590446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Digits data visualization","metadata":{}},{"cell_type":"code","source":"data.head","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:46:24.593119Z","iopub.execute_input":"2024-04-15T06:46:24.593445Z","iopub.status.idle":"2024-04-15T06:46:24.615268Z","shell.execute_reply.started":"2024-04-15T06:46:24.593406Z","shell.execute_reply":"2024-04-15T06:46:24.614285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\nfrom tensorflow.keras import models, layers\n\n# Load the MNIST training data CSV file\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n\n# Separate features (pixel values) and labels for training data\nX_train = train_data.drop(columns=['label'])  # Features\ny_train = train_data['label']  # Labels\n\n# Reshape the input data for CNN\nX_train_cnn = X_train.values.reshape(-1, 28, 28, 1)\n\n# Split the data into training and validation sets for CNN\nX_train_cnn, X_val_cnn, y_train, y_val = train_test_split(X_train_cnn, y_train, test_size=0.1, random_state=42)\n\n# Define the CNN model\n\ncnn_model = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(128, activation='relu'),\n    layers.Dropout(0.5),\n    layers.Dense(10, activation='softmax')\n])\n\n# Compile the CNN model\ncnn_model.compile(optimizer='adam',\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the CNN model\ncnn_model.fit(X_train_cnn, y_train, epochs=60, validation_data=(X_val_cnn, y_val))\n\n# Load the MNIST test data CSV file\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\n# Reshape the input data for CNN\nX_test_cnn = test_data.values.reshape(-1, 28, 28, 1)\n\n# Predict labels for the test data using CNN\ncnn_predictions = np.argmax(cnn_model.predict(X_test_cnn), axis=1)\n\n# Visualize some sample output images and predicted labels\nplt.figure(figsize=(10, 6))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(X_test_cnn[i].reshape(28, 28), cmap='gray')\n    plt.title(f\"CNN Prediction: {cnn_predictions[i]}\")\n    plt.axis('off')\nplt.suptitle('Sample Output Images and CNN Predictions', fontsize=16)\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:04:17.305333Z","iopub.execute_input":"2024-04-15T07:04:17.305701Z","iopub.status.idle":"2024-04-15T07:07:20.927961Z","shell.execute_reply.started":"2024-04-15T07:04:17.305673Z","shell.execute_reply":"2024-04-15T07:07:20.927016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB\n\n# Separate features (pixel values) and labels for training data\nX_train = train_data.drop(columns=['label'])  # Features\ny_train = train_data['label']  # Labels\n\n# Make sure X_train and y_train have the same number of samples\nassert len(X_train) == len(y_train), \"Number of samples in features and labels do not match.\"\n\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)\n\n# k-Nearest Neighbors (k-NN)\n\n# knn_model = KNeighborsClassifier(n_neighbors=9, weights='distance', metric='euclidean')\nknn_model = KNeighborsClassifier(n_neighbors=2, weights='distance', algorithm='brute', metric='euclidean')\n\nknn_model.fit(X_train, y_train)\n\n\n# Evaluate the k-NN model on the validation set\nknn_accuracy = knn_model.score(X_val, y_val)\nprint(\"Validation Accuracy (k-NN):\", knn_accuracy)\n\n\nknn_predictions = knn_model.predict(test_data)\n\n\n\n# Visualize some sample output images and predicted labels\nplt.figure(figsize=(10, 6))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(test_data.iloc[i].values.reshape(28, 28), cmap='gray')\n    plt.title(f\"RF Prediction: {knn_predictions[i]}\")\n    plt.axis('off')\nplt.suptitle('Sample Output Images and knn Predictions', fontsize=16)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:14:16.504853Z","iopub.execute_input":"2024-04-15T07:14:16.505228Z","iopub.status.idle":"2024-04-15T07:14:48.992861Z","shell.execute_reply.started":"2024-04-15T07:14:16.505199Z","shell.execute_reply":"2024-04-15T07:14:48.991920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # Combine predictions from CNN and knn\n","metadata":{}},{"cell_type":"code","source":"# Simple voting mechanism\nensemble_predictions = (cnn_predictions + knn_predictions) // 2 \npredicted_labels = ensemble_predictions\n# Visualize some sample output images and ensemble predictions\nplt.figure(figsize=(15, 9))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(test_data.iloc[i].values.reshape(28, 28), cmap='gray')\n    plt.title(f\"Ensemble Prediction: {ensemble_predictions[i]}\")\n    plt.axis('off')\nplt.suptitle('Sample Output Images and Ensemble Predictions', fontsize=16)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:15:02.567026Z","iopub.execute_input":"2024-04-15T07:15:02.567972Z","iopub.status.idle":"2024-04-15T07:15:03.303622Z","shell.execute_reply.started":"2024-04-15T07:15:02.567931Z","shell.execute_reply":"2024-04-15T07:15:03.302553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Weighted averaging for ensemble predictions\nensemble_predictions2 = ((0.90 * cnn_predictions) + (0.10 * knn_predictions))\n\npredicted_labels = ensemble_predictions2\n# Visualize some sample output images and ensemble predictions\nplt.figure(figsize=(15, 9))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(test_data.iloc[i].values.reshape(28, 28), cmap='gray')\n    plt.title(f\"Ensemble Prediction: {ensemble_predictions2[i]}\")\n    plt.axis('off')\nplt.suptitle('Sample Output Images and Ensemble Predictions', fontsize=16)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:22:57.507356Z","iopub.execute_input":"2024-04-15T07:22:57.507955Z","iopub.status.idle":"2024-04-15T07:22:58.186567Z","shell.execute_reply.started":"2024-04-15T07:22:57.507924Z","shell.execute_reply":"2024-04-15T07:22:58.185644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check dimensions of predictions\nif cnn_predictions.shape == knn_predictions.shape:\n    print(\"Dimensions of CNN and KNN predictions match.\")\nelse:\n    print(\"Dimensions of CNN and KNN predictions do not match.\")\n\n# Check range of values in predictions\nif (cnn_predictions.min() >= 0 and cnn_predictions.max() <= 9) and (knn_predictions.min() >= 0 and knn_predictions.max() <= 9):\n    print(\"Values in CNN and KNN predictions are within the expected range (0-9).\")\nelse:\n    print(\"Values in CNN and KNN predictions are outside the expected range.\")\n\n# Check dimensions and range of values for ensemble_predictions2\nif ensemble_predictions2.shape == cnn_predictions.shape:\n    print(\"Dimensions of ensemble_predictions2 match CNN predictions.\")\nelse:\n    print(\"Dimensions of ensemble_predictions2 do not match CNN predictions.\")\n\nif (ensemble_predictions2.min() >= 0 and ensemble_predictions2.max() <= 9):\n    print(\"Values in ensemble_predictions2 are within the expected range (0-9).\")\nelse:\n    print(\"Values in ensemble_predictions2 are outside the expected range.\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:22:29.093343Z","iopub.execute_input":"2024-04-15T07:22:29.094064Z","iopub.status.idle":"2024-04-15T07:22:29.102948Z","shell.execute_reply.started":"2024-04-15T07:22:29.094013Z","shell.execute_reply":"2024-04-15T07:22:29.101912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Weighted averaging for ensemble predictions\nensemble_predictions2 = (0.93 * cnn_predictions + 0.07 * knn_predictions)\n\n# Round ensemble predictions to the nearest integer\nensemble_predictions_rounded = np.round(ensemble_predictions2)\n\n# Prepare submission file\nsubmission_df = pd.DataFrame({'ImageId': range(1, len(ensemble_predictions_rounded) + 1),\n                              'Label': ensemble_predictions_rounded.astype(int)})\n\n# Save submission file\nsubmission_df.to_csv(\"submission_digit_recognizer.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:23:43.026471Z","iopub.execute_input":"2024-04-15T07:23:43.026820Z","iopub.status.idle":"2024-04-15T07:23:43.065966Z","shell.execute_reply.started":"2024-04-15T07:23:43.026792Z","shell.execute_reply":"2024-04-15T07:23:43.065244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # predictions = np.argmax(model.predict(X_testr_reshaped), axis=1)\n\n# image_ids = np.arange(1, len(ensemble_predictions2) + 1)\n\n# submission_df = pd.DataFrame({'ImageId': image_ids, 'Label': ensemble_predictions2})\n\n\n# submission_df.to_csv('submission_digit_recognizerzz.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:49:16.844595Z","iopub.execute_input":"2024-04-15T06:49:16.844916Z","iopub.status.idle":"2024-04-15T06:49:16.848989Z","shell.execute_reply.started":"2024-04-15T06:49:16.844887Z","shell.execute_reply":"2024-04-15T06:49:16.848045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Prepare submission file\n# submission_df = pd.DataFrame({'ImageId': range(1, len(predicted_labels) + 1),\n#                               'Label': predicted_labels})\n\n# # Save submission file\n# submission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:49:16.852307Z","iopub.execute_input":"2024-04-15T06:49:16.852887Z","iopub.status.idle":"2024-04-15T06:49:16.856713Z","shell.execute_reply.started":"2024-04-15T06:49:16.852860Z","shell.execute_reply":"2024-04-15T06:49:16.855817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}