Complete predict & predict_proba with voting & probabilities

This commit is contained in:
2024-02-23 23:11:14 +01:00
parent 52abd2d670
commit 8477698d8d
5 changed files with 161 additions and 283 deletions

View File

@@ -51,32 +51,6 @@ namespace bayesnet {
result /= sum;
return result;
}
std::vector<std::vector<double>> Ensemble::voting(std::vector<std::vector<int>>& votes)
{
// Convert n_models x m matrix to a m x n_class_states matrix
std::vector<std::vector<double>> y_pred_final;
int numClasses = states.at(className).size();
auto sum = std::reduce(significanceModels.begin(), significanceModels.end());
// y_pred is m x n_models with the prediction of every model for each sample
std::cout << std::string(80, '*') << std::endl;
for (int i = 0; i < votes.size(); ++i) {
// n_votes store in each index (value of class) the significance added by each model
// i.e. n_votes[0] contains how much value has the value 0 of class. That value is generated by the models predictions
std::vector<double> n_votes(numClasses, 0.0);
for (int j = 0; j < n_models; ++j) {
n_votes[votes[i][j]] += significanceModels.at(j);
}
for (auto& x : n_votes) {
std::cout << x << " ";
}
std::cout << std::endl;
// To only do one division per result and gain precision
std::transform(n_votes.begin(), n_votes.end(), n_votes.begin(), [sum](double x) { return x / sum; });
y_pred_final.push_back(n_votes);
}
std::cout << std::string(80, '*') << std::endl;
return y_pred_final;
}
std::vector<std::vector<double>> Ensemble::predict_proba(std::vector<std::vector<int>>& X)
{
if (!fitted) {
@@ -94,7 +68,6 @@ namespace bayesnet {
std::vector<int> Ensemble::predict(std::vector<std::vector<int>>& X)
{
auto res = predict_proba(X);
std::cout << "res: " << res.size() << ", " << res[0].size() << std::endl;
return compute_arg_max(res);
}
torch::Tensor Ensemble::predict(torch::Tensor& X)
@@ -151,6 +124,13 @@ namespace bayesnet {
}
return y_pred;
}
std::vector<std::vector<double>> Ensemble::predict_average_voting(std::vector<std::vector<int>>& X)
{
torch::Tensor Xt = bayesnet::vectorToTensor(X, false);
auto y_pred = predict_average_voting(Xt);
std::vector<std::vector<double>> result = tensorToVectorDouble(y_pred);
return result;
}
torch::Tensor Ensemble::predict_average_voting(torch::Tensor& X)
{
// Build a m x n_models tensor with the predictions of each model
@@ -169,21 +149,6 @@ namespace bayesnet {
}
return voting(y_pred);
}
std::vector<std::vector<double>> Ensemble::predict_average_voting(std::vector<std::vector<int>>& X)
{
auto Xt = vectorToTensor(X);
auto y_pred = predict_average_voting(Xt);
auto res = voting(y_pred);
std::vector<std::vector<double>> result;
// Iterate over cols
for (int i = 0; i < res.size(1); ++i) {
auto col_tensor = res.index({ "...", i });
auto col = std::vector<double>(col_tensor.data_ptr<double>(), col_tensor.data_ptr<double>() + res.size(0));
result.push_back(col);
}
return result;
//return tensorToVector<double>(res);
}
float Ensemble::score(torch::Tensor& X, torch::Tensor& y)
{
auto y_pred = predict(X);