Ejemplo n.º 1
0
        /// <summary>
        /// Returns distance between two N-dimensional double vectors.
        /// </summary>
        /// 
        /// <param name="p">1st point vector.</param>
        /// <param name="q">2nd point vector.</param>
        /// 
        /// <returns>Returns Cosine distance 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 GetDistance( double[] p, double[] q )
        {
            double distance;

            CosineSimilarity dist = new CosineSimilarity( );
            distance = (double) 1 - dist.GetSimilarityScore( p, q );

            return distance;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns distance between two N-dimensional double vectors.
        /// </summary>
        ///
        /// <param name="p">1st point vector.</param>
        /// <param name="q">2nd point vector.</param>
        ///
        /// <returns>Returns Cosine distance 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 GetDistance(double[] p, double[] q)
        {
            double distance;

            var dist = new CosineSimilarity();

            distance = 1 - dist.GetSimilarityScore(p, q);

            return(distance);
        }
Ejemplo n.º 3
0
        public void CosineSimilarityTest( )
        {
            CosineSimilarity sim = new CosineSimilarity( );

            Assert.Throws<ArgumentException>( ( ) => sim.GetSimilarityScore( p0, q4 ) );

            double result = sim.GetSimilarityScore( p0, q0 );
            Assert.AreApproximatelyEqual( result, .8, 0.00001 );

            result = sim.GetSimilarityScore( p1, q1 );
            Assert.AreApproximatelyEqual( result, 0.97014, 0.00001 );

            result = sim.GetSimilarityScore( p2, q2 );
            Assert.AreEqual( result, 0 );

            result = sim.GetSimilarityScore( p3, q3 );
            Assert.AreApproximatelyEqual( result, 1, 0.00001 );

            result = sim.GetSimilarityScore( p4, q4 );
            Assert.AreApproximatelyEqual( result, 0.96065, 0.00001 );

            result = sim.GetSimilarityScore( p5, q5 );
            Assert.AreApproximatelyEqual( result, 0.96897, 0.00001 );
        }