How to calculate cosine similarity between two frequency vectors in MATLAB?

与世无争的帅哥 提交于 2021-02-05 11:54:33

问题


I need to find the cosine similarity between two frequency vectors in MATLAB.

Example vectors:

a = [2,3,4,4,6,1]
b = [1,3,2,4,6,3]

How do I measure the cosine similarity between these vectors in MATLAB?


回答1:


Take a quick look at the mathematical definition of Cosine similarity.

From the definition, you just need the dot product of the vectors divided by the product of the Euclidean norms of those vectors.

% MATLAB 2018b
a = [2,3,4,4,6,1]; 
b = [1,3,2,4,6,3];

cosSim = sum(a.*b)/sqrt(sum(a.^2)*sum(b.^2));            % 0.9436

Alternatively, you could use

cosSim = (a(:).'*b(:))/sqrt(sum(a.^2)*sum(b.^2));        % 0.9436

which gives the same result.


After reading this correct answer, to avoid sending you to another castle I've added another approach using MATLAB's built-in linear algebra functions, dot() and norm().

cosSim = dot(a,b)/(norm(a)*norm(b));                     % 0.9436

See also the tag-wiki for cosine-similarity.


Performance by Approach:

  1. sum(a.*b)/sqrt(sum(a.^2)*sum(b.^2))
  2. (a(:).'*b(:))/sqrt(sum(a.^2)*sum(b.^2))
  3. dot(a,b)/(norm(a)*norm(b))

Each point represents the geometric mean of the computation times for 10 randomly generated vectors.




回答2:


If you have the Statistics toolbox, you can use the pdist2 function with the 'cosine' input flag, which gives 1 minus the cosine similarity:

a = [2,3,4,4,6,1];
b = [1,3,2,4,6,3];
result = 1-pdist2(a, b, 'cosine');


来源:https://stackoverflow.com/questions/57187941/how-to-calculate-cosine-similarity-between-two-frequency-vectors-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!