LintCode C++代码 余弦相似度;

本文介绍了余弦相似度,它是衡量两个向量在内积空间中夹角的余弦,用于评估它们之间的相似性。当角度为0°时,余弦相似度为1,其他情况小于1。公式为:cos(θ) = A·B / (||A|| ||B||),其中A·B是向量A和B的点积,||A||和||B||分别是它们的模。如果向量A和B都为0,则返回2表示无效的相似度。解决思路包括计算内积,判断是否为0,再计算相似度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

评测
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]).

思路: 先计算各个向量的内积;
为0 返回2;
计算两个向量的乘积;
返回相似度;

class Solution {
public:
    /**
     * @param A: An integer array.
     * @param B: An integer array.
     * @return: Cosine similarity.
     */
    double cosineSimilarity(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值