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

From Medien Wiki
Line 39: Line 39:
</syntaxhighlight>
</syntaxhighlight>
Our classifier is written in a class called "KMeans" because it most closely resembles a KMeans classifier.  
Our classifier is written in a class called "KMeans" because it most closely resembles a KMeans classifier.  
So inside the "KMeans.h" and "KMeans.cpp" you will find the guts of our classifier.
So inside the "KMeans.cpp" you will find the guts of our classifier. You can see that, when the KMeans classifier gets instantiated (=the contructor is called), we feed it with the points and the class labels


<syntaxhighlight lang="c++">
<syntaxhighlight lang="c++">
KMeans::KMeans(vector<Point2D> &pVec, vector<int> &cVec, int n_classes )
KMeans::KMeans(vector<Point2D> &points, vector<int> &labels, int n_classes )
{
{
     _n_classes = n_classes;
     _n_classes = n_classes;
Line 50: Line 50:
         int numPoints = 0;
         int numPoints = 0;
          
          
         for(int i=0; i<pVec.size(); i++)
         for(int i=0; i<points.size(); i++)
         {
         {
             if(cVec[i]==c) // see if point in pVec belongs to class c
             if(labels[i]==c) // see if point in points belongs to class c
             {
             {
                 _centroids[c] = _centroids[c] + pVec[i];
                 _centroids[c] = _centroids[c] + points[i];
                 numPoints = numPoints + 1;
                 numPoints = numPoints + 1;
             }
             }
         }
         }
         // all points in pVec, that belong to the class c are added up
         // all points in "points", that belong to the class c are added up
         // now, divide by num of points in class c
         // now, divide by num of points in class c
         _centroids[c] = _centroids[c]/numPoints;
         _centroids[c] = _centroids[c]/numPoints;