2024-04-11 16:02:49 +00:00
|
|
|
// ***************************************************************
|
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
|
|
|
// SPDX-FileType: SOURCE
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// ***************************************************************
|
|
|
|
|
2023-07-11 20:23:49 +00:00
|
|
|
#ifndef BAYESNET_METRICS_H
|
|
|
|
#define BAYESNET_METRICS_H
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2024-03-08 21:20:54 +00:00
|
|
|
#include <torch/torch.h>
|
2023-07-11 20:23:49 +00:00
|
|
|
namespace bayesnet {
|
|
|
|
class Metrics {
|
2024-04-07 22:13:59 +00:00
|
|
|
public:
|
|
|
|
Metrics() = default;
|
|
|
|
Metrics(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int classNumStates);
|
|
|
|
Metrics(const std::vector<std::vector<int>>& vsamples, const std::vector<int>& labels, const std::vector<std::string>& features, const std::string& className, const int classNumStates);
|
|
|
|
std::vector<int> SelectKBestWeighted(const torch::Tensor& weights, bool ascending = false, unsigned k = 0);
|
|
|
|
std::vector<double> getScoresKBest() const;
|
|
|
|
double mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
|
|
|
std::vector<float> conditionalEdgeWeights(std::vector<float>& weights); // To use in Python
|
|
|
|
torch::Tensor conditionalEdge(const torch::Tensor& weights);
|
|
|
|
std::vector<std::pair<int, int>> maximumSpanningTree(const std::vector<std::string>& features, const torch::Tensor& weights, const int root);
|
2023-10-11 19:17:26 +00:00
|
|
|
protected:
|
2023-11-08 17:45:35 +00:00
|
|
|
torch::Tensor samples; // n+1xm torch::Tensor used to fit the model where samples[-1] is the y std::vector
|
|
|
|
std::string className;
|
|
|
|
double entropy(const torch::Tensor& feature, const torch::Tensor& weights);
|
|
|
|
std::vector<std::string> features;
|
2023-10-11 19:17:26 +00:00
|
|
|
template <class T>
|
2023-11-08 17:45:35 +00:00
|
|
|
std::vector<std::pair<T, T>> doCombinations(const std::vector<T>& source)
|
2023-10-13 10:29:25 +00:00
|
|
|
{
|
2023-11-08 17:45:35 +00:00
|
|
|
std::vector<std::pair<T, T>> result;
|
2023-10-13 10:29:25 +00:00
|
|
|
for (int i = 0; i < source.size(); ++i) {
|
|
|
|
T temp = source[i];
|
|
|
|
for (int j = i + 1; j < source.size(); ++j) {
|
|
|
|
result.push_back({ temp, source[j] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2023-10-14 11:12:04 +00:00
|
|
|
template <class T>
|
2023-11-08 17:45:35 +00:00
|
|
|
T pop_first(std::vector<T>& v)
|
2023-10-14 11:12:04 +00:00
|
|
|
{
|
|
|
|
T temp = v[0];
|
|
|
|
v.erase(v.begin());
|
|
|
|
return temp;
|
|
|
|
}
|
2024-04-07 22:13:59 +00:00
|
|
|
private:
|
|
|
|
int classNumStates = 0;
|
|
|
|
std::vector<double> scoresKBest;
|
|
|
|
std::vector<int> featuresKBest; // sorted indices of the features
|
|
|
|
double conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
2023-07-11 20:23:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|