Exemplo n.º 1
0
        // testFace has been projected to the subspace
        static projectedTrainingMatrix[] findKNN(projectedTrainingMatrix[] trainingSet, double[][] testFace, int K, EuclideanDistance metric)
        {
            int NumOfTrainingSet = trainingSet.Length;

            // initialization
            projectedTrainingMatrix[] neighbors = new projectedTrainingMatrix[K];
            int i;

            for (i = 0; i < K; i++)
            {
                trainingSet[i].distance = metric.getDistance(trainingSet[i].matrix,
                                                             testFace);
//			System.out.println("index: " + i + " distance: "
//					+ trainingSet[i].distance);
                neighbors[i] = trainingSet[i];
            }

            // go through the remaining records in the trainingSet to find K nearest
            // neighbors
            for (i = K; i < NumOfTrainingSet; i++)
            {
                trainingSet[i].distance = metric.getDistance(trainingSet[i].matrix,
                                                             testFace);
//			System.out.println("index: " + i + " distance: "
//					+ trainingSet[i].distance);

                int maxIndex = 0;
                for (int j = 0; j < K; j++)
                {
                    if (neighbors[j].distance > neighbors[maxIndex].distance)
                    {
                        maxIndex = j;
                    }
                }

                if (neighbors[maxIndex].distance > trainingSet[i].distance)
                {
                    neighbors[maxIndex] = trainingSet[i];
                }
            }
            return(neighbors);
        }
Exemplo n.º 2
0
 public static String assignLabel(projectedTrainingMatrix[] trainingSet, double[][] testFace, int K, EuclideanDistance metric)
 {
     projectedTrainingMatrix[] neighbors = findKNN(trainingSet, testFace, K, metric);
     return(classify(neighbors));
 }