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]).
Given A = [1, 2, 3], B = [2, 3 ,4].
Return 0.9926.
Given A = [0], B = [0].
Return 2.0000
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)); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/65440.html
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...
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...
摘要:機器學習派是后起之秀,而相似度派則是泰山北斗,以致撐起來推薦系統的半壁江山。純來做推薦基本不靠譜,所以我們來試一下基于和相似度來實現一個推薦系統。 對于內容類網站或者APP,搜索和推薦已經是標配。搜索相對容易,使用Elasticsearch簡單配置一下就可以做出一個性能還不錯效果也還可以的搜索引擎,然而,推薦系統的話,沒有專門的團隊實踐起來還挺困難的。 網上推薦系統相關的理論非常多,但...
閱讀 1559·2021-11-17 09:33
閱讀 1111·2021-11-12 10:36
閱讀 2422·2019-08-30 15:54
閱讀 2446·2019-08-30 13:14
閱讀 2920·2019-08-26 14:05
閱讀 3296·2019-08-26 11:32
閱讀 3011·2019-08-26 10:09
閱讀 3005·2019-08-26 10:09