refactor predict and predict_proba

This commit is contained in:
Ricardo Montañana Gómez 2023-07-07 00:33:04 +02:00
parent c22eba3d5c
commit c4836bd5e3
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
2 changed files with 11 additions and 13 deletions

View File

@ -151,13 +151,17 @@ namespace bayesnet {
for (int col = 0; col < samples.size(); ++col) { for (int col = 0; col < samples.size(); ++col) {
sample.push_back(samples[col][row]); sample.push_back(samples[col][row]);
} }
predictions.push_back(predict_sample(sample).first); vector<double> classProbabilities = predict_sample(sample);
// Find the class with the maximum posterior probability
auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
int predictedClass = distance(classProbabilities.begin(), maxElem);
predictions.push_back(predictedClass);
} }
return predictions; return predictions;
} }
vector<pair<int, double>> Network::predict_proba(const vector<vector<int>>& samples) vector<vector<double>> Network::predict_proba(const vector<vector<int>>& samples)
{ {
vector<pair<int, double>> predictions; vector<vector<double>> predictions;
vector<int> sample; vector<int> sample;
for (int row = 0; row < samples[0].size(); ++row) { for (int row = 0; row < samples[0].size(); ++row) {
sample.clear(); sample.clear();
@ -179,7 +183,7 @@ namespace bayesnet {
} }
return (double)correct / y_pred.size(); return (double)correct / y_pred.size();
} }
pair<int, double> Network::predict_sample(const vector<int>& sample) vector<double> Network::predict_sample(const vector<int>& sample)
{ {
// Ensure the sample size is equal to the number of features // Ensure the sample size is equal to the number of features
if (sample.size() != features.size()) { if (sample.size() != features.size()) {
@ -190,14 +194,8 @@ namespace bayesnet {
for (int i = 0; i < sample.size(); ++i) { for (int i = 0; i < sample.size(); ++i) {
evidence[features[i]] = sample[i]; evidence[features[i]] = sample[i];
} }
vector<double> classProbabilities = exactInference(evidence); return exactInference(evidence);
// Find the class with the maximum posterior probability
auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
int predictedClass = distance(classProbabilities.begin(), maxElem);
double maxProbability = *maxElem;
return make_pair(predictedClass, maxProbability);
} }
double Network::computeFactor(map<string, int>& completeEvidence) double Network::computeFactor(map<string, int>& completeEvidence)
{ {

View File

@ -16,7 +16,7 @@ namespace bayesnet {
string className; string className;
int laplaceSmoothing; int laplaceSmoothing;
bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&); bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&);
pair<int, double> predict_sample(const vector<int>&); vector<double> predict_sample(const vector<int>&);
vector<double> exactInference(map<string, int>&); vector<double> exactInference(map<string, int>&);
double computeFactor(map<string, int>&); double computeFactor(map<string, int>&);
public: public:
@ -34,7 +34,7 @@ namespace bayesnet {
string getClassName(); string getClassName();
void fit(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&); void fit(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&);
vector<int> predict(const vector<vector<int>>&); vector<int> predict(const vector<vector<int>>&);
vector<pair<int, double>> predict_proba(const vector<vector<int>>&); vector<vector<double>> predict_proba(const vector<vector<int>>&);
double score(const vector<vector<int>>&, const vector<int>&); double score(const vector<vector<int>>&, const vector<int>&);
inline string version() { return "0.1.0"; } inline string version() { return "0.1.0"; }
}; };