Can anyone have a look see what might be wrong with my code?

My goal is to do a program to predict credit rating using a decision tree.
My program should compile as the following:
1
2
$ make creditrating
$ ./creditrating.bin [train] [test] [minleaf]


My code can only runs with train01. When I type train02/03... it doesn't work or shows core dump. Also the output would be the same no matter what number I set for minleaf:

Could anyone help me, please?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <iterator>

using namespace std;


vector<creditrating> readData(string filename, int flag);
Node *DTL(vector<creditrating>train, int minleaf);
Node *choosesplit(vector<creditrating>train);
void sort(vector<creditrating>train, int attr);
double get_gain(vector<creditrating>trains, double splitValue, int attr);
string predict(Node *n, creditrating data);


//main
int main (int argc, const char * argv[]){
  
    if (argc != 4) {
        cerr << "Usage: creditrating TRAIN TEST MINLEAF\n";
        return 1;
    }
    
    string train_num = argv[1];
    string text_num = argv[2];
    int minleaf = stoi(argv[3]);
 
    vector<creditrating>train = readData(train_num, 0);
    vector<creditrating>test = readData(text_num, 1);
    

    Node *n = DTL(train,20);
    for (int i = 0; i < test.size(); i++){
        string label = predict(n, test[i]);
        cout<<label<<endl;
    }

    return 0;
}


//read from txt
vector<creditrating> readData(string filename, int flag){
    vector<creditrating> datas;
    char buffer[256];
    ifstream in;
    in.open(filename);
    int line = 0;
    while (!in.eof()){
        in.getline (buffer,100);
        if(line > 0){
            string readline(buffer, buffer + strlen(buffer));
            istringstream iss(readline);

            vector<string> results(istream_iterator<string>{iss}, istream_iterator<string>());
            int isEnter = 0;
            for (int i = 0; i < results.size(); i++, isEnter++){
                if(isEnter == 0){
                    creditrating trainOrTestData;
                    trainOrTestData.WC_TA = stod(results[0]);
                    trainOrTestData.RE_TA = stod(results[1]);
                    trainOrTestData.EBIT_TA = stod(results[2]);
                    trainOrTestData.MVE_BVTD = stod(results[3]);
                    trainOrTestData.S_TA = stod(results[4]);
                    if(flag == 0){
                        trainOrTestData.Rating = results[5];
                    }
                    datas.push_back(trainOrTestData);
                }
            }
        }
        line++;
    }
    in.close();
    return datas;
}


//2 DTL Algorithm
Node *DTL(vector<creditrating> train, int minleaf){
    if(train.size() <= minleaf){
        Node *node = new Node(); //create new leaf node n
        //calculate the number of each of the 8 different level of rating AAA,BB...
        int number[8];
        for (int i = 0; i < 8; i++){
            number[i] = 0;
        }
        //go through data
        for (creditrating d: train){
            if( !d.Rating.compare("AAA")){
                number[0] ++;
            }
            else if(!d.Rating.compare("AA")){
                number[1] ++;
            }
            else if(!d.Rating.compare("A")){
                number[2] ++;
            }
            else if(!d.Rating.compare("BBB")){
                number[3] ++;
            }
            else if(!d.Rating.compare("BB")){
                number[4] ++;
            }
            else if(!d.Rating.compare("B")){
                number[5] ++;
            }
            else if(!d.Rating.compare("CCC")){
                number[6] ++;
            }
            else{
                number[7] ++;
            }
        }
        int max = 0;
        int maxIndex = 0;
        for (int i = 0; i < 8; i++){
            if(number[i] > max){
                max = number[i];
                maxIndex = i;
            }
        }
        string label = "";
        switch (maxIndex){
            case 0: 
                label = "AAA";
                break;
            case 1: 
                label = "AA";
                break;
            case 2: 
                label = "A";
                break;      
            case 3: 
                label = "BBB";
                break;
            case 4: 
                label = "BB";
                break;
            case 5: 
                label = "B";
                break;
            case 6: 
                label = "CCC";
                break;
            case 7: 
                label = "unknown";
                break;
            default:
                break;
        }
        node->label = label;
        return node;
    }
    Node *n = choosesplit(train);

    //left&right
    vector<creditrating> left;
    vector<creditrating> right;
    double value;
    for(int i = 0; i< train.size(); i++){
        switch (n->attr){
            case 0:
                value = train[i].WC_TA;
                break;
            case 1:
                value = train[i].RE_TA;
                break;
            case 2:
                value = train[i].EBIT_TA;
                break;
            case 3:
                value = train[i].MVE_BVTD;
                break;
            case 4:
                value = train[i].S_TA;
                break;
            default:
                break;
        }
        if(value <= n->splitValue){
            left.push_back(train[i]);
        } else {
            right.push_back(train[i]);
        }
    }
    n->left = DTL(left, minleaf);
    n->right = DTL(right, minleaf);
    return n;
}


