14 KiB
14 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 "FeatureSelect.h" 10 : namespace bayesnet { 11 250 : FeatureSelect::FeatureSelect(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights) : 12 250 : Metrics(samples, features, className, classNumStates), maxFeatures(maxFeatures == 0 ? samples.size(0) - 1 : maxFeatures), weights(weights) 13 : 14 : { 15 250 : } 16 184 : void FeatureSelect::initialize() 17 : { 18 184 : selectedFeatures.clear(); 19 184 : selectedScores.clear(); 20 184 : } 21 5151 : double FeatureSelect::symmetricalUncertainty(int a, int b) 22 : { 23 : /* 24 : Compute symmetrical uncertainty. Normalize* information gain (mutual 25 : information) with the entropies of the features in order to compensate 26 : the bias due to high cardinality features. *Range [0, 1] 27 : (https://www.sciencedirect.com/science/article/pii/S0020025519303603) 28 : */ 29 15453 : auto x = samples.index({ a, "..." }); 30 15453 : auto y = samples.index({ b, "..." }); 31 5151 : auto mu = mutualInformation(x, y, weights); 32 5151 : auto hx = entropy(x, weights); 33 5151 : auto hy = entropy(y, weights); 34 5151 : return 2.0 * mu / (hx + hy); 35 15453 : } 36 184 : void FeatureSelect::computeSuLabels() 37 : { 38 : // Compute Simmetrical Uncertainty between features and labels 39 : // https://en.wikipedia.org/wiki/Symmetric_uncertainty 40 1976 : for (int i = 0; i < features.size(); ++i) { 41 1792 : suLabels.push_back(symmetricalUncertainty(i, -1)); 42 : } 43 184 : } 44 11429 : double FeatureSelect::computeSuFeatures(const int firstFeature, const int secondFeature) 45 : { 46 : // Compute Simmetrical Uncertainty between features 47 : // https://en.wikipedia.org/wiki/Symmetric_uncertainty 48 : try { 49 11429 : return suFeatures.at({ firstFeature, secondFeature }); 50 : } 51 3359 : catch (const std::out_of_range& e) { 52 3359 : double result = symmetricalUncertainty(firstFeature, secondFeature); 53 3359 : suFeatures[{firstFeature, secondFeature}] = result; 54 3359 : return result; 55 3359 : } 56 : } 57 1851 : double FeatureSelect::computeMeritCFS() 58 : { 59 1851 : double rcf = 0; 60 8514 : for (auto feature : selectedFeatures) { 61 6663 : rcf += suLabels[feature]; 62 : } 63 1851 : double rff = 0; 64 1851 : int n = selectedFeatures.size(); 65 12209 : for (const auto& item : doCombinations(selectedFeatures)) { 66 10358 : rff += computeSuFeatures(item.first, item.second); 67 1851 : } 68 1851 : return rcf / sqrt(n + (n * n - n) * rff); 69 : } 70 184 : std::vector<int> FeatureSelect::getFeatures() const 71 : { 72 184 : if (!fitted) { 73 0 : throw std::runtime_error("FeatureSelect not fitted"); 74 : } 75 184 : return selectedFeatures; 76 : } 77 184 : std::vector<double> FeatureSelect::getScores() const 78 : { 79 184 : if (!fitted) { 80 0 : throw std::runtime_error("FeatureSelect not fitted"); 81 : } 82 184 : return selectedScores; 83 : } 84 : } |
![]() |
Generated by: LCOV version 2.0-1 |
</html>