C++

 class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: Cosine similarity.
*/
double cosineSimilarity(vector<int> A, vector<int> B) {
// write your code here
int AB = , size;
double lenA = , lenB = ;
size = A.size();
for (int i = ; i < size; i++) {
AB += A[i]*B[i];
lenA += A[i]*A[i];
lenB += B[i]*B[i];
}
if ( == AB && == lenA && == lenB) {
return ;
} else {
return AB/(sqrt(lenA)*sqrt(lenB));
} }
};

LintCode: Cosine Similarity的更多相关文章

  1. [LintCode] Cosine Similarity 余弦公式

    Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...

  2. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  3. cosine similarity

    Cosine similarity is a measure of similarity between two non zero vectors of an inner product space  ...

  4. 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)

    在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...

  5. 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)

    之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...

  6. Cosine Similarity of Two Vectors

    #include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...

  7. spark MLlib 概念 5: 余弦相似度(Cosine similarity)

    概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. 利用JAVA计算TFIDF和Cosine相似度-学习版本

    写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...

随机推荐

  1. AngularJS中Directive间交互实现合成

    假设需要烹饪一道菜肴,有3种原料,可以同时使用所有的3种原料,可以使用其中2种,也可以使用其中1种. 如果以Directive的写法,大致是:<bread material1 material2 ...

  2. Android 数据存储04之Content Provider

    Content Provider 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang 1 URI 通用资源标志符(Universal Resource Identif ...

  3. C#编程(二十一)----------扩展方法

    C#中的扩展方法 有许多扩展类的方式.如果有类的源代码,继承就是给类添加功能的好方法.但是如果没有源代码,怎么办?吃屎可以使用扩展方法,它允许改变一个类,但不需要该类的源代码.扩展方法是静态方法,它是 ...

  4. ORACLE的init.ora配置文件中参数详解

    db_name = "orcl"   一个数据库标识符,应与CREATE DATABASE 语句中指定的名称相对应. instance_name = orcl在多个例程使用相同服务 ...

  5. windows下PHP不能开启pgsql扩展的解决方法

    Tip: 环境 windows8.1 64位 + xampp1.8.1 + postgresql 9.3.6-2 第一步: php.ini中开启pgsql扩展  extension=php_pgsql ...

  6. RabbitMQ:基本命令

    rabbitmq的安装.启动和停止 rabbitmq-service.bat install rabbitmq-service.bat start rabbitmq-service.bat stop ...

  7. Excel VBA 从一个工作簿查找另一个一个工作簿中的一些内容复制到另外一个工作簿

    帮朋友来写个Excel VBA 以前写过ASP,所以对vb略微熟悉,但VBA 没有仔细研究过. 以前只研究过 vba 写一个 计算个人所得税的程序. 这次写的功能也算是简单,但也耗费了两天的功夫. 需 ...

  8. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

  9. Gridview利用DataFormatString属性设置数据格式

    首 先把Gridview的AutoGenerateColumns属性设为False(默认是False),DataField选择相应的字段,特别需要注 意的是要把需要设置的字段的HtmlEncode属性 ...

  10. Android -- Exif

    Exif Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了数码照片的信息,包括拍摄的光圈.快门.平衡白.ISO.焦距.日期时间等各种和拍摄 ...