//3 Algorithm choosesplite -cut function
Node *choosesplit(vector<creditrating> train){
    double bestGain = 0;
    Node *node = new Node();
    for (int i = 0; i < 5; i++){
        //cout<<"111"<<endl;
        sort(train, i);
        //cout<<"222"<<endl;
        for (int j = 0; j < train.size(); j++){
            double splitval = 0;
            switch (i){
                case 0:
                    splitval = 0.5 * (train[j].WC_TA + train[j+1].WC_TA);
                    break;
                case 1:
                    splitval = 0.5 * (train[j].RE_TA + train[j+1].RE_TA);
                    break;
                case 2:
                    splitval = 0.5 * (train[j].EBIT_TA + train[j+1].EBIT_TA);
                    break;
                case 3:
                    splitval = 0.5 * (train[j].MVE_BVTD + train[j+1].MVE_BVTD);
                    break;
                case 4:
                    splitval = 0.5 * (train[j].S_TA + train[j+1].S_TA);
                    break;
                default:
                    break;
            }
            double gain = get_gain(train, splitval, i);  
            
            if (gain > bestGain){
                bestGain = gain;
                node->attr = i;
                node->splitValue = splitval;
            }
        }
    }
    // Node *node = new Node();
    return node;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//sorting
void sort(vector<creditrating> train, int attr){
    for (int i = 0; i < train.size(); i++){
        for (int j = 0; j < train.size() - i - 1; j++){
            creditrating d1 = train[j];
            creditrating d2 = train[j+1];
            double value = 0;
            switch(attr){
                case 0:
                    value = d1.WC_TA - d2.WC_TA;
                    break;
                case 1:
                    value = d1.RE_TA - d2.RE_TA;
                    break;
                case 2:
                    value = d1.EBIT_TA - d2.EBIT_TA;
                    break;
                case 3:
                    value = d1.MVE_BVTD - d2.MVE_BVTD;
                    break;
                case 4:
                    value = d1.S_TA - d2.S_TA;
                    break;
                default :
                    break;
            }

            if(value > 0){
                creditrating temp = d1;
                train[j] = d2;
                train[j + 1] = temp;
            }
        }
    }
}

double get_gain(vector<creditrating> trains, double splitValue, int attr){
    //calculate I of nodes-> I of left nodes and I of right nodes 
    //calculate root node: total, n_AAA, n_BBB.....
    //calculate left tree: total, n_AAA, n_BBB.....
    //calculate right tree: total, n_AAA, n_BBB.....
    double i_root = 0, i_left = 0, i_right = 0;
    int t_root = 0, t_left = 0, t_right = 0;
    int n_root[8];
    int n_left[8];
    int n_right[8];
    for (int i = 0; i < 8; i++){
        n_root[i] = 0;
        n_left[i] = 0;
        n_right[i] = 0;
    }
    for (creditrating data : trains){
        double value = 0;
        switch(attr){
            case 0:
                value = data.WC_TA;
                break;
            case 1:
                value = data.RE_TA;
                break;
            case 2:
                value = data.EBIT_TA;
                break;
            case 3:
                value = data.MVE_BVTD;
                break;
            case 4:
                value = data.S_TA;
                break;
            default:
                break;
        }

            
        if(!data.Rating.compare("AAA")){
            n_root[0]++;
            if(value <= splitValue){
                n_left[0]++;
            }else{
                n_right[0]++;
            }
        }
        else if (!data.Rating.compare("AA")){
            n_root[1]++;
            value <= splitValue ? n_left[1]++ : n_right[1]++;
        }
        else if (!data.Rating.compare("A")){
            n_root[2]++;
            value <= splitValue ? n_left[2]++ : n_right[2]++;
        }
        else if (!data.Rating.compare("BBB")){
            n_root[3]++;
            value <= splitValue ? n_left[3]++ : n_right[3]++;
        }
        else if (!data.Rating.compare("BB")){
            n_root[4]++;
            value <= splitValue ? n_left[4]++ : n_right[4]++;
        }
        else if (!data.Rating.compare("B")){
            n_root[5]++;
            value <= splitValue ? n_left[5]++ : n_right[5]++;
        }
        else if (!data.Rating.compare("CCC")){
            n_root[6]++;
            value <= splitValue ? n_left[6]++ : n_right[6]++;
        }
        else {
            n_root[7]++;
            value <= splitValue ? n_left[7]++ : n_right[7]++;
        }
    }
    for (int i = 0; i < 8; i++){
        t_root += n_root[i];
        t_left += n_left[i];
        t_right += n_right[i];
    }

    for (int i = 0; i < 8; i++){
        if(n_left[i] != 0 && t_left != 0){
            i_left += -((double)n_left[i]/t_left)*log((double)n_left[i]/t_left)/log(2);
        }
        if(n_right[i] != 0 && t_right != 0){
            i_right += -((double)n_right[i]/t_right)*log((double)n_right[i]/t_right)/log(2);
        }
        if(n_root[i] != 0 && t_root != 0){
            i_root += -((double)n_root[i]/t_root)*log((double)n_root[i]/t_root)/log(2);
        }
    } 
    double gain = i_root - ((double)t_left/t_root) * i_left - ((double)t_right/t_root) * i_right;

    return gain;
}

//4 Algorithm 3 - predict
string predict(Node *n, creditrating data){
    while(n->left != NULL && n->right != NULL){
        double value = 0;
        switch(n->attr){
            case 0:
                value = data.WC_TA;
                break;
            case 1:
                value = data.RE_TA;
                break;
            case 2:
                value = data.EBIT_TA;
                break;
            case 3:
                value = data.MVE_BVTD;
                break;
            case 4:
                value = data.S_TA;
                break;
            default:
                break;
        }
        if (value <= n->splitValue){
            n = n->left;
        }else{
            n = n->right;
        }
    }
    return n->label;
}
Did you try running the code in your debugger?

The debugger should be able to tell you exactly where it detects the problem.

Are you sure the input files are actually opening and actually contain valid data?

You are also passing a lot of parameters by value that should probably be passed by reference/const reference.

I can't compile the code because there appears to be "missing bits", for example what is a Node and where is it defined?

Yes. [test][train]actually refer to files (named train0x, test0x) that contain the customer data. for example:
test02:

    WC_TA        RE_TA      EBIT_TA     MVE_BVTD         S_TA
     0.02400      0.05000      0.03400      0.73500      0.27100
     0.38200      0.39000      0.07400      0.96900      0.24500
    -0.00900     -0.19300      0.03200      0.13200      0.09200
     0.16700      0.19000      0.05200      0.54800      0.12800
     0.03900      0.19500      0.03900      1.09300      0.28100
     0.33200      0.49200      0.07400      2.89600      0.46700
     0.06100      0.12600      0.04300      0.55000      0.26100

train02:
 
     WC_TA        RE_TA      EBIT_TA     MVE_BVTD         S_TA         Rating
     0.01300      0.10400      0.03600      0.44700      0.14200           BB
     0.23200      0.33500      0.06200      1.96900      0.28100            A
     0.31100      0.36700      0.07400      1.93500      0.36600            A
     0.19400      0.26300      0.06200      1.01700      0.22800          BBB
     0.12100      0.41300      0.05700      3.64700      0.46600          AAA
    -0.11700     -0.79900      0.01000      0.17900      0.08200          CCC
     0.08700      0.15800      0.04900      0.81600      0.32400          BBB


That's basically what the file contains. I think I might miss some implementation, which causes my program cannot run with the rest of train file.

This is the algorithm for my DLT.
Algorithm 1 DTL(data, minleaf)
Require: data in the form of N input-output pairs {xi, yi}Ni=1, minleaf ≥ 1.
1: if (N ≤ minleaf) or (yi = yj for all i, j) or (xi = xj for all i, j) then
2: Create new leaf node n.
3: if there is a unique mode (most frequent value) in {yi}Ni=1 then
4: n.label ← mode in {yi}Ni=1
5: else
6: n.label ← unknown
7: end if
8: return n.
9: end if
10: [attr,splitval] ← choose-split(data)
11: Create new node n.
12: n.attr ← attr
13: n.splitval ← splitval
14: n.lef t ← DTL(data with xi [attr] ≤ splitval, minleaf)
15: n.right ← DTL(data with xii [attr] > splitval, minleaf)
16: return n.


Last edited on
Topic archived. No new replies allowed.