{"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":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Digit Recognizer Kaggle Competition: Vector Database Notebook","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-30T10:58:59.527784Z","iopub.execute_input":"2023-12-30T10:58:59.528192Z","iopub.status.idle":"2023-12-30T10:58:59.544393Z","shell.execute_reply.started":"2023-12-30T10:58:59.528156Z","shell.execute_reply":"2023-12-30T10:58:59.543053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install chromadb","metadata":{"execution":{"iopub.status.busy":"2023-12-30T10:58:59.547557Z","iopub.execute_input":"2023-12-30T10:58:59.548030Z","iopub.status.idle":"2023-12-30T10:59:14.577471Z","shell.execute_reply.started":"2023-12-30T10:58:59.547988Z","shell.execute_reply":"2023-12-30T10:59:14.576051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport chromadb\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom random import randint","metadata":{"execution":{"iopub.status.busy":"2023-12-30T10:59:14.581991Z","iopub.execute_input":"2023-12-30T10:59:14.582367Z","iopub.status.idle":"2023-12-30T10:59:15.672518Z","shell.execute_reply.started":"2023-12-30T10:59:14.582330Z","shell.execute_reply":"2023-12-30T10:59:15.671432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the csv's Data with Pandas\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\n# Prepare Train Vectors\ntrain_vectors = train_data.drop(\"label\", axis=1).values","metadata":{"execution":{"iopub.status.busy":"2023-12-30T10:59:15.674796Z","iopub.execute_input":"2023-12-30T10:59:15.675323Z","iopub.status.idle":"2023-12-30T10:59:21.319321Z","shell.execute_reply.started":"2023-12-30T10:59:15.675292Z","shell.execute_reply":"2023-12-30T10:59:21.318259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train ChromaDB or Any Other Model (if applicable)\n# Establish a connection to ChromaDB\nchroma_client = chromadb.Client()\n\n# Check if the \"MNIST\" collection exists, create it if not\ncollection_name = \"MNIST\"\ntry:\n    collection = chroma_client.get_collection(collection_name)\nexcept Exception as e:\n    print(f\"Error: {e}\")\n    print(\n        f\"Collection '{collection_name}' doesn't exist or couldn't be accessed. Creating a new one...\"\n    )\n\n    # Create the collection\n    collection = chroma_client.create_collection(\n        name=collection_name, metadata={\"hnsw:space\": \"cosine\"}\n    )","metadata":{"execution":{"iopub.status.busy":"2023-12-30T10:59:21.320605Z","iopub.execute_input":"2023-12-30T10:59:21.321170Z","iopub.status.idle":"2023-12-30T10:59:21.634154Z","shell.execute_reply.started":"2023-12-30T10:59:21.321132Z","shell.execute_reply":"2023-12-30T10:59:21.633277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare Reference Vectors\nref_labels = train_data[\"label\"].values\nref_imgs = train_data.drop(\"label\", axis=1).values\ntest_imgs = test_data.values\n\n# Convert data to lists to satisfy ChromaDB\nref_digits = [d.tolist() for d in ref_imgs]\ntest_digits = [d.tolist() for d in test_imgs]\n\n# Feed reference digits into ChromaDB\nfor i in tqdm(range(len(ref_digits))):\n    collection.add(\n        embeddings=ref_digits[i],\n        metadatas=[{\"label\": str(ref_labels[i])}],\n        ids=[str(i)],\n    )","metadata":{"execution":{"iopub.status.busy":"2023-12-30T10:59:21.635383Z","iopub.execute_input":"2023-12-30T10:59:21.635895Z","iopub.status.idle":"2023-12-30T11:00:53.289323Z","shell.execute_reply.started":"2023-12-30T10:59:21.635846Z","shell.execute_reply":"2023-12-30T11:00:53.288454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DIGIT_SHAPE = (28, 28)\n\n# Define the Prediction Function \ndef predict(img, show=False):\n    if isinstance(img, np.ndarray):\n        img_list = img.tolist()  # Convert numpy array to list\n    else:\n        img_list = img\n    res = collection.query(query_embeddings=img_list, n_results=1)\n    if show:\n        plt.imshow(np.array(img_list).reshape(DIGIT_SHAPE), cmap=\"hot_r\")\n        \n        # Remove spines and ticks\n        ax = plt.gca()\n        ax.set_xticks([])\n        ax.set_yticks([])\n        \n        # Remove spines using a for loop\n        ax = plt.gca()\n        for spine in [\"top\", \"right\", \"bottom\", \"left\"]:\n            ax.spines[spine].set_visible(False)\n        \n    return res[\"metadatas\"][0][0][\"label\"]\n\n# Example prediction and display\nprint(\"Label:\")\ntest_digit = randint(0, len(test_digits))\npredict(test_digits[test_digit], show=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-30T11:00:53.290627Z","iopub.execute_input":"2023-12-30T11:00:53.291773Z","iopub.status.idle":"2023-12-30T11:00:53.433470Z","shell.execute_reply.started":"2023-12-30T11:00:53.291736Z","shell.execute_reply":"2023-12-30T11:00:53.431962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate Accuracy on Train Data\ntrue_train_labels = train_data[\"label\"].values\n\npredicted_train_labels = []\n\nfor train_vector in train_vectors:\n    predicted_train_label = predict(train_vector)\n    predicted_train_labels.append(predicted_train_label)\n\n# Compare predicted labels with true labels on train data\ncorrect_train_predictions = np.sum(np.array_equiv(predicted_train_labels, true_train_labels))\ntotal_train_predictions = len(true_train_labels)\n\nfor i in range(10):  # Print the first 10 predictions for debugging\n    print(\n        f\"True Label: {true_train_labels[i]}, Predicted Label: {predicted_train_labels[i]}\"\n    )","metadata":{"execution":{"iopub.status.busy":"2023-12-30T11:00:53.435790Z","iopub.execute_input":"2023-12-30T11:00:53.437357Z","iopub.status.idle":"2023-12-30T11:01:56.233983Z","shell.execute_reply.started":"2023-12-30T11:00:53.437294Z","shell.execute_reply":"2023-12-30T11:01:56.232676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features from test data\ntest_vectors = test_data.values\n\n# Assuming you have a predict function that takes an image vector and returns the predicted label\npredicted_labels_test = []\n\n# Predict labels for each test vector\nfor test_vector in test_vectors:\n    predicted_label = predict(test_vector)\n    predicted_labels_test.append(predicted_label)","metadata":{"execution":{"iopub.status.busy":"2023-12-30T11:01:56.235258Z","iopub.execute_input":"2023-12-30T11:01:56.235578Z","iopub.status.idle":"2023-12-30T11:02:38.105451Z","shell.execute_reply.started":"2023-12-30T11:01:56.235550Z","shell.execute_reply":"2023-12-30T11:02:38.104145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a submission file\nsubmission_df = pd.DataFrame(\n    {\"ImageId\": range(1, len(test_data) + 1), \"Label\": predicted_labels_test}\n)\nsubmission_df.to_csv(\"submission_db.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-30T11:02:38.108350Z","iopub.execute_input":"2023-12-30T11:02:38.108733Z","iopub.status.idle":"2023-12-30T11:02:38.186340Z","shell.execute_reply.started":"2023-12-30T11:02:38.108692Z","shell.execute_reply":"2023-12-30T11:02:38.185197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Result in Kaggle: 0.96950","metadata":{}}]}