nok / sklearn-porter

Transpile trained scikit-learn estimators to C, Java, JavaScript and others.
BSD 3-Clause "New" or "Revised" License
1.28k stars 170 forks source link

decision tree C code exported by porter have zero integrity score with custom test_data. #70

Closed Marslanali closed 4 years ago

Marslanali commented 4 years ago

Hi,

Thanks for this great library. I have test decision tree C code by porter with Iris data sets, as given in the example and its giving me integrity score of 1. https://github.com/nok/sklearn-porter/tree/stable/examples/estimator/classifier/DecisionTreeClassifier/c

However, when I train the model with my own test data and check the integrity score, its give me zero. And also I run the c code by porter for my custom test data, and its make incorrect predictions.

Below is the my code snippet.

int predict(float features[3]) {

int classes[4];

if (features[1] <= 1738.4554443359375) {
    if (features[0] <= 88.28286743164062) {
        if (features[0] <= 84.56035614013672) {
            classes[0] = 0;
            classes[1] = 3556;
            classes[2] = 2;
            classes[3] = 0;
        } else {
            classes[0] = 0;
            classes[1] = 262;
            classes[2] = 56;
            classes[3] = 0;
        }
    } else {
        if (features[2] <= 1944.3283081054688) {
            classes[0] = 5;
            classes[1] = 1783;
            classes[2] = 817;
            classes[3] = 99;
        } else {
            classes[0] = 24;
            classes[1] = 287;
            classes[2] = 2300;
            classes[3] = 305;
        }
    }
} else {
    if (features[0] <= 672.5470275878906) {
        if (features[0] <= 216.8995819091797) {
            classes[0] = 0;
            classes[1] = 152;
            classes[2] = 281;
            classes[3] = 0;
        } else {
            classes[0] = 94;
            classes[1] = 198;
            classes[2] = 2;
            classes[3] = 1992;
        }
    } else {
        if (features[0] <= 4341.470458984375) {
            classes[0] = 1629;
            classes[1] = 148;
            classes[2] = 238;
            classes[3] = 315;
        } else {
            classes[0] = 551;
            classes[1] = 0;
            classes[2] = 415;
            classes[3] = 1198;
        }
    }
}

int index = 0;
for (int i = 0; i < 4; i++) {
    index = classes[i] > classes[index] ? i : index;
}
return index;

}

Any one please shed some light on this issue ?

Thanks alot

Marslanali commented 4 years ago

.