Upload scikit-learn-iris-embeddings-c07c5c1b-d289-413e-85dd-5c70bb6466ec.ipynb with huggingface_hub
Browse files
scikit-learn-iris-embeddings-c07c5c1b-d289-413e-85dd-5c70bb6466ec.ipynb
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "fe385b51",
|
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": "c996c9a7",
|
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": "d7138bc0",
|
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": "f9ed7b2e",
|
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": "0b1b906a",
|
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": "0a2cb338",
|
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": "83486c49",
|
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": "1eb3cabe",
|
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": "7aef5a85",
|
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": "ac657435",
|
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": "c24a474e",
|
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": "62d9713b",
|
128 |
+
"metadata": {},
|
129 |
+
"source": [
|
130 |
+
"## 3. Perform a text search"
|
131 |
+
]
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"cell_type": "code",
|
135 |
+
"execution_count": null,
|
136 |
+
"id": "100a52bf",
|
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": "b0f61c41",
|
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": "a5df38d3",
|
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 |
+
}
|