File size: 3,970 Bytes
ad58fa3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
{
"cells": [
{
"cell_type": "markdown",
"id": "0ca77529",
"metadata": {},
"source": [
"---\n",
"# **Embeddings Notebook for scikit-learn/iris dataset**\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "ca62cfc7",
"metadata": {},
"source": [
"## 1. Setup necessary libraries and load the dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80132099",
"metadata": {},
"outputs": [],
"source": [
"# Install and import necessary libraries.\n",
"!pip install pandas sentence-transformers faiss-cpu"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd10aa7b",
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer\n",
"import faiss"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01fb2f8e",
"metadata": {},
"outputs": [],
"source": [
"# Load the dataset as a DataFrame\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"hf://datasets/scikit-learn/iris/Iris.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d16d4199",
"metadata": {},
"outputs": [],
"source": [
"# Specify the column name that contains the text data to generate embeddings\n",
"column_to_generate_embeddings = 'Species'"
]
},
{
"cell_type": "markdown",
"id": "01765f8f",
"metadata": {},
"source": [
"## 2. Loading embedding model and creating FAISS index"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30c844c1",
"metadata": {},
"outputs": [],
"source": [
"# Remove duplicate entries based on the specified column\n",
"df = df.drop_duplicates(subset=column_to_generate_embeddings)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c452fd90",
"metadata": {},
"outputs": [],
"source": [
"# Convert the column data to a list of text entries\n",
"text_list = df[column_to_generate_embeddings].tolist()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f402d7a5",
"metadata": {},
"outputs": [],
"source": [
"# Specify the embedding model you want to use\n",
"model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78bdea9c",
"metadata": {},
"outputs": [],
"source": [
"vectors = model.encode(text_list)\n",
"vector_dimension = vectors.shape[1]\n",
"\n",
"# Initialize the FAISS index with the appropriate dimension (384 for this model)\n",
"index = faiss.IndexFlatL2(vector_dimension)\n",
"\n",
"# Encode the text list into embeddings and add them to the FAISS index\n",
"index.add(vectors)"
]
},
{
"cell_type": "markdown",
"id": "a55a9c05",
"metadata": {},
"source": [
"## 3. Perform a text search"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9036d2e7",
"metadata": {},
"outputs": [],
"source": [
"# Specify the text you want to search for in the list\n",
"text_to_search = text_list[0]\n",
"print(f\"Text to search: {text_to_search}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f6f304f",
"metadata": {},
"outputs": [],
"source": [
"# Generate the embedding for the search query\n",
"query_embedding = model.encode([text_to_search])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d1857fe",
"metadata": {},
"outputs": [],
"source": [
"# Perform the search to find the 'k' nearest neighbors (adjust 'k' as needed)\n",
"D, I = index.search(query_embedding, k=10)\n",
"\n",
"# Print the similar documents\n",
"print(f\"Similar documents: {[text_list[i] for i in I[0]]}\")"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
|