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

From Medien Wiki

Solutions to Homework Task 2

Spoiler Alert! Again, accept the challenge and try on your own first! :)

Calculation of the Centroid

Description of Classifier Code from Session 4

Until now, we have been looking at points on a 2D plane. We know how to calculate the distance between them and how to find the centroid of a point cloud. Let's call these point cloud classes from now on. With this tool set at hand we can already code a simple classifier. We could e.g. detect whether any given point (P_new) is closer to the point cloud which we call class_0 or class_1. We can find out by calculating the distances of P_new to the centroids of all known classes as shown in the picture below.

2d classify.png

Homework

This week your task will be to modify the code from monday's session to work on n-dimensional data points. "N-dimensional" means the number of dimension can be chosen when we instantiate the clusterer. So the same code should work for 2 dimension (as we programmed it already), 3 dimension, 4 dimensions and so on...

Feel free to fork the repl.it and include a new class "PointND":

Clusterer-2D


The Class PointND should should be structured as shown in the following header file:

#ifndef PointN_H
#define PointN_H
#include <vector>
using namespace std;

class PointND {
    public:
        PointND(int dimensions); // constructor for zero point of n-dimensions

        PointND(vector<float> newND); // constructor for a point which copies the coordinates from an n-dimensional vector

        PointND operator+(PointND& p2);

        PointND operator/(float f);
       
        float getDim(int idx_dimension); // get the component of the indexed dimension, (this was getX() and getY() before)

        int size() // return how many dimension our current point has!
        {
            return _pointND.size();
        }
       
        void print();

        float getDistance(PointND&); // extend the euclidean distance to n dimensions

    private:
        vector<float> _pointND;
};
#endif

Your task is to:

  • Implement a PointND.cpp file that behaves like our class Point2D on n-dimensional points
  • Test that class with a 3D distance measurement, and on success
  • Modify our clusterer to work with the new PointND class!


Have fun and if you're stuck, write a mail or post a message in the forum!

Best wishes, Clemens