{"id":"cosine_distance","title":"Cosine distance","family":"lock-step","definition":"reference","intro_en":"Cosine comparison uses the angle between two complete sample vectors. It divides their dot product by both vector norms and leaves the mean level intact. The animation shows the signed coordinate products and their norm-scaled accumulation, then applies the selected angular or one-minus transform when this card reports a distance.","intro_zh":"余弦比较使用完整采样向量之间的夹角，以两向量范数乘积归一化点积，并保留均值水平。动图展示带符号坐标乘积及归一化累加；距离型条目随后应用反余弦或 1 减相似度变换。","steps_en":["Keep each full sample vector without mean subtraction.","Compute vector norms and coordinate-wise products.","Divide the product sum by the product of the two norms.","Subtract similarity from one."],"steps_zh":["保留完整采样向量，不去均值。","计算向量范数和逐坐标乘积。","以范数乘积归一化点积。","应用所示最终变换得到分数。"],"formula":"1 - dot(x,y)/(||x||_2 ||y||_2)","caveats_en":["Zero-norm vectors have no defined angle; this example excludes them.","Positive scaling is removed but an additive offset changes the direction.","The normalized angular form is a metric on unit directions; 1-cosine generally fails the triangle inequality."],"caveats_zh":["零范数向量的夹角未定义，本例不包含此情形。","正缩放被消除，但加性偏移会改变方向。","归一化角距离在单位方向上是度量；1−余弦一般不满足三角不等式。"],"sources":[{"label":"SciPy: cosine","url":"https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cosine.html"}],"x":[1.0,2.0,2.0,4.0,3.0,5.0,5.0,7.0,8.0,6.0,9.0,10.0],"y":[1.0,1.8,2.2,3.4,2.7,4.1,4.1,6.4,6.1,5.5,8.0,9.2],"input_label":"Deterministic synthetic illustration; sample index is the time axis.","parameters":{"centering":false,"ranking":false,"tie_rule":"not applicable"},"result":{"label":"distance","value":0.002441296340284005,"detail":""},"frames":[{"phase":0,"caption":"Compare paired observations on the same time grid","panel":{"kind":"scatter","title":"Paired observations: x versus y","a":[1.0,2.0,2.0],"b":[1.0,1.8,2.2],"marker":2}},{"phase":0,"caption":"Compare paired observations on the same time grid","panel":{"kind":"scatter","title":"Paired observations: x versus y","a":[1.0,2.0,2.0,4.0,3.0,5.0],"b":[1.0,1.8,2.2,3.4,2.7,4.1],"marker":5}},{"phase":0,"caption":"Compare paired observations on the same time grid","panel":{"kind":"scatter","title":"Paired observations: x versus y","a":[1.0,2.0,2.0,4.0,3.0,5.0,5.0,7.0,8.0],"b":[1.0,1.8,2.2,3.4,2.7,4.1,4.1,6.4,6.1],"marker":8}},{"phase":0,"caption":"Compare paired observations on the same time grid","panel":{"kind":"scatter","title":"Paired observations: x versus y","a":[1.0,2.0,2.0,4.0,3.0,5.0,5.0,7.0,8.0,6.0,9.0,10.0],"b":[1.0,1.8,2.2,3.4,2.7,4.1,4.1,6.4,6.1,5.5,8.0,9.2],"marker":11}},{"phase":1,"caption":"Keep vector directions; do not center","panel":{"kind":"lines","title":"Uncentered vectors","a":[1.0,2.0,2.0],"a_label":"x","b":[1.0,1.8,2.2],"b_label":"y"}},{"phase":1,"caption":"Keep vector directions; do not center","panel":{"kind":"lines","title":"Uncentered vectors","a":[1.0,2.0,2.0,4.0,3.0,5.0],"a_label":"x","b":[1.0,1.8,2.2,3.4,2.7,4.1],"b_label":"y"}},{"phase":1,"caption":"Keep vector directions; do not center","panel":{"kind":"lines","title":"Uncentered vectors","a":[1.0,2.0,2.0,4.0,3.0,5.0,5.0,7.0,8.0],"a_label":"x","b":[1.0,1.8,2.2,3.4,2.7,4.1,4.1,6.4,6.1],"b_label":"y"}},{"phase":1,"caption":"Keep vector directions; do not center","panel":{"kind":"lines","title":"Uncentered vectors","a":[1.0,2.0,2.0,4.0,3.0,5.0,5.0,7.0,8.0,6.0,9.0,10.0],"a_label":"x","b":[1.0,1.8,2.2,3.4,2.7,4.1,4.1,6.4,6.1,5.5,8.0,9.2],"b_label":"y"}},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":0},"value":0.002753405199171173,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":1},"value":0.012665663916187396,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":3},"value":0.06222695750126851,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.0,0.0,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":4},"value":0.08452953961455502,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.0,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":6},"value":0.19741915278057312,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.0,0.0,0.0,0.0],"a_label":"contribution","marker":7},"value":0.32077170570344166,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.13436617371955323,0.09086237157264872,0.0,0.0],"a_label":"contribution","marker":9},"value":0.5460002509956436,"value_label":"partial normalized dot product"},{"phase":2,"caption":"Accumulate normalized products using fixed full-vector norms","panel":{"kind":"bars","title":"Contribution to normalized dot product","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.13436617371955323,0.09086237157264872,0.19824517434032446,0.25331327832374795],"a_label":"contribution","marker":11},"value":0.997558703659716,"value_label":"partial normalized dot product"},{"phase":3,"caption":"Subtract similarity from one","panel":{"kind":"bars","title":"All normalized coordinate products","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.13436617371955323,0.09086237157264872,0.19824517434032446,0.25331327832374795],"a_label":"contribution","marker":0},"value":0.002441296340284005,"value_label":"final result"},{"phase":3,"caption":"Subtract similarity from one","panel":{"kind":"bars","title":"All normalized coordinate products","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.13436617371955323,0.09086237157264872,0.19824517434032446,0.25331327832374795],"a_label":"contribution","marker":3},"value":0.002441296340284005,"value_label":"final result"},{"phase":3,"caption":"Subtract similarity from one","panel":{"kind":"bars","title":"All normalized coordinate products","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.13436617371955323,0.09086237157264872,0.19824517434032446,0.25331327832374795],"a_label":"contribution","marker":7},"value":0.002441296340284005,"value_label":"final result"},{"phase":3,"caption":"Subtract similarity from one","panel":{"kind":"bars","title":"All normalized coordinate products","a":[0.002753405199171173,0.009912258717016223,0.012114982876353162,0.037446310708727955,0.022302582113286507,0.05644480658300905,0.05644480658300905,0.12335255292286856,0.13436617371955323,0.09086237157264872,0.19824517434032446,0.25331327832374795],"a_label":"contribution","marker":11},"value":0.002441296340284005,"value_label":"final result"}],"animation_steps_en":["Keep the uncentered sample vectors.","Compute coordinate products and both vector norms.","Divide the dot product by the norm product.","Return one minus cosine."],"echotime_status":"Low-priority addition","echotime_api":""}
