Skip to main content
Welcome back. This tutorial assumes you already have LanceDB set up with vectors for several fruits and that a mango query vector is defined. We’ll:
  • Reload the stored fruit vectors from LanceDB,
  • Visualize vectors in 3D and 2D (Red vs Yellow) color-space,
  • Run three similarity/distance searches (Cosine similarity, Euclidean distance, Dot product),
  • Compare rankings and visualize metric differences.
If you’re following along in a fresh notebook, run each code block sequentially so variables (like fruit_vectors and mango_query) are available for plotting and metric computation.

1) Reload LanceDB and fruit vectors

Run this in a fresh notebook cell to ensure data is present:
This confirms the dataset is available and shows the query vector we will search with. Verify the loaded fruits and their vectors:
Example console output:

2) Visualize the vectors (3D and 2D projection)

Plot the fruit vectors in 3D color-space plus a 2D top-down projection (Red vs Yellow). This helps reason about which fruits should be nearest the mango query by color.
The image is a screenshot of a Jupyter Notebook displaying a 3D and 2D plot showing fruit vectors in color space, with fruits represented by blue dots and a mango query by a red star, illustrating clustering based on color similarity.
This visualization should make it intuitive which fruits are likely nearest the mango query (for example: papaya, peach, nectarine).

3) Define and compute three similarity/distance metrics

We will compare three commonly used metrics: Compute each metric and produce sorted rankings:
Print a formatted comparison of rankings:
Observed ordering (example from these vectors):
  • Cosine top 3: papaya, peach, pear
  • Euclidean top 3: papaya, peach, pear
  • Dot product top 3: pear, pineapple, peach
To extract the top-k results for quick comparison:
Note: all three metrics can often agree on which items are closest (e.g., papaya is near the top for cosine and Euclidean), but they can disagree on exact ordering because they emphasize different vector properties (direction vs magnitude).

4) Visualize metric scores

A bar chart or grouped bar plot makes it easier to compare scores across metrics and highlight how one metric (e.g., dot product) can favor high-magnitude vectors like pear.
The image shows a Jupyter notebook interface with a code snippet and visualizations comparing fruit similarity across three metrics: Cosine Similarity, Euclidean Distance, and Dot Product. The bars indicate that papaya is the top match across all metrics.

Summary and next steps

Similarity search powers recommendation engines and RAG (retrieval-augmented generation). In recommendations it matches queries to products and user embeddings (purchase history); in RAG it retrieves documents that ground generation, making ranking quality critical to final output quality.
Practical tips:
  • In recommendation systems, a short query like shoe combined with a user embedding quickly improves relevance (sports vs formal vs ski).
  • In RAG, better similarity ranking yields more useful supporting documents and better generated answers.
References:

Watch Video

Practice Lab