IFD:EAI SoS21/course material/Session 4: Programming the Classifier Part1: Difference between revisions

From Medien Wiki
Line 67: Line 67:
For that it traverses the vector of points a couple of times. Once for every class we have. In each of these traverses it only looks for points of the same class, sums them up, keeps a record of how many they were, and finally divides by the number of points it found. So we end up with the centroid for every class and save it in a vector of centroids called "_centroids". This is a private variable of our class KMeans.  
For that it traverses the vector of points a couple of times. Once for every class we have. In each of these traverses it only looks for points of the same class, sums them up, keeps a record of how many they were, and finally divides by the number of points it found. So we end up with the centroid for every class and save it in a vector of centroids called "_centroids". This is a private variable of our class KMeans.  


Now, when we classify, we know the centroids and just need to calculate the distance from our new point to every centroid.  
Now, when we classify, we know the centroids and just need to calculate the distance from our new point to every centroid.
<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
int KMeans::classify(Point2D newPoint)
int KMeans::classify(Point2D newPoint)
Line 85: Line 85:
}
}
</syntaxhighlight>
</syntaxhighlight>
The idea here is to start with a very large distance and only save a centroid as a candidate if its distance is smaller than the distance we already have recorded. We will automatically arrive at the closest centroid and the minimum distance.


=Homework=
=Homework=