forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCosineSimilarity.cs
69 lines (62 loc) · 2.26 KB
/
CosineSimilarity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2010
// contacts@aforgenet.com
//
namespace AForge.Math.Metrics
{
using System;
/// <summary>
/// Cosine similarity metric.
/// </summary>
///
/// <remarks><para>This class represents the
/// <a href="http://en.wikipedia.org/wiki/Cosine_similarity">Cosine Similarity metric</a>.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // instantiate new similarity class
/// CosineSimilarity sim = new CosineSimilarity( );
/// // create two vectors for inputs
/// double[] p = new double[] { 2.5, 3.5, 3.0, 3.5, 2.5, 3.0 };
/// double[] q = new double[] { 3.0, 3.5, 1.5, 5.0, 3.5, 3.0 };
/// // get similarity between the two vectors
/// double similarityScore = sim.GetSimilarityScore( p, q );
/// </code>
/// </remarks>
///
public sealed class CosineSimilarity : ISimilarity
{
/// <summary>
/// Returns similarity score for two N-dimensional double vectors.
/// </summary>
///
/// <param name="p">1st point vector.</param>
/// <param name="q">2nd point vector.</param>
///
/// <returns>Returns Cosine similarity between two supplied vectors.</returns>
///
/// <exception cref="ArgumentException">Thrown if the two vectors are of different dimensions (if specified
/// array have different length).</exception>
///
public double GetSimilarityScore( double[] p, double[] q )
{
double denominator = 0, numerator = 0, pSumSq = 0, qSumSq = 0;
double pValue, qValue;
if ( p.Length != q.Length )
throw new ArgumentException( "Input vectors must be of the same dimension." );
for ( int x = 0, len = p.Length; x < len; x++ )
{
pValue = p[x];
qValue = q[x];
numerator += pValue * qValue;
pSumSq += pValue * pValue;
qSumSq += qValue * qValue;
}
denominator = Math.Sqrt( pSumSq ) * Math.Sqrt( qSumSq );
return ( denominator == 0 ) ? 0 : numerator / denominator;
}
}
}