PageSourceSearch

https://norahsakal.com/assets/js/e506ac80.a19c8521.js

js norahsakal.com collected 2026-10-02 07:36:33 UTC 7,787 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkblog_folder=self.webpackChunkblog_folder||[]).push([[2651],{50889:(e,i,t)=>{t.r(i),t.d(i,{assets:()=>c,contentTitle:()=>o,default:()=>h,frontMatter:()=>a,metadata:()=>s,toc:()=>l});const s=JSON.parse('{"id":"ai_for_ecommerce/aws_titan_image_embeddings/what_is_cosine_similarity","title":"What is cosine similarity?","description":"When we turn customer questions and product descriptions into vectors, we move into a space where we can directly compare and measure how similar items are.","source":"@site/docs/ai_for_ecommerce/aws_titan_image_embeddings/9_what_is_cosine_similarity.md","sourceDirName":"ai_for_ecommerce/aws_titan_image_embeddings","slug":"/ai_for_ecommerce/aws_titan_image_embeddings/what_is_cosine_similarity","permalink":"/tutorials/ai_for_ecommerce/aws_titan_image_embeddings/what_is_cosine_similarity","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":9,"frontMatter":{"title":"What is cosine similarity?"},"sidebar":"tutorialSidebar","previous":{"title":"What are embeddings?","permalink":"/tutorials/ai_for_ecommerce/aws_titan_image_embeddings/what_are_embeddings"},"next":{"title":"Generate product embeddings","permalink":"/tutorials/ai_for_ecommerce/aws_titan_image_embeddings/generate_product_embeddings"}}');var r=t(74848),n=t(28453);const a={title:"What is cosine similarity?"},o="Cosine Similarity in Vector Space",c={},l=[{value:"Understanding Cosine Similarity",id:"understanding-cosine-similarity",level:2},{value:"Simplifying Search with Cosine Similarity",id:"simplifying-search-with-cosine-similarity",level:2},{value:"Example: red yoga pants",id:"example-red-yoga-pants",level:3},{value:"Cosine similarity for e-commerce",id:"cosine-similarity-for-e-commerce",level:2}];function m(e){const i={h1:"h1",h2:"h2",h3:"h3",header:"header",img:"img",p:"p",strong:"strong",...(0,n.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(i.header,{children:(0,r.jsx)(i.h1,{id:"cosine-similarity-in-vector-space",children:"Cosine Similarity in Vector Space"})}),"\n",(0,r.jsx)(i.p,{children:"When we turn customer questions and product descriptions into vectors, we move into a space where we can directly compare and measure how similar items are."}),"\n",(0,r.jsx)(i.p,{children:"But how exactly do we compare these vectors and get the right product?"}),"\n",(0,r.jsxs)(i.p,{children:[(0,r.jsx)(i.img,{alt:"How exactly do we compare these vectors?",src:t(74736).A+"",title:"How exactly do we compare these vectors?",width:"1920",height:"1080"}),"\n",(0,r.jsx)("div",{style:{textAlign:"center",marginBottom:"20px"},children:"How exactly do we compare these vectors?"})]}),"\n",(0,r.jsx)(i.p,{children:"This is where we'll use cosine similarity."}),"\n",(0,r.jsx)(i.h2,{id:"understanding-cosine-similarity",children:"Understanding Cosine Similarity"}),"\n",(0,r.jsx)(i.p,{children:"Cosine similarity measures how close two vectors are by calculating the cosine of the angle between them in a multi-dimensional space."}),"\n",(0,r.jsxs)(i.p,{children:[(0,r.jsx)(i.img,{alt:"Cosine determines how similar two vectors are",src:t(131).A+"",title:"Cosine determines how similar two vectors are",width:"1920",height:"1080"}),"\n",(0,r.jsx)("div",{style:{textAlign:"center",marginBottom:"20px"},children:"Cosine determines how similar two vectors are"})]}),"\n",(0,r.jsx)(i.p,{children:"This technique checks the cosine of the angle connecting two points; if the vectors match, the angle is 0 degrees, making the cosine value 1, which means they're exactly alike."}),"\n",(0,r.jsxs)(i.p,{children:["For example, if we match the vector for a customer's request for ",(0,r.jsx)(i.strong,{children:'"red yoga pants"'})," against the vector for red yoga pants, the small angle suggests they're very similar, giving a cosine score near 1."]}),"\n",(0,r.jsx)(i.p,{children:"On the other hand, if we compare it with a vector for yellow yoga pants, the bigger angle means they're not as similar, pushing the cosine score towards -1."}),"\n",(0,r.jsx)(i.h2,{id:"simplifying-search-with-cosine-similarity",children:"Simplifying Search with Cosine Similarity"}),"\n",(0,r.jsx)(i.p,{children:"Using cosine similarity, we can compare a customer's search query in vector form against all product vectors in our database to rank products. The closer a product's vector is to the query vector, indicated by a higher cosine score, the higher it ranks as a match to the customer's needs."}),"\n",(0,r.jsx)(i.h3,{id:"example-red-yoga-pants",children:"Example: red yoga pants"}),"\n",(0,r.jsxs)(i.p,{children:["Let's say a customer searches for ",(0,r.jsx)(i.strong,{children:"red yoga pants"}),". Cosine similarity helps our search algorithm prioritize red yoga pants by recognizing their vector is closer (has a higher cosine similarity score) to the customer's query vector:"]}),"\n",(0,r.jsxs)(i.p,{children:[(0,r.jsx)(i.img,{alt:"High Cosine Similarity: Matching Customer Queries",src:t(41511).A+"",title:"High Cosine Similarity: Matching Customer Queries",width:"1920",height:"1080"}),"\n",(0,r.jsx)("div",{style:{textAlign:"center",marginBottom:"20px"},children:"High Cosine Similarity: Matching Customer Queries "})]}),"\n",(0,r.jsxs)(i.p,{children:["In contrast, a less similar product, like ",(0,r.jsx)(i.strong,{children:"yellow yoga pants"}),", has a wider angle to the query vector, resulting in a lower similarity score:"]}),"\n",(0,r.jsxs)(i.p,{children:[(0,r.jsx)(i.img,{alt:"Large Angle, Low Similarity: Divergent Product Match",src:t(64254).A+"",title:"Large Angle, Low Similarity: Divergent Product Match",width:"1920",height:"1080"}),"\n",(0,r.jsx)("div",{style:{textAlign:"center",marginBottom:"20px"},children:"Large Angle, Low Similarity: Divergent Product Match"})]}),"\n",(0,r.jsx)(i.h2,{id:"cosine-similarity-for-e-commerce",children:"Cosine similarity for e-commerce"}),"\n",(0,r.jsx)(i.p,{children:"This is why embeddings and cosine similarity are game-changers for semantic search. They allow our application to grasp the context and nuances of customer questions, delivering semantically relevant products beyond just keyword matches:"}),"\n",(0,r.jsxs)(i.p,{children:[(0,r.jsx)(i.img,{alt:"High Cosine Similarity: Matching Customer Queries",src:t(75795).A+"",title:"High Cosine Similarity: Matching Customer Queries",width:"720",height:"405"}),"\n",(0,r.jsx)("div",{style:{textAlign:"center",marginBottom:"20px"},children:"High Cosine Similarity: Matching Customer Queries"})]}),"\n",(0,r.jsx)(i.p,{children:"Let's go back to our product catalog of bags and create embeddings for each product in the next section, so we can match them with customer inquiries."})]})}function h(e={}){const{wrapper:i}={...(0,n.R)(),...e.components};return i?(0,r.jsx)(i,{...e,children:(0,r.jsx)(m,{...e})}):m(e)}},41511:(e,i,t)=>{t.d(i,{A:()=>s});const s=t.p+"assets/images/10_small_angle-25e5917aef20829c9bfe0219efd488fb.png"},64254:(e,i,t)=>{t.d(i,{A:()=>s});const s=t.p+"assets/images/11_large_angle-006a67fbf131c5f04e83851996a869f8.png"},75795:(e,i,t)=>{t.d(i,{A:()=>s});
1const s=t.p+"assets/images/12_vectors_embeddings_gif-0197106a9facd3b95f2fe917985e5fe8.gif"},74736:(e,i,t)=>{t.d(i,{A:()=>s});const s=t.p+"assets/images/8_how_compare_vectors-7d889eef6a862dc5ca103f155cf2fff6.png"},131:(e,i,t)=>{t.d(i,{A:()=>s});const s=t.p+"assets/images/9_cosine_similarity-52e5452147f606f06cb304febe0c9825.png"},28453:(e,i,t)=>{t.d(i,{R:()=>a,x:()=>o});var s=t(96540);const r={},n=s.createContext(r);function a(e){const i=s.useContext(n);return s.useMemo((function(){return"function"==typeof e?e(i):{...i,...e}}),[i,e])}function o(e){let i;return i=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:a(e.components),s.createElement(n.Provider,{value:i},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.