LintCode: Cosine Similarity
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的更多相关文章
- [LintCode] Cosine Similarity 余弦公式
Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- cosine similarity
Cosine similarity is a measure of similarity between two non zero vectors of an inner product space ...
- 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)
在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...
- 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)
之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...
- Cosine Similarity of Two Vectors
#include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...
- spark MLlib 概念 5: 余弦相似度(Cosine similarity)
概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 利用JAVA计算TFIDF和Cosine相似度-学习版本
写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...
随机推荐
- 在ASP.NET MVC中使用Knockout实践02,组合View Model成员、Select绑定、通过构造器创建View Model,扩展View Model方法
本篇体验使用ko.computed(fn)计算.组合View Model成员.Select元素的绑定.使用构造器创建View Model.通过View Model的原型(Prototype)为View ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- windows phone 基础
一.安装 建议安装Windows 7环境,XP中不能运行模拟器,Vista系统支持,但不解释.系统安装完后,直接去微软网站在线安装即可,非常方便,美中不足的是如果你的网速不快,那可能要折磨你半天,快得 ...
- 将 nginx 安装成 windows 的方法
服务器这几天不稳定,经常性的重启(硬件问题),而且是windows环境,在其上跑了nginx,每次重启后需要手动启动nginx方能是整个系统正常. 所以就查找了下一种方法,能否将nginx做成wind ...
- IOS开发常用宏定义
//release屏蔽NSLog//放在.pch文件里#ifdef DEBUG #else#define NSLog(...) {};#endif //G.C.D#define BACK(block) ...
- .NET:注意 Primitive 这个小坑
背景 有个需求,需要递归遍历类型的所有属性(属性的属性),然后对不同的类型做不同的处理,或者只是将类型分为三类:Primitive.Complex 和 Collection.因为 MS 的 Type ...
- java容器的总结
1.什么是容器? 在程序中,容器是一种用来容纳对象的数据结构,比如说list.set .map.queue. 2.为什么需要容器? 我们为什么需要容器呢?因为在程序中,我们会在任意时刻和任意位置创建任 ...
- SharePoint Online 创建网站集
前言 本文介绍如何在Office 365中创建SharePoint网站集. 正文 通过登录地址登录到Office 365环境中,我们可以在左上角的按钮中点开,进入管理员,也可以直接在页面中点击管理: ...
- 一分钟了解:String & StringBuilder & StringBuffer
这三个都是字符串对象,本篇就来分析下它们的使用途径,力求简单明了. 一.String String 长度是不可变的,如果你要改变string对象的字符或者是拼接字符的话,系统就会新建一个string, ...
- 大话+图说:Java字节码指令——只为让你懂
前言 随着Java开发技术不断被推到新的高度,对于Java程序员来讲越来越需要具备对更深入的基础性技术的理解,比如Java字节码指令.不然,可能很难深入理解一些时下的新框架.新技术,盲目一味追新也会越 ...