Skip to content

Commit

Permalink
Merge pull request #119 from Behnke02/main
Browse files Browse the repository at this point in the history
MERGE: Update to Logreg ML methods
  • Loading branch information
akielaries authored Dec 23, 2024
2 parents e4f0b66 + 152137e commit 549446c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
28 changes: 27 additions & 1 deletion include/openGPMP/ml/logreg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class LogReg {
* @return A vector of predicted probabilities
*/
std::vector<double> predict(const std::vector<std::vector<double>> &X_test);

/**
* @brief Computes the accuracy of the model on the given test data
* @param X_test The feature matrix of the test data
Expand All @@ -98,6 +98,32 @@ class LogReg {
double accuracy(const std::vector<std::vector<double>> &X_test,
const std::vector<int> &y_test);

/**
* @brief Computes the accuracy of the model on the given test data
* @param predictions The predicted labels of the test data
* @param y_test The true labels of the test data
* @return The accuracy of the model
*/
double accuracy(const std::vector<double> &predictions,
const std::vector<int> &y_test);

/*
* @brief Computes the precision of the model on the given test data
* @param X_test The feature matrix on the test data
* @param y_test The true labels of the test data
* @return The precision of the model
*/
double precision(const std::vector<std::vector<double>> &X_test,
const std::vector<int> &y_test);

/*
* @brief Computes the recall of the model on the given test data
* @param X_test The feature matrix on the test data
* @param y_test The true labels of the test data
* @return The recall of the model
*/
double recall(const std::vector<std::vector<double>> &X_test,
const std::vector<int> &y_test);
/**
* @brief Performs feature scaling on the input feature matrix
* @param X The feature matrix to be scaled
Expand Down
37 changes: 37 additions & 0 deletions modules/ml/logreg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,43 @@ gpmp::ml::LogReg::accuracy(const std::vector<std::vector<double>> &X_test,
return static_cast<double>(correct) / y_test.size();
}

double
gpmp::ml::LogReg::accuracy(const std::vector<double> &predictions,
const std::vector<int> &y_test){
int correct = 0;
for (size_t i = 0; i < predictions.size(); ++i) {
correct += ((predictions[i] >= 0.5 && y_test[i] == 1) ||
(predictions[i] < 0.5 && y_test[i] == 0));
}
return static_cast<double>(correct) / y_test.size();
}

double
gpmp::ml::LogReg::precision(const std::vector<std::vector<double>> &X_test,
const std::vector<int> &y_test){
std::vector<double> predictions = predict(X_test);
int true_positives = 0;
int false_positives = 0;
for (size_t i = 0; i < predictions.size(); ++i) {
true_positives += (predictions[i] >= 0.5 && y_test[i] == 1);
false_positives += (predictions[i] >= 0.5 && y_test[i] == 0);
}
return static_cast<double>(true_positives / (true_positives + false_positives));
}

double
gpmp::ml::LogReg::recall(const std::vector<std::vector<double>> &X_test,
const std::vector<int> &y_test){
std::vector<double> predictions = predict(X_test);
int true_positives = 0;
int false_negatives = 0;
for (size_t i = 0; i < predictions.size(); ++i) {
true_positives += (predictions[i] >= 0.5 && y_test[i] == 1);
false_negatives += (predictions[i] < 0.5 && y_test[i] == 1);
}
return static_cast<double>(true_positives / (true_positives + false_negatives));
}

double gpmp::ml::LogReg::sigmoid(double z) {
return 1.0 / (1.0 + exp(-z));
}
Expand Down

0 comments on commit 549446c

Please sign in to comment.