public double distanceFrom(RecordNeighbor other) { double distance, fixedParamsDistance, listsDistance; fixedParamsDistance = calculateFixedParametersDistanceFrom(other); listsDistance = calculatRecognitionsDistanceFrom(other); distance = Math.Sqrt(Math.Pow(fixedParamsDistance, 2) + Math.Pow(listsDistance, 2)); return(distance); }
public double calculateFixedParametersDistanceFrom(RecordNeighbor other) { double distance; double[] myFixedParameters = PrepareFixedParameters(); double[] otherFixedParameters = other.PrepareFixedParameters(); int paramsCount = myFixedParameters.Length; double[] distances = new double[paramsCount]; for (int i = 0; i < paramsCount; i++) { distances[i] = Math.Pow((myFixedParameters[i] - otherFixedParameters[i]), 2); } distance = Math.Sqrt(distances.Sum()); return(distance); }
public double calculatRecognitionsDistanceFrom(RecordNeighbor other) { int myRecognitionsCount = Recognitions.Count; int otherRecognitionsCount = other.Recognitions.Count; List <double> longerRecognitionsList; List <double> shorterRecognitionsList; List <double> longerIntervalsList; List <double> shorterIntervalsList; int smallerCount, largerCount; //find the shorter lists and the longer lists if (myRecognitionsCount >= otherRecognitionsCount) { largerCount = myRecognitionsCount; smallerCount = otherRecognitionsCount; longerRecognitionsList = Recognitions; longerIntervalsList = Intervals; shorterRecognitionsList = other.Recognitions; shorterIntervalsList = other.Intervals; } else { largerCount = otherRecognitionsCount; smallerCount = myRecognitionsCount; longerRecognitionsList = other.Recognitions; longerIntervalsList = other.Intervals; shorterRecognitionsList = Recognitions; shorterIntervalsList = Intervals; } //trim the first part of the longer lists by skipping the first elements in each list //the amount of skipped-on elements is the diffrence between the longer and the shorter lists List <double> trimmedRecognitions = new List <double>(longerRecognitionsList.Skip(largerCount - smallerCount)); List <double> trimmedIntervals = new List <double>(longerIntervalsList.Skip(largerCount - smallerCount)); //compare the last recognitions and last intervals between the current object and the other object double recognitionsBasedDistance = compareDistanceBetweenEvenSizedLists(trimmedRecognitions, shorterRecognitionsList, true); double intervalsBasedDistance = compareDistanceBetweenEvenSizedLists(trimmedIntervals, shorterIntervalsList, false); double distance = Math.Sqrt(Math.Pow(recognitionsBasedDistance, 2) + Math.Pow(intervalsBasedDistance, 2)); return(distance); }
public string TestAndClassify(RecordNeighbor toTest, int k) { Dictionary <string, double> classifications = new Dictionary <string, double>(); NeighborsComparer comparer = new NeighborsComparer(toTest); // Sorting the neighbors set by distance from tested object. mNeighbors.Sort(comparer); // Keeping the classifications of the k closest neighbors // (first k objects in the sorted list) in a dictionary, // counting the repeatitions of each one of them. for (int i = 0; i < k; i++) { if (classifications.ContainsKey(mNeighbors[i].Classification)) { classifications[mNeighbors[i].Classification]++; } else { classifications.Add(mNeighbors[i].Classification, 1 / mNeighbors[i].distanceFrom(toTest)); //test!!!!!!!!!!!!!!!!!! } } // Returns the classification of the most common of them // pay attention to cases of a few different classifications // with equal amount of repeatitons. List <KeyValuePair <string, double> > maxpairs = new List <KeyValuePair <string, double> >(); double max = classifications.Values.Max(); foreach (KeyValuePair <String, double> pair in classifications) { if (pair.Value == max) { maxpairs.Add(pair); } } return(maxpairs[0].Key); }