Download

Struggling to build up a decision tree

I need to code a function that can automatically build up a decision tree. I think I have all the functions needed to help me with it. But since it the first time I need to build a tree, I’m stuck with the actual function.

I think I need a recursive method, but am not able to find or define one. I’ve been trying all day, starting on paper and never got something close. I did manage to print all the tables and give the result, but this function didn’t build a tree.

Below here is a link to all my project files. If someone could give feedback on how to make a function that can automatically build a decision tree or what I’m doing wrong before trying to build a decision tree (for example: an illogical node class), then I would be really thankful.

link to my code: https://drive.google.com/file/d/0B57BhxwS42z8QjFpMzlJRzdscms/view?usp=sharing

Node class (header):



 class Node {
public:
    //variables
    Node* parent;
    map<string, double> probs; //map met I-waarden (values) en kolomnamen (keys) van "huidige" tabel
    map<string, Node*> children;
    vector<vector<string> > t;
    string s;

    //functions
    Node(vector<vector<string> >);
    ~Node();
};


Tree::expandExternal



void Tree::expandExternal(const Position& p, vector<vector<string> >& tabel) {
    Node* a;
    a = p.v;
    map<string, vector<vector<string>>> m = split(tabel);
    string s; //s is bv. "ja" of "nee" of "hoog" of "laag"
    map<string, Node*> nodeMap;

    for (auto const &ent : m) {
        // ent.first is de key, ent.second is de value
        vector<vector<string>> tempTabel = ent.second;
        Node* n = new Node(tempTabel);
        n->parent = a;
        n->s = ent.first;
        n->t = ent.second;
        nodeMap[ent.first] = n;
        print(tempTabel);
    }
    a->children = nodeMap;
}


DTree::learn



void DTree::learn(vector<vector<string>>& table) {
    unsigned int numberOfRows, numberOfColumns;
    updateSize(table, numberOfRows, numberOfColumns);

    addRoot(table);

    //function recursive?

}


DTree::recursive



void DTree::recursive(Node* p) {
    //vector<vector<string>> tempTable;
    //tempTable = p->t;
    Node* a;

    for (auto const &ent : p->children) {
        while (!checkLastColumn(p->t)) {
            expandExternal(p, p->t);
            recursive(p->children->ent.second);
        }
    }
}


PS: please don’t mind the comments in my native language (Dutch).

Some background info about the train.dat file and the project: The program starts with the train.dat file and always splits the table based on the lowest Gini impurity (lowestI in code). It does this until the last column (“studeert”, which is in Englisch “studies”) only contains the same words (“ja” or “nee” which are respectively translated to “yes” and “no”).