13 KiB
13 KiB
<html lang="en">
<head>
</head>
</html>
LCOV - code coverage report | ||||||||||||||||||||||
![]() | ||||||||||||||||||||||
|
||||||||||||||||||||||
![]() |
Line data Source code 1 : // *************************************************************** 2 : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez 3 : // SPDX-FileType: SOURCE 4 : // SPDX-License-Identifier: MIT 5 : // *************************************************************** 6 : 7 : #include <limits> 8 : #include "bayesnet/utils/bayesnetUtils.h" 9 : #include "CFS.h" 10 : namespace bayesnet { 11 6 : void CFS::fit() 12 : { 13 6 : initialize(); 14 6 : computeSuLabels(); 15 6 : auto featureOrder = argsort(suLabels); // sort descending order 16 6 : auto continueCondition = true; 17 6 : auto feature = featureOrder[0]; 18 6 : selectedFeatures.push_back(feature); 19 6 : selectedScores.push_back(suLabels[feature]); 20 6 : featureOrder.erase(featureOrder.begin()); 21 34 : while (continueCondition) { 22 28 : double merit = std::numeric_limits<double>::lowest(); 23 28 : int bestFeature = -1; 24 164 : for (auto feature : featureOrder) { 25 136 : selectedFeatures.push_back(feature); 26 : // Compute merit with selectedFeatures 27 136 : auto meritNew = computeMeritCFS(); 28 136 : if (meritNew > merit) { 29 57 : merit = meritNew; 30 57 : bestFeature = feature; 31 : } 32 136 : selectedFeatures.pop_back(); 33 : } 34 28 : if (bestFeature == -1) { 35 : // meritNew has to be nan due to constant features 36 0 : break; 37 : } 38 28 : selectedFeatures.push_back(bestFeature); 39 28 : selectedScores.push_back(merit); 40 28 : featureOrder.erase(remove(featureOrder.begin(), featureOrder.end(), bestFeature), featureOrder.end()); 41 28 : continueCondition = computeContinueCondition(featureOrder); 42 : } 43 6 : fitted = true; 44 6 : } 45 28 : bool CFS::computeContinueCondition(const std::vector<int>& featureOrder) 46 : { 47 28 : if (selectedFeatures.size() == maxFeatures || featureOrder.size() == 0) { 48 1 : return false; 49 : } 50 27 : if (selectedScores.size() >= 5) { 51 : /* 52 : "To prevent the best first search from exploring the entire 53 : feature subset search space, a stopping criterion is imposed. 54 : The search will terminate if five consecutive fully expanded 55 : subsets show no improvement over the current best subset." 56 : as stated in Mark A.Hall Thesis 57 : */ 58 10 : double item_ant = std::numeric_limits<double>::lowest(); 59 10 : int num = 0; 60 10 : std::vector<double> lastFive(selectedScores.end() - 5, selectedScores.end()); 61 40 : for (auto item : lastFive) { 62 35 : if (item_ant == std::numeric_limits<double>::lowest()) { 63 10 : item_ant = item; 64 : } 65 35 : if (item > item_ant) { 66 5 : break; 67 : } else { 68 30 : num++; 69 30 : item_ant = item; 70 : } 71 : } 72 10 : if (num == 5) { 73 5 : return false; 74 : } 75 10 : } 76 22 : return true; 77 : } 78 : } |
![]() |
Generated by: LCOV version 2.0-1 |
</html>