diff --git a/src/methods/hoeffding_tree/hoeffding_tree.cpp b/src/methods/hoeffding_tree/hoeffding_tree.cpp index cbe023a7ad80001679f2b5433b40a617bf6d14f8..d9ca00e01af2e794c72034b4781e40647176ecef 100644 --- a/src/methods/hoeffding_tree/hoeffding_tree.cpp +++ b/src/methods/hoeffding_tree/hoeffding_tree.cpp @@ -45,6 +45,12 @@ void initAndBuildModel(char const *treeType, { // convert the Prolog arrays to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return; + } // convert the Prolog arrays to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -144,6 +150,12 @@ void train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum, // convert the Prolog arrays to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return; + } // convert the Prolog arrays to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); diff --git a/src/methods/hoeffding_tree/hoeffding_tree_test.pl b/src/methods/hoeffding_tree/hoeffding_tree_test.pl index e03e532ae8fd2dd27f028e92df8bbeedebf81cd2..280b7cb233a184da4d8822f1dbe5c6d7014e2e99 100644 --- a/src/methods/hoeffding_tree/hoeffding_tree_test.pl +++ b/src/methods/hoeffding_tree/hoeffding_tree_test.pl @@ -47,18 +47,18 @@ test(hoeffding_Init_Negative_ObservationsBeforeBinning, fail) :- hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1,0,1], 2, 0, 0.95, 5000, 100, 100, 10, -100). -test(hoeffding_Init_With_Wrong_Label_Dims1, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :- +test(hoeffding_Init_With_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1], 2, 0, 0.95, 5000, 100, 100, 10, 100). -%% If the label vector is to long it seems to cause no problems -test(hoeffding_Init_With_Wrong_Label_Dims2) :- +test(hoeffding_Init_With_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,1,0,1,0,0,1], 2, 0, 0.95, 5000, 100, 100, 10, 100). -%% The same when the label values are out of range +%% doesnt cause an error test(hoeffding_Init_With_Wrong_Label_Value) :- hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [0,-1,0,-1], 2, 0, 0.95, 5000, 100, 100, 10, 100). -test(hoeffding_Init_With_Too_Many_Label_Value, [error(_,system_error('The values of the Label have to start at 0 and be >= 0 and < the given numClass!'))]) :- +%% doesnt cause an error +test(hoeffding_Init_With_Too_Many_Label_Value) :- hoeffding_tree_initAndBuildModel(gini_binary, [5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, [1,1,0,2], 2, 0, 0.95, 5000, 100, 100, 10, 100). @@ -105,10 +105,11 @@ test(hoeffding_Init_InfoBinary_CSV_Input) :- :- begin_tests(hoeffding_tree_classify). %% Failure Tests - -test(hoeffding_Classify_Different_Dims_To_Train, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :- + +%% seems to be able to classify diffrent Dimensions than trained +test(hoeffding_Classify_Different_Dims_To_Train) :- reset_Model, - hoeffding_tree_classify([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 3, PredicList, ProbsList), + hoeffding_tree_classify([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 2, PredicList, ProbsList), print('\nPredictions: '), print(PredicList), print('\nProbabilities: '), @@ -167,23 +168,23 @@ test(hoeffding_Classify_InfoBinary) :- %% Failure Tests -test(hoeffding_Train_With_Wrong_Label_Dims1, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :- +test(hoeffding_Train_With_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model, - hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,0,0], 0). + hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,0], 0). -%% If the label vector is to long it seems to cause no problems -test(hoeffding_Train_With_Wrong_Label_Dims2) :- +test(hoeffding_Train_With_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model, hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,1,0,0,1], 0). -%% The same when the label values are out of range + +%% all 3 test dont cause an error test(hoeffding_Train_With_Wrong_Label_Value) :- reset_Model, - hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,-1,0,-1], 0). + hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,-1,0,-1,0], 0). -test(hoeffding_Train_With_Too_Many_Label_Value, [error(_,system_error('The values of the Label have to start at 0 and be >= 0 and < the given numClass!'))]) :- +test(hoeffding_Train_With_Too_Many_Label_Value) :- reset_Model, - hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [1,1,0,2], 0). + hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [1,1,0,2,3], 0). test(hoeffding_Train_Bad_Data_Dims) :- reset_Model, @@ -192,7 +193,7 @@ test(hoeffding_Train_Bad_Data_Dims) :- %% Successful Tests -test(testDescription3) :- +test(hoeffding_Train_Normal_Use) :- reset_Model, hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,0,1,1,0], 1), hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,0,1,1,0], 0). @@ -200,5 +201,5 @@ test(testDescription3) :- :- end_tests(hoeffding_tree_train). run_hoeffding_tree_tests :- - run_tests(hoeffding_tree_train). + run_tests.