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