{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Priyesh Dave",
  "title_listed": "Day 16: Cosine Similarity and Semantic Search Basics",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "技术教程",
  "is_paid_flag": "否",
  "url": "https://dev.to/priyeshdave6/day-16-cosine-similarity-and-semantic-search-basics-2pje",
  "publish_date": "2026-09-12",
  "corpus_stage": "",
  "notes": "",
  "id": "c2f3c304dda2",
  "collected_at": "2026-10-02T13:49:44",
  "title": "Day 16: Cosine Similarity and Semantic Search Basics - DEV Community",
  "body_markdown": "Previously, on Day 15:\nExplained why visualizing word embeddings is useful, introduced the challenges of high-dimensional data, and compared two popular dimensionality reduction techniques—PCA and t-SNE—for plotting embeddings, with code examples for practical visualization.\nWhat Cosine Similarity Measures\nCosine similarity measures how closely two vectors point in the same direction, regardless of their length.\nPicture a vector as an arrow from the origin in space. Cosine similarity looks at the angle between two such arrows. If they point almost the same way, the angle is small, and cosine similarity is close to 1. If they’re at right angles (90 degrees), similarity is 0. If they point in opposite directions, similarity is -1.\nFor word embeddings, this is important. Embeddings for similar words—like \"dog\" and \"puppy\"—point in similar directions, even if their lengths are different. Cosine similarity captures this. By contrast, Euclidean distance measures the straight-line distance between the tips of the arrows. That means two vectors can be far apart (large Euclidean distance) but still point in nearly the same direction (high cosine similarity).\nIn natural language processing (NLP), direction typically represents meaning. Magnitude is usually less important.\nHow to Compute Cosine Similarity\nGiven two vectors, A and B:\nCosine similarity = (A · B) / (||A|| * ||B||)\nThe dot product (A · B) multiplies each pair of components and adds up the results.\n||A|| means the norm (length) of vector A: take each component, square it, add them up, then take the square root.\nThe denominator normalizes by length, so only the angle matters.\nThink of two people tossing darts from the center of a dartboard. If their darts go in nearly the same direction, cosine similarity is high—no matter how far they throw.\nThe result ranges from -1 (opposite directions) to 1 (same direction). For word embeddings, values tend to be above 0 unless the words are totally unrelated or opposites.\nWhy normalize? Without normalization, longer vectors would always seem more similar to everything else, regardless of actual direction.\nStep-by-Step Example\nSuppose:\nA = [1, 2]\nB = [2, 4]\nDot product:\n(1 × 2) + (2 × 4) = 2 + 8 = 10\nNorm of A:\nsqrt(1² + 2²) = sqrt(1 + 4) = sqrt(5) ≈ 2.236\nNorm of B:\nsqrt(2² + 4²) = sqrt(4 + 16) = sqrt(20) ≈ 4.472\nDivide:\n10 / (2.236 × 4.472) ≈ 10 / 10 = 1\nScore of 1: arrows point exactly the same way.\nExample: Comparing Words with Embeddings\nSuppose you have simple 2D vectors for some words:\n“cat” = [1, 2]\n“dog” = [0.9, 2.1]\n“car” = [-2, 0.5]\n“apple” = [0.2, -1]\nTo find the word most similar to \"cat,\" compute cosine similarities:\ncat vs dog: dot = (1 × 0.9) + (2 × 2.1) = 0.9 + 4.2 = 5.1\nnorms: sqrt(1²+2²) ≈ 2.236, sqrt(0.9²+2.1²) ≈ 2.293\nsimilarity = 5.1 / (2.236 × 2.293) ≈ 5.1 / 5.12 ≈ 0.996\ncat vs car: dot = (1 × -2) + (2 × 0.5) = -2 + 1 = -1\nnorms: 2.236, sqrt(4 + 0.25) ≈ 2.06\nsimilarity = -1 / (2.236 × 2.06) ≈ -1 / 4.605 ≈ -0.217\ncat vs apple: (1 × 0.2) + (2 × -1) = 0.2 - 2 = -1.8\nnorms: 2.236, sqrt(0.2² + 1²) = sqrt(0.04 + 1) ≈ 1.02\nsimilarity = -1.8 / (2.236 × 1.02) ≈ -1.8 / 2.28 ≈ -0.789\n“Dog” is almost perfectly aligned with “cat.” “Car” and “apple” are much less similar, with negative scores showing they point in different or even opposite directions.\nSemantic Search Basics\nTraditional keyword search matches exact words or phrases.\nSemantic search uses embeddings—vectors that represent words, sentences, or documents. It finds items whose embeddings are close in direction (high cosine similarity). This means you can find results that match the meaning, not just the words.\nExample:\nWhen searching \"quick animal,\" a keyword search falls short. A semantic search matches \"fast dog,\" \"speedy fox,\" or \"cheetah\" because their embeddings are nearby in vector space.\nSemantic search works at many levels:\nWord-level:\nFind synonyms or related words.\nSentence/document-level:\nRetrieve passages with related meaning, even if phrased differently.\nThe key mechanism is comparing embedding vectors, almost always with cosine similarity.\nReal-World Example: Simple Semantic Search in Python\nHere’s a minimal Python function to find the most semantically similar words to a query, using cosine similarity and small sample embeddings:\nimport\nnumpy\nas\nnp\ndef\nmost_similar\n(\nquery_word\n,\nvocab\n,\nembeddings\n,\ntop_n\n=\n3\n):\n# Get vector for the query word\nif\nquery_word\nnot\nin\nvocab\n:\nraise\nValueError\n(\n'\nQuery word not in vocabulary!\n'\n)\nquery_vec\n=\nembeddings\n[\nvocab\n.\nindex\n(\nquery_word\n)]\n# Normalize query vector\nquery_vec\n=\nquery_vec\n/\nnp\n.\nlinalg\n.\nnorm\n(\nquery_vec\n)\nsimilarities\n=\n[]\nfor\nword\n,\nvec\nin\nzip\n(\nvocab\n,\nembeddings\n):\n# Normalize each embedding\nvec_norm\n=\nvec\n/\nnp\n.\nlinalg\n.\nnorm\n(\nvec\n)\n# Cosine similarity (since normalized)\nsim\n=\nnp\n.\ndot\n(\nquery_vec\n,\nvec_norm\n)\nsimilarities\n.\nappend\n((\nword\n,\nsim\n))\n# Sort by similarity, descending, skip the query word itself\nsimilarities\n=\nsorted\n(\nsimilarities\n,\nkey\n=\nlambda\nx\n:\nx\n[\n1\n],\nreverse\n=\nTrue\n)\nreturn\n[\nw\nfor\nw\n,\ns\nin\nsimilarities\nif\nw\n!=\nquery_word\n][:\ntop_n\n]\n# Example usage:\nvocab\n=\n[\n'\ncat\n'\n,\n'\ndog\n'\n,\n'\ncar\n'\n,\n'\napple\n'\n]\nembeddings\n=\nnp\n.\narray\n([\n[\n1\n,\n2\n],\n# cat\n[\n0.9\n,\n2.1\n],\n# dog\n[\n-\n2\n,\n0.5\n],\n# car\n[\n0.2\n,\n-\n1\n],\n# apple\n])\nprint\n(\nmost_similar\n(\n'\ncat\n'\n,\nvocab\n,\nembeddings\n,\ntop_n\n=\n2\n))\n# Output: ['dog', 'car']\nEnter fullscreen mode\nExit fullscreen mode\nThis function:\nLooks up the queried word’s vector.\nComputes cosine similarity with every other word.\nReturns the top-N most similar words by meaning.\nThe same logic, scaled up, forms the engine of modern semantic search systems. The only difference is bigger vectors and much larger vocabularies.\nKey Takeaways\nCosine similarity measures the angle between vectors, ignoring magnitude.\nWord embeddings with similar meaning point in similar directions in vector space.\nSemantic search uses cosine similarity to match meanings, not just keywords.\nNegative cosine scores indicate opposite or unrelated meanings between embeddings.\nCosine similarity is commonly used for comparing sentences, documents, or words in NLP.\nTry It Yourself\nCreate an array of at least four sample word embeddings—either randomly generated or manually assigned—and define a corresponding vocabulary list. Write a short Python script that, given any word in your vocabulary, computes cosine similarity with all other words and prints out the three most semantically similar words. Try this for at least two different query words.\nFurther Resources\n🎥\nSemantic Search using Embeddings in Python | Cosine Similarity Explained\n🎥\nSemantic Search Fundamentals (Video)\n📄\nHow to Build Semantic Search with Embeddings: a Practical Walkthrough\n📘\nLet’s Understand Search — Qdrant (Module 1)\n📄\nEmbeddings and Semantic Search — Praveen T N\nComing up on Day 17:\nNeural Networks Refresher for NLP Engineers",
  "metadata": {
    "title": "Day 16: Cosine Similarity and Semantic Search Basics - DEV Community",
    "description": "Previously, on Day 15: Explained why visualizing word embeddings is useful, introduced the challenges... Tagged with ai, nlp, genai, 100daysofcode.",
    "keywords": "ai, nlp, genai, 100daysofcode, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/priyeshdave6/day-16-cosine-similarity-and-semantic-search-basics-2pje",
    "og:title": "Day 16: Cosine Similarity and Semantic Search Basics",
    "og:description": "Previously, on Day 15: Explained why visualizing word embeddings is useful, introduced the challenges...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1200,height=627,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdw02eyl0qzkf1mx5j1ta.png",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Day 16: Cosine Similarity and Semantic Search Basics",
    "twitter:description": "Previously, on Day 15: Explained why visualizing word embeddings is useful, introduced the challenges...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1200,height=627,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdw02eyl0qzkf1mx5j1ta.png"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/c2f3c304dda2/code/body-000.txt",
      "chars": 1081,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [
    {
      "url": "https://www.youtube.com/watch?v=vu2gtfe2oHo",
      "status": "ok",
      "local_path": "attachments/c2f3c304dda2/videos/vu2gtfe2oHo.mp4",
      "bytes": 12512663,
      "valid": true,
      "height": 720,
      "width": 1280,
      "duration": "337.841000"
    }
  ],
  "links": [
    {
      "href": "https://www.youtube.com/watch?v=vu2gtfe2oHo",
      "label": "Semantic Search using Embeddings in Python | Cosine Similarity Explained",
      "type": "a"
    },
    {
      "href": "https://mixpeek.com/education/videos/semantic-search-fundamentals",
      "label": "Semantic Search Fundamentals (Video)",
      "type": "a"
    },
    {
      "href": "https://www.andergrove.com/tools/cosine-similarity/guide/",
      "label": "How to Build Semantic Search with Embeddings: a Practical Walkthrough",
      "type": "a"
    },
    {
      "href": "https://qdrant.tech/course/beginners/module-1/",
      "label": "Let’s Understand Search — Qdrant (Module 1)",
      "type": "a"
    },
    {
      "href": "https://praveentn.live/learn/concepts/embeddings-semantic-search",
      "label": "Embeddings and Semantic Search — Praveen T N",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:48:08Z",
  "source_url": "https://dev.to/priyeshdave6/day-16-cosine-similarity-and-semantic-search-basics-2pje",
  "papers": [],
  "actual_video_files": [
    {
      "local_path": "attachments/c2f3c304dda2/videos/vu2gtfe2oHo.mp4",
      "bytes": 12512663,
      "valid": true,
      "height": 720,
      "width": 1280,
      "duration": "337.841000",
      "status": "ok"
    }
  ],
  "body_chars": 6863,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/c2f3c304dda2/pages/source.html",
  "local_html": "attachments/c2f3c304dda2/pages/article.html",
  "body_base_path": "attachments/c2f3c304dda2/pages/",
  "body_markdown_path": "attachments/c2f3c304dda2/pages/article.md"
}