Ejemplo n.º 1
0
    static int Classify(double[] unknown,
      double[][] trainData, int numClasses, int k, int numFeatures)
    {
      int n = trainData.Length;
      IndexAndDistance[] info = new IndexAndDistance[n];
      for (int i = 0; i < n; ++i)
      {
        IndexAndDistance curr = new IndexAndDistance();
        double dist = Distance(unknown, trainData[i]);
        curr.idx = i;
        curr.dist = dist;
        info[i] = curr;
      }

      Array.Sort(info); // sort by distance
      Console.WriteLine("Nearest / Distance / Class");
      Console.WriteLine("==========================");
      for (int i = 0; i < k; ++i)
      {
        int c = (int) trainData[info[i].idx][numFeatures]; //to zmienilem
        string dist = info[i].dist.ToString("F3");
        Console.WriteLine("( " + trainData[info[i].idx][0] +
                          "," + trainData[info[i].idx][1] + " )  :  " +
                          dist + "        " + c);
      }
      int result = Vote(info, trainData, numClasses, k, numFeatures);
      return result;
    } // Classify
Ejemplo n.º 2
0
        public static string Classify(double[] unknown, double[][] trainData, int numClasses, int k)
        {
            int n = trainData.Length;

            IndexAndDistance[] info = new IndexAndDistance[n];
            for (int i = 0; i < n; i++)
            {
                IndexAndDistance curr = new IndexAndDistance();
                double           dist = Distance(unknown, trainData[i]);
                curr.idx  = i;
                curr.dist = dist;
                info[i]   = curr;
            }
            Array.Sort(info);
            for (int i = 0; i < k; ++i)
            {
                string c    = trainData[info[i].idx][trainData[0].Length - 1] == 1 ? "Iris-virginica" : (trainData[info[i].idx][trainData[0].Length - 2] == 1 ? "Iris - versicolor" : "Iris - setosa");
                double dist = info[i].dist;
                Console.WriteLine($"Nearest: ({trainData[info[i].idx][0]}, {trainData[info[i].idx][1]}, {trainData[info[i].idx][2]}, {trainData[info[i].idx][3]}) Distance: {dist} Class: {c}");
            }
            string result = Vote(info, trainData, numClasses, k);

            return(result);
        }