Files
BayesNet/html/BayesNet/bayesnet/feature_selection/IWSS.cc.gcov.html

10 KiB

<html lang="en"> <head> </head>
LCOV - code coverage report
Current view: top level - BayesNet/bayesnet/feature_selection - IWSS.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.3 % 30 28
Test Date: 2024-04-30 13:17:26 Functions: 100.0 % 2 2

            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 "IWSS.h"
      10              : namespace bayesnet {
      11           18 :     IWSS::IWSS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold) :
      12           18 :         FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
      13              :     {
      14           18 :         if (threshold < 0 || threshold > .5) {
      15            8 :             throw std::invalid_argument("Threshold has to be in [0, 0.5]");
      16              :         }
      17           18 :     }
      18           10 :     void IWSS::fit()
      19              :     {
      20           10 :         initialize();
      21           10 :         computeSuLabels();
      22           10 :         auto featureOrder = argsort(suLabels); // sort descending order
      23           10 :         auto featureOrderCopy = featureOrder;
      24              :         // Add first and second features to result
      25              :         //     First with its own score
      26           10 :         auto first_feature = pop_first(featureOrderCopy);
      27           10 :         selectedFeatures.push_back(first_feature);
      28           10 :         selectedScores.push_back(suLabels.at(first_feature));
      29              :         //     Second with the score of the candidates
      30           10 :         selectedFeatures.push_back(pop_first(featureOrderCopy));
      31           10 :         auto merit = computeMeritCFS();
      32           10 :         selectedScores.push_back(merit);
      33           34 :         for (const auto feature : featureOrderCopy) {
      34           34 :             selectedFeatures.push_back(feature);
      35              :             // Compute merit with selectedFeatures
      36           34 :             auto meritNew = computeMeritCFS();
      37           34 :             double delta = merit != 0.0 ? std::abs(merit - meritNew) / merit : 0.0;
      38           34 :             if (meritNew > merit || delta < threshold) {
      39           24 :                 if (meritNew > merit) {
      40            0 :                     merit = meritNew;
      41              :                 }
      42           24 :                 selectedScores.push_back(meritNew);
      43              :             } else {
      44           10 :                 selectedFeatures.pop_back();
      45           10 :                 break;
      46              :             }
      47           24 :             if (selectedFeatures.size() == maxFeatures) {
      48            0 :                 break;
      49              :             }
      50              :         }
      51           10 :         fitted = true;
      52           10 :     }
      53              : }
        

Generated by: LCOV version 2.0-1

</html>