网站建设模版文档,摄影网页设计模板,兰州网站维护,中国芯片制造最新消息余弦相似度#xff08;Cosine Similarity#xff09;是一种衡量两个非零向量之间角度的度量方式#xff0c;用于评估它们之间的相似性。它的值范围从 -1 到 1#xff0c;其中 1 表示完全相同的方向#xff08;即向量完全相同#xff09;#xff0c;0 表示正交#xff0…余弦相似度Cosine Similarity是一种衡量两个非零向量之间角度的度量方式用于评估它们之间的相似性。它的值范围从 -1 到 1其中 1 表示完全相同的方向即向量完全相同0 表示正交没有相似性而 -1 表示完全相反的方向。
假设我们有两个向量 A 和 B它们的余弦相似度可以通过以下公式计算
$ \text{similarity} \cos(\theta) \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| |\mathbf{B}|} $
其中
$ \mathbf{A} \cdot \mathbf{B} $ 是向量 A 和 B 的点积内积。$ |\mathbf{A}| 和 和 和 |\mathbf{B}| $ 分别是向量 A 和 B 的模长长度。
具体来说
点积内积$ \mathbf{A} \cdot \mathbf{B} \sum_{i1}^{n} A_i B_i $其中 (n) 是向量的维度。模长长度$ |\mathbf{A}| \sqrt{\sum_{i1}{n} A_i^2} $。
公式可以进一步展开为
$ \text{similarity} \frac{\sum\limits_{i1}^{n} A_i B_i}{\sqrt{\sum\limits_{i1}^{n} A_i^2} \sqrt{\sum\limits_{i1}^{n} B_i^2}} $
示例计算
假设我们有两个向量 A 和 B其中
$ \mathbf{A} [1, 2, 3] $$ \mathbf{B} [4, 5, 6] $
我们可以按照上述公式计算它们之间的余弦相似度
点积 $ \mathbf{A} \cdot \mathbf{B} 14 25 3*6 4 10 18 32 $模长 $ |\mathbf{A}| \sqrt{12 22 3^2} \sqrt{1 4 9} \sqrt{14} $$ |\mathbf{B}| \sqrt{42 52 6^2} \sqrt{16 25 36} \sqrt{77} $ 余弦相似度 $ \text{similarity} \frac{32}{\sqrt{14} \sqrt{77}} \frac{32}{\sqrt{1078}} $
我们可以使用 Python 来计算这个值
import numpy as np# 定义两个向量
vector_a np.array([1, 2, 3])
vector_b np.array([4, 5, 6])# 计算点积
dot_product np.dot(vector_a, vector_b)# 计算模长
norm_a np.linalg.norm(vector_a)
norm_b np.linalg.norm(vector_b)# 计算余弦相似度
cosine_similarity dot_product / (norm_a * norm_b)print(Cosine similarity:, cosine_similarity)
文章转载自: http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.pghgq.cn.gov.cn.pghgq.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.lkcqz.cn.gov.cn.lkcqz.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.bpmnx.cn.gov.cn.bpmnx.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.gmswp.cn.gov.cn.gmswp.cn http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn