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

9.4 KiB

<html lang="en"> <head> </head>
LCOV - code coverage report
Current view: top level - bayesnet/feature_selection - FCBF.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.3 % 26 24
Test Date: 2024-04-21 16:43:29 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 "bayesnet/utils/bayesnetUtils.h"
       8              : #include "FCBF.h"
       9              : namespace bayesnet {
      10              : 
      11            7 :     FCBF::FCBF(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            7 :         FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
      13              :     {
      14            7 :         if (threshold < 1e-7) {
      15            2 :             throw std::invalid_argument("Threshold cannot be less than 1e-7");
      16              :         }
      17            7 :     }
      18            5 :     void FCBF::fit()
      19              :     {
      20            5 :         initialize();
      21            5 :         computeSuLabels();
      22            5 :         auto featureOrder = argsort(suLabels); // sort descending order
      23            5 :         auto featureOrderCopy = featureOrder;
      24           42 :         for (const auto& feature : featureOrder) {
      25              :             // Don't self compare
      26           37 :             featureOrderCopy.erase(featureOrderCopy.begin());
      27           37 :             if (suLabels.at(feature) == 0.0) {
      28              :                 // The feature has been removed from the list
      29           16 :                 continue;
      30              :             }
      31           21 :             if (suLabels.at(feature) < threshold) {
      32            0 :                 break;
      33              :             }
      34              :             // Remove redundant features
      35          116 :             for (const auto& featureCopy : featureOrderCopy) {
      36           95 :                 double value = computeSuFeatures(feature, featureCopy);
      37           95 :                 if (value >= suLabels.at(featureCopy)) {
      38              :                     // Remove feature from list
      39           33 :                     suLabels[featureCopy] = 0.0;
      40              :                 }
      41              :             }
      42           21 :             selectedFeatures.push_back(feature);
      43           21 :             selectedScores.push_back(suLabels[feature]);
      44           21 :             if (selectedFeatures.size() == maxFeatures) {
      45            0 :                 break;
      46              :             }
      47              :         }
      48            5 :         fitted = true;
      49            5 :     }
      50              : }
        

Generated by: LCOV version 2.0-1

</html>