余弦相似度
来源:互联网 发布:危险品物流软件有哪些 编辑:程序博客网 时间:2024/06/05 02:53
Cosine similarity is a measure of similarity between two vectors of an inner product space that measures the cosine of the angle between them. The cosine of 0° is 1, and it is less than 1 for any other angle.
See wiki: Cosine Similarity
Here is the formula:
cosine-similarity
Given two vectors A and B with the same size, calculate the cosine similarity.
Return 2.0000 if cosine similarity is invalid (for example A = [0] and B = [0]).
您在真实的面试中是否遇到过这个题? Yes
样例
给出 A = [1, 2, 3], B = [2, 3 ,4].
返回 0.9926.
给出 A = [0], B = [0].
See wiki: Cosine Similarity
Here is the formula:
cosine-similarity
Given two vectors A and B with the same size, calculate the cosine similarity.
Return 2.0000 if cosine similarity is invalid (for example A = [0] and B = [0]).
您在真实的面试中是否遇到过这个题? Yes
样例
给出 A = [1, 2, 3], B = [2, 3 ,4].
返回 0.9926.
给出 A = [0], B = [0].
返回 2.0000
import java.text.DecimalFormat;import java.util.Scanner;/** * Cosine similarity is a measure of similarity between two vectors of an inner product space that measures the cosine of the angle between them. The cosine of 0° is 1, and it is less than 1 for any other angle.See wiki: Cosine SimilarityHere is the formula:cosine-similarityGiven two vectors A and B with the same size, calculate the cosine similarity.Return 2.0000 if cosine similarity is invalid (for example A = [0] and B = [0]).您在真实的面试中是否遇到过这个题? Yes样例给出 A = [1, 2, 3], B = [2, 3 ,4].返回 0.9926.给出 A = [0], B = [0].返回 2.0000 * * @author Dell * */public class Test445 { public static double cosineSimilarity(int[]A, int[]B) { int sum=0; for(int i=0;i<A.length;i++) { sum=sum+A[i]*B[i]; } int sumA=0; int sumB=0; for(int i=0;i<A.length;i++) { sumA=sumA+A[i]*A[i]; } if(sumA==0) return 2.0000; for(int i=0;i<B.length;i++) { sumB=sumB+B[i]*B[i]; } if(sumB==0) return 2.0000; return sum/(Math.sqrt(sumA)*Math.sqrt(sumB)); }public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<a.length;i++) { a[i]=sc.nextInt(); } int[] b=new int[n]; for(int i=0;i<b.length;i++) { b[i]=sc.nextInt(); } double result=cosineSimilarity(a,b); DecimalFormat df=new DecimalFormat("0.0000"); System.out.println(df.format(result));}}
阅读全文
0 0
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- 余弦相似度
- Miscellaneous | 余弦相似度
- 余弦相似度-lintcode
- 余弦相似度
- Restrict关键字
- 浅析人脸检测之Haar分类器方法:Haar特征、积分图、 AdaBoost 、级联
- C#练习——继承与重载、索引器
- UVa 400 Unix ls 命令
- appium学习
- 余弦相似度
- spring-boot使用mongoDB
- 数据库第16周学习攻略(第十组)
- tc
- 人与世界,事情与人生,思考与能力,文章与说话,人生经验
- Jsoup获取全国地区数据(省市县镇村)
- HTML5新增的主体结构元素
- URL、URI和URN三者之间的关系
- SpringBoot初始教程之统一异常处理(三)