Add selectKParis method
This commit is contained in:
@@ -30,6 +30,44 @@ namespace bayesnet {
|
||||
}
|
||||
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
|
||||
}
|
||||
std::vector<std::pair<int, int>> Metrics::SelectKPairs(const torch::Tensor& weights, bool ascending, unsigned k)
|
||||
{
|
||||
// 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);
|
||||
// }
|
||||
return pairsKBest;
|
||||
}
|
||||
std::vector<int> Metrics::SelectKBestWeighted(const torch::Tensor& weights, bool ascending, unsigned k)
|
||||
{
|
||||
// Return the K Best features
|
||||
|
@@ -16,6 +16,7 @@ namespace bayesnet {
|
||||
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<std::pair<int, int>> SelectKPairs(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);
|
||||
double conditionalMutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& labels, const torch::Tensor& weights);
|
||||
@@ -41,7 +42,7 @@ namespace bayesnet {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
template <class T>
|
||||
template <class T>
|
||||
T pop_first(std::vector<T>& v)
|
||||
{
|
||||
T temp = v[0];
|
||||
@@ -52,6 +53,8 @@ namespace bayesnet {
|
||||
int classNumStates = 0;
|
||||
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;
|
||||
double conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user