Esempio n. 1
0
        /// <summary>
        /// Finds the K nearest instances to the test instance.
        /// </summary>
        /// <param name="test">The test instance.</param>
        /// <returns>An array of the k-nearest results.</returns>
        /// <remarks></remarks>
        public Result[] FindKNearest(Instance test)
        {
            List<Result> results = new List<Result>();

            foreach (Instance train in TrainingInstances)
            {
                double dist = double.MaxValue;

                switch (Metric)
                {
                    case Metric.DistanceSquared:   dist = train.DistanceSquared(test);        break;
                    case Metric.EuclideanDistance: dist = train.EuclideanDistance(test);      break;
                    case Metric.ManhattanDistance: dist = train.ManhattanDistance(test);      break;
                    case Metric.ChebyshevDistance: dist = train.ChebyshevDistance(test);      break;
                    case Metric.CosineSimilarity:  dist = 1 - train.CosineSimilarity(test);   break;
                    case Metric.PearsonSimilarity: dist = 1 - train.PearsonSimilarity(test);  break;
                    case Metric.JaccardSimilarity: dist = 1 - train.JaccardSimilarity(test);  break;
                    default: throw new NotSupportedException();
                }

                results.Add(new Result(train.Class, dist));
            }

            int k = (int)Math.Min(K, TrainingInstances.Count);
            results.Sort();
            Result[] knr = new Result[k];
            results.CopyTo(0, knr, 0, k);

            return knr;
        }
Esempio n. 2
0
 /// <summary>
 /// Classifies the specified test instance.
 /// </summary>
 /// <param name="test">The test instance.</param>
 /// <returns>A classification result.</returns>
 /// <remarks></remarks>
 public abstract Result Classify(Instance test);
Esempio n. 3
0
 /// <summary>
 /// Calculates the Jaccard similarity of this instance with a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The Jaccard similarity.</returns>
 /// <remarks></remarks>
 public double JaccardSimilarity(Instance vector)
 {
     double m11 = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         m11 += (Features[i] == vector.Features[i]) ? 1 : 0;
     }
     return m11 / Features.Length;
 }
Esempio n. 4
0
 /// <summary>
 /// Classifies the specified test instance.
 /// </summary>
 /// <param name="test">The test instance.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 public override Result Classify(Instance test)
 {
     return FindMaxClass(FindKNearest(test));
 }
Esempio n. 5
0
 /// <summary>
 /// Calculates the Cosine similarity of this instance with a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The Cosine similarity.</returns>
 /// <remarks></remarks>
 public double CosineSimilarity(Instance vector)
 {
     return DotProduct(vector) / (Length() * vector.Length());
 }
Esempio n. 6
0
 /// <summary>
 /// Calculates the Pearson similarity of this instance with a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The Pearson similarity.</returns>
 /// <remarks></remarks>
 public double PearsonSimilarity(Instance vector)
 {
     double meanx = Mean(), meany = vector.Mean();
     double sdx = StandardDeviation(), sdy = vector.StandardDeviation();
     double sum = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         sum += ((Features[i] - meanx) / sdx) * ((vector.Features[i] - meany) / sdy);
     }
     return sum / Features.Length;
 }
Esempio n. 7
0
 /// <summary>
 /// Calculates the Chebyshev distance of this instance to a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The chebyshev distance.</returns>
 /// <remarks></remarks>
 public double ChebyshevDistance(Instance vector)
 {
     double max = double.NegativeInfinity, dist = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         dist = Math.Abs(vector.Features[i] - Features[i]);
         if (dist > max) max = dist;
     }
     return dist;
 }
Esempio n. 8
0
 /// <summary>
 /// Calculates the Minkowski distance of this instance to a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <param name="power">The Minkowski power.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 public double MinkowskiDistance(Instance vector, double power)
 {
     double sum = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         sum += Math.Pow(Math.Abs(vector.Features[i] - Features[i]), power);
     }
     return Math.Pow(sum, 1 / power);
 }
Esempio n. 9
0
 /// <summary>
 /// Calculates the Manhattan distance of this instance to a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The Manhattan distance.</returns>
 /// <remarks></remarks>
 public double ManhattanDistance(Instance vector)
 {
     double sum = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         sum += Math.Abs(vector.Features[i] - Features[i]);
     }
     return sum;
 }
Esempio n. 10
0
 /// <summary>
 /// Calculates the Euclidean distance of this instance to a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The distance.</returns>
 /// <remarks></remarks>
 public double EuclideanDistance(Instance vector)
 {
     return Math.Sqrt(DistanceSquared(vector));
 }
Esempio n. 11
0
 /// <summary>
 /// Calculates the squared Euclidean distance of this instance to a specified instance.
 /// </summary>
 /// <param name="vector">The instance.</param>
 /// <returns>The squared distance.</returns>
 /// <remarks></remarks>
 public double DistanceSquared(Instance vector)
 {
     double sum = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         sum += (vector.Features[i] - Features[i])
                 * (vector.Features[i] - Features[i]);
     }
     return sum;
 }
Esempio n. 12
0
 /// <summary>
 /// Performs the vector dot-product of this instance's features and a specified instance's.
 /// </summary>
 /// <param name="vector">The instance to dot with.</param>
 /// <returns>The dot product.</returns>
 /// <remarks></remarks>
 public double DotProduct(Instance vector)
 {
     double sum = 0;
     for (int i = 0; i < Features.Length; i += 1)
     {
         sum += vector.Features[i] * Features[i];
     }
     return sum;
 }
Esempio n. 13
0
        /// <summary>
        /// Classifies the specified test instance.
        /// </summary>
        /// <param name="test">The test instance.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public override Result Classify(Instance test)
        {
            string maxClass = "?";
            double maxProb = float.MinValue;

            double[] features = FeatureReduction.Discretize(test.Features, 10);

            foreach (KeyValuePair<string, double> pair in Classes)
            {
                double score = Math.Log(pair.Value / TotalExamples);

                for (int i = 0; i < features.Length; i++)
                {
                    score += Math.Log(Count(pair.Key, i, features[i]) / pair.Value);
                }

                if (score > maxProb)
                {
                    maxProb = score;
                    maxClass = pair.Key;
                }
            }

            return new Result(maxClass, maxProb);
        }