Complete selectKPairs method & test
This commit is contained in:
@@ -34,38 +34,40 @@ namespace bayesnet {
|
||||
{
|
||||
// Return the K Best features
|
||||
auto n = features.size();
|
||||
if (k == 0) {
|
||||
k = n;
|
||||
}
|
||||
// compute scores
|
||||
scoresKPairs.clear();
|
||||
pairsKBest.clear();
|
||||
auto label = samples.index({ -1, "..." });
|
||||
// for (int i = 0; i < n; ++i) {
|
||||
// for (int j = i + 1; j < n; ++j) {
|
||||
// scoresKBest.push_back(mutualInformation(samples.index({ i, "..." }), samples.index({ j, "..." }), weights));
|
||||
// featuresKBest.push_back(i);
|
||||
// featuresKBest.push_back(j);
|
||||
// }
|
||||
// }
|
||||
// // sort & reduce scores and features
|
||||
// if (ascending) {
|
||||
// sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
|
||||
// { return scoresKBest[i] < scoresKBest[j]; });
|
||||
// sort(scoresKBest.begin(), scoresKBest.end(), std::less<double>());
|
||||
// if (k < n) {
|
||||
// for (int i = 0; i < n - k; ++i) {
|
||||
// featuresKBest.erase(featuresKBest.begin());
|
||||
// scoresKBest.erase(scoresKBest.begin());
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
|
||||
// { return scoresKBest[i] > scoresKBest[j]; });
|
||||
// sort(scoresKBest.begin(), scoresKBest.end(), std::greater<double>());
|
||||
// featuresKBest.resize(k);
|
||||
// scoresKBest.resize(k);
|
||||
// }
|
||||
auto labels = samples.index({ -1, "..." });
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
for (int j = i + 1; j < n; ++j) {
|
||||
auto key = std::make_pair(i, j);
|
||||
auto value = conditionalMutualInformation(samples.index({ i, "..." }), samples.index({ j, "..." }), labels, weights);
|
||||
scoresKPairs.push_back({ key, value });
|
||||
}
|
||||
}
|
||||
// sort scores
|
||||
if (ascending) {
|
||||
sort(scoresKPairs.begin(), scoresKPairs.end(), [](auto& a, auto& b)
|
||||
{ return a.second < b.second; });
|
||||
|
||||
} else {
|
||||
sort(scoresKPairs.begin(), scoresKPairs.end(), [](auto& a, auto& b)
|
||||
{ return a.second > b.second; });
|
||||
}
|
||||
for (auto& [pairs, score] : scoresKPairs) {
|
||||
pairsKBest.push_back(pairs);
|
||||
}
|
||||
if (k != 0) {
|
||||
if (ascending) {
|
||||
for (int i = 0; i < n - k; ++i) {
|
||||
pairsKBest.erase(pairsKBest.begin());
|
||||
scoresKPairs.erase(scoresKPairs.begin());
|
||||
}
|
||||
} else {
|
||||
pairsKBest.resize(k);
|
||||
scoresKPairs.resize(k);
|
||||
}
|
||||
}
|
||||
return pairsKBest;
|
||||
}
|
||||
std::vector<int> Metrics::SelectKBestWeighted(const torch::Tensor& weights, bool ascending, unsigned k)
|
||||
@@ -107,7 +109,10 @@ namespace bayesnet {
|
||||
{
|
||||
return scoresKBest;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::pair<int, int>, double>> Metrics::getScoresKPairs() const
|
||||
{
|
||||
return scoresKPairs;
|
||||
}
|
||||
torch::Tensor Metrics::conditionalEdge(const torch::Tensor& weights)
|
||||
{
|
||||
auto result = std::vector<double>();
|
||||
|
@@ -18,6 +18,7 @@ namespace bayesnet {
|
||||
std::vector<int> SelectKBestWeighted(const torch::Tensor& weights, bool ascending = false, unsigned k = 0);
|
||||
std::vector<std::pair<int, int>> SelectKPairs(const torch::Tensor& weights, bool ascending = false, unsigned k = 0);
|
||||
std::vector<double> getScoresKBest() const;
|
||||
std::vector<std::pair<std::pair<int, int>, double>> getScoresKPairs() const;
|
||||
double mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
||||
double conditionalMutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& labels, const torch::Tensor& weights);
|
||||
torch::Tensor conditionalEdge(const torch::Tensor& weights);
|
||||
@@ -34,7 +35,7 @@ namespace bayesnet {
|
||||
std::vector<std::pair<T, T>> doCombinations(const std::vector<T>& source)
|
||||
{
|
||||
std::vector<std::pair<T, T>> result;
|
||||
for (int i = 0; i < source.size(); ++i) {
|
||||
for (int i = 0; i < source.size() - 1; ++i) {
|
||||
T temp = source[i];
|
||||
for (int j = i + 1; j < source.size(); ++j) {
|
||||
result.push_back({ temp, source[j] });
|
||||
@@ -42,7 +43,7 @@ namespace bayesnet {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
template <class T>
|
||||
template <class T>
|
||||
T pop_first(std::vector<T>& v)
|
||||
{
|
||||
T temp = v[0];
|
||||
@@ -54,7 +55,7 @@ namespace bayesnet {
|
||||
std::vector<double> scoresKBest;
|
||||
std::vector<int> featuresKBest; // sorted indices of the features
|
||||
std::vector<std::pair<int, int>> pairsKBest; // sorted indices of the pairs
|
||||
std::map<std::pair<int, int>, double> scoresKPairs;
|
||||
std::vector<std::pair<std::pair<int, int>, double>> scoresKPairs;
|
||||
double conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user