距離指標とインデックス作成の基礎
ベクトルの類似度を定義する距離指標と、大規模環境でベクトル検索を高速化する近似最近傍インデックスについて学びます。
「距離指標とインデックス作成の基礎」はCoddyKit上の無料Vector Databases: Pinecone, Weaviate & pgvectorレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはVector Databases: Pinecone, Weaviate & pgvector学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Measuring Closeness
A vector DB finds vectors close to your query, but 'close' is defined by a distance metric — and the metric you pick shapes what counts as similar.
Euclidean Distance
Euclidean (L2) distance is the straight-line distance between two points: smaller means closer. Run the example to see it.
import math
def l2(a, b):
return math.sqrt(sum((x-y)**2 for x, y in zip(a, b)))
print(round(l2([0,0],[3,4]), 1))Cosine Similarity
Cosine similarity measures the angle between vectors, ignoring magnitude. It's the go-to for text embeddings, since direction carries the meaning.
import math
def cosine(a, b):
dot = sum(x*y for x, y in zip(a, b))
na = math.sqrt(sum(x*x for x in a))
nb = math.sqrt(sum(y*y for y in b))
return dot / (na*nb)
print(round(cosine([1,0],[1,1]), 3))Dot Product
The dot product blends angle and magnitude. With normalized vectors it equals cosine similarity — which is why many systems normalize, then use dot product for speed.
Choosing a Metric
Match the metric to what your model was trained for: text usually cosine, normalized vectors dot product, some image or geo data Euclidean. A mismatch silently hurts results.
The Brute-Force Problem
Brute force — comparing the query to every stored vector — is exact but slow: millions of vectors mean millions of comparisons per query. It doesn't scale.
Approximate Nearest Neighbor
ANN indexes trade a sliver of accuracy for massive speed by cleverly skipping most candidates. Recall stays high while latency drops by orders of magnitude.
HNSW Indexes
HNSW builds a layered graph you navigate coarse to fine for excellent recall and speed — the default in many vector DBs, tunable via ef and M.
IVF Indexes
IVF clusters vectors into buckets and only searches the ones nearest your query. Fewer probes mean faster search, at a small accuracy cost.
The Recall-Speed Trade-off
Every ANN index exposes knobs that trade recall for speed. Searching more candidates raises both recall and latency. Tune to your accuracy target, then push speed.
Putting It Together
Putting it together: pick the right distance metric for your embeddings, then use an ANN index (HNSW or IVF) to query fast at scale, tuning the recall-speed knobs.
Quick Check
Test your understanding of metrics and indexes.
Recap
Recap: the core distance metrics (Euclidean, cosine, dot product) — text usually cosine — plus ANN indexes like HNSW and IVF that trade a little recall for big speed.
よくある質問
「距離指標とインデックス作成の基礎」レッスンは無料ですか?
はい。「距離指標とインデックス作成の基礎」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Vector Databases: Pinecone, Weaviate & pgvectorコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Vector Databases: Pinecone, Weaviate & pgvectorコースには全4レッスンが含まれています。
「距離指標とインデックス作成の基礎」で何を学びますか?
ベクトルの類似度を定義する距離指標と、大規模環境でベクトル検索を高速化する近似最近傍インデックスについて学びます。 ブラウザで直接実行するハンズオンコードでVector Databases: Pinecone, Weaviate & pgvectorを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Vector Databases: Pinecone, Weaviate & pgvectorを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのVector Databases: Pinecone, Weaviate & pgvectorは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「距離指標とインデックス作成の基礎」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このVector Databases: Pinecone, Weaviate & pgvectorレッスンでコードを書いて実行できますか?
はい。すべてのVector Databases: Pinecone, Weaviate & pgvectorレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ベクトルデータベースとは
- 埋め込み:中核概念
- 類似度検索の仕組み
- 距離指標とインデックス作成の基礎