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/ ...
随机推荐
- 如何快速分析一款ios软件或需求的大流程,然后在业务层实现,不牵扯到界面?
如何快速分析一款ios软件或需求的大流程,然后在业务层实现,不牵扯到界面?
- C#编程(二十三)----------实现继承
原文链接:http://blog.csdn.net/shanyongxu/article/details/46593809 如果要声明派生自另一个类的一个类,可以使用下面的语法: class Deri ...
- ubuntu下安装android sdk运行模拟器出现错误:
./emulator: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No ...
- DotNetty的通道处理细节
第一,客户端如何向服务器主动发送消息: 第二,服务器如何向指定客户端发送消息: 第三,在哪里做报文的拆包和组包. public partial class FrmMain : Form { publi ...
- 技术人生:Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing how to use it.
Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing ho ...
- android 工具类 DateUtil
提取了一些在开发过程中可能会用到的日期相关的函数作为工具类.供大家參考: /** * 日期操作工具类. * * @author shimiso */ public class DateUtil { p ...
- Java 如何实现在线预览文档及修改(Office文件)
测试地址: https://sms.idxkj.cn 用户名:aa 密码:123456
- Swift - 绘制背景线条
Swift - 绘制背景线条 效果 源码 // // BackgroundLineView.swift // LineBackgroundView // // Created by YouXianMi ...
- 获得手机当前的ip地址
package com.kale.floating.net; import java.net.Inet4Address; import java.net.InetAddress; import jav ...
- FreeMarker:Java模板引擎
本资源由 伯乐在线 - 黄余粮 整理 FreeMarker 是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写.FreeMarker被设计用来生成HTML Web页面,特别是基于M ...