成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Cosine Similarity

Betta / 3005人閱讀

Problem

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:

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

Example

Given A = [1, 2, 3], B = [2, 3 ,4].
Return 0.9926.
Given A = [0], B = [0].
Return 2.0000

Solution
class Solution {
    public double cosineSimilarity(int[] A, int[] B) {
        // write your code here
        if (A.length == 0 || B.length == 0 || A.length != B.length) {
            return 2;
        }
        int aa = 0, bb = 0, ab = 0;
        for (int i = 0; i < A.length; i++) {
            aa += A[i]* A[i];
            bb += B[i]* B[i];
            ab += A[i]* B[i];
        }
        if (aa == 0 || bb == 0) return 2;
        return ab / (Math.sqrt(aa) * Math.sqrt(bb));
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65440.html

相關(guān)文章

  • Collaborative Filtering

    Memory basedget user-item matrix and calculate cosine similarity between $u_k, u_a$$$sim^{cos}(u_k,u_a)=frac{u_kcdot u_a}{||u_k|| ||u_a||}$$calculate in python, each row of train_data_matrix represent...

    oogh 評(píng)論0 收藏0
  • [LeetCode/LintCode] Sentence Similarity

    Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...

    dreamtecher 評(píng)論0 收藏0
  • 基于 Postgres 實(shí)現(xiàn)一個(gè)推薦系統(tǒng)

    摘要:機(jī)器學(xué)習(xí)派是后起之秀,而相似度派則是泰山北斗,以致?lián)纹饋?lái)推薦系統(tǒng)的半壁江山。純來(lái)做推薦基本不靠譜,所以我們來(lái)試一下基于和相似度來(lái)實(shí)現(xiàn)一個(gè)推薦系統(tǒng)。 對(duì)于內(nèi)容類網(wǎng)站或者APP,搜索和推薦已經(jīng)是標(biāo)配。搜索相對(duì)容易,使用Elasticsearch簡(jiǎn)單配置一下就可以做出一個(gè)性能還不錯(cuò)效果也還可以的搜索引擎,然而,推薦系統(tǒng)的話,沒(méi)有專門的團(tuán)隊(duì)實(shí)踐起來(lái)還挺困難的。 網(wǎng)上推薦系統(tǒng)相關(guān)的理論非常多,但...

    wean 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<