721
edits
Line 38: | Line 38: | ||
KMeans classifier = KMeans(points, classLabels, 2); | KMeans classifier = KMeans(points, classLabels, 2); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Our | 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. | |||
<syntaxhighlight lang="c++"> | |||
KMeans::KMeans(vector<Point2D> &pVec, vector<int> &cVec, int n_classes ) | |||
{ | |||
_n_classes = n_classes; | |||
for(int c=0; c < n_classes; c++) | |||
{ | |||
_centroids.push_back(Point2D(0,0)); | |||
int numPoints = 0; | |||
for(int i=0; i<pVec.size(); i++) | |||
{ | |||
if(cVec[i]==c) // see if point in pVec belongs to class c | |||
{ | |||
_centroids[c] = _centroids[c] + pVec[i]; | |||
numPoints = numPoints + 1; | |||
} | |||
} | |||
// all points in pVec, that belong to the class c are added up | |||
// now, divide by num of points in class c | |||
_centroids[c] = _centroids[c]/numPoints; | |||
} | |||
} | |||
</syntaxhighlight> | |||
=Homework= | =Homework= |