From 4b757ae7ba272a5de5717c26454eee177c8a8f69 Mon Sep 17 00:00:00 2001 From: Shihao Jing Date: Fri, 3 Mar 2017 16:16:52 -0500 Subject: [PATCH 1/6] add option which can output probabilities [confidence score] of each class given the feature --- .../naive_bayes/naive_bayes_classifier.hpp | 2 +- .../naive_bayes/naive_bayes_classifier_impl.hpp | 14 ++++++++++---- src/mlpack/methods/naive_bayes/nbc_main.cpp | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp b/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp index efd62bd00a..be59b44d84 100644 --- a/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp +++ b/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp @@ -123,7 +123,7 @@ class NaiveBayesClassifier * @param data List of data points. * @param results Vector that class predictions will be placed into. */ - void Classify(const MatType& data, arma::Row& results); + void Classify(const MatType& data, arma::Row& results, MatType& results_proba); //! Get the sample means for each class. const MatType& Means() const { return means; } diff --git a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp index b2d9bfeb7b..b690cb7035 100644 --- a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp +++ b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp @@ -162,8 +162,7 @@ void NaiveBayesClassifier::Train(const VecType& point, } template -void NaiveBayesClassifier::Classify(const MatType& data, - arma::Row& results) +void NaiveBayesClassifier::Classify(const MatType& data, arma::Row& results, MatType& results_proba) { // Check that the number of features in the test data is same as in the // training data. @@ -195,10 +194,18 @@ void NaiveBayesClassifier::Classify(const MatType& data, // Calculate probability as sum of logarithm to decrease floating point // errors. - testProbs.col(i) += (data.n_rows / -2.0 * log(2 * M_PI) - 0.5 * + testProbs.col(i) += (data.n_rows / - 2.0 * log(2 * M_PI) - 0.5 * log(arma::det(arma::diagmat(variances.col(i)))) + exponents); } + arma::vec log_prob_x(data.n_cols); // log(Prob(X)) + for (size_t j = 0; j < data.n_cols; ++j) + log_prob_x(j) = log(arma::accu(exp(testProbs.row(j)))); + + testProbs -= arma::repmat(log_prob_x, 1, testProbs.n_cols); + + results_proba = arma::mat(exp(testProbs.t())); + // Now calculate the label. for (size_t i = 0; i < data.n_cols; ++i) { @@ -206,7 +213,6 @@ void NaiveBayesClassifier::Classify(const MatType& data, arma::uword maxIndex = 0; arma::vec pointProbs = testProbs.row(i).t(); pointProbs.max(maxIndex); - results[i] = maxIndex; } diff --git a/src/mlpack/methods/naive_bayes/nbc_main.cpp b/src/mlpack/methods/naive_bayes/nbc_main.cpp index 71ab2b25af..95f55cc649 100644 --- a/src/mlpack/methods/naive_bayes/nbc_main.cpp +++ b/src/mlpack/methods/naive_bayes/nbc_main.cpp @@ -77,6 +77,8 @@ PARAM_FLAG("incremental_variance", "The variance of each class will be " PARAM_MATRIX_IN("test", "A matrix containing the test set.", "T"); PARAM_UMATRIX_OUT("output", "The matrix in which the predicted labels for the" " test set will be written.", "o"); +PARAM_MATRIX_OUT("output_proba", "The matrix in which the predicted probability of labels for the" + " test set will be written.", "p"); int main(int argc, char* argv[]) { @@ -98,8 +100,8 @@ int main(int argc, char* argv[]) Log::Warn << "--incremental_variance (-I) ignored because --training_file " << "(-t) is not specified." << endl; - if (!CLI::HasParam("output") && !CLI::HasParam("output_model")) - Log::Warn << "Neither --output_file (-o) nor --output_model_file (-M) " + if (!CLI::HasParam("output") && !CLI::HasParam("output_model") && !CLI::HasParam("output_proba")) + Log::Warn << "Neither --output_file (-o), nor --output_model_file (-M), nor --output_proba_file (-p)" << "specified; no output will be saved!" << endl; if (CLI::HasParam("output") && !CLI::HasParam("test")) @@ -110,6 +112,10 @@ int main(int argc, char* argv[]) Log::Warn << "--test_file (-T) specified, but classification results will " << "not be saved because --output_file (-o) is not specified." << endl; + if (!CLI::HasParam("output_proba") && CLI::HasParam("test")) + Log::Warn << "--test_file (-T) specified, but predicted probability of labels will " + << "not be saved because --output_proba_file (-p) is not specified." << endl; + // Either we have to train a model, or load a model. NBCModel model; if (CLI::HasParam("training")) @@ -168,8 +174,9 @@ int main(int argc, char* argv[]) // Time the running of the Naive Bayes Classifier. Row results; + mat results_proba = std::move(CLI::GetParam("output_proba"));; Timer::Start("nbc_testing"); - model.nbc.Classify(testingData, results); + model.nbc.Classify(testingData, results, results_proba); Timer::Stop("nbc_testing"); if (CLI::HasParam("output")) @@ -181,6 +188,9 @@ int main(int argc, char* argv[]) // Output results. CLI::GetParam>("output") = std::move(rawResults); } + + if (CLI::HasParam("output_proba")) + CLI::GetParam("output_proba") = std::move(results_proba); } if (CLI::HasParam("output_model")) From f41b840ba08c5d160eaa7234bf8960ae2cce9644 Mon Sep 17 00:00:00 2001 From: Shihao Jing Date: Sat, 4 Mar 2017 17:21:10 -0500 Subject: [PATCH 2/6] Add tests for posteriors --- src/mlpack/tests/nbc_test.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/mlpack/tests/nbc_test.cpp b/src/mlpack/tests/nbc_test.cpp index 38c255d12d..3f46460ed0 100644 --- a/src/mlpack/tests/nbc_test.cpp +++ b/src/mlpack/tests/nbc_test.cpp @@ -25,6 +25,7 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierTest) const char* testFilename = "testSet.csv"; const char* trainResultFilename = "trainRes.csv"; const char* testResultFilename = "testRes.csv"; + const char* testResultProbaFilename = "testResProba.csv"; size_t classes = 2; arma::mat trainData, trainRes, calcMat; @@ -60,16 +61,27 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierTest) arma::mat testData; arma::Mat testRes; + arma::mat testResProba; arma::Row calcVec; + arma::mat calcProba; data::Load(testFilename, testData, true); data::Load(testResultFilename, testRes, true); + data::Load(testResultProbaFilename, testResProba, true); testData.shed_row(testData.n_rows - 1); // Remove the labels. - nbcTest.Classify(testData, calcVec); + nbcTest.Classify(testData, calcVec, calcProba); for (size_t i = 0; i < testData.n_cols; i++) BOOST_REQUIRE_EQUAL(testRes(i), calcVec(i)); + + for (size_t i = 0; i < testResProba.n_cols; ++i) + { + for (size_t j = 0; j < testResProba.n_rows; ++j) + { + BOOST_REQUIRE_CLOSE(testResProba(i, j) + .00001, calcProba(i, j) + .00001, 0.01); + } + } } // The same test, but this one uses the incremental algorithm to calculate @@ -80,6 +92,7 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierIncrementalTest) const char* testFilename = "testSet.csv"; const char* trainResultFilename = "trainRes.csv"; const char* testResultFilename = "testRes.csv"; + const char* testResultProbaFilename = "testResProba.csv"; size_t classes = 2; arma::mat trainData, trainRes, calcMat; @@ -115,16 +128,23 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierIncrementalTest) arma::mat testData; arma::Mat testRes; + arma::mat testResProba; arma::Row calcVec; + arma::mat calcProba; data::Load(testFilename, testData, true); data::Load(testResultFilename, testRes, true); + data::Load(testResultProbaFilename, testResProba, true); testData.shed_row(testData.n_rows - 1); // Remove the labels. - nbcTest.Classify(testData, calcVec); + nbcTest.Classify(testData, calcVec, calcProba); for (size_t i = 0; i < testData.n_cols; i++) BOOST_REQUIRE_EQUAL(testRes(i), calcVec(i)); + + for (size_t i = 0; i < testResProba.n_cols; ++i) + for (size_t j = 0; j < testResProba.n_rows; ++j) + BOOST_REQUIRE_CLOSE(testResProba(i, j) + .00001, calcProba(i, j) + .00001, 0.01); } /** From 27230dfad7b9a3c29b57de725dd4b34905479b3c Mon Sep 17 00:00:00 2001 From: Shihao Jing Date: Sat, 4 Mar 2017 18:56:50 -0500 Subject: [PATCH 3/6] Add 'testResProba.csv' for testing posterior in nbc --- src/mlpack/tests/data/testResProba.csv | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/mlpack/tests/data/testResProba.csv diff --git a/src/mlpack/tests/data/testResProba.csv b/src/mlpack/tests/data/testResProba.csv new file mode 100644 index 0000000000..ece0ea7aba --- /dev/null +++ b/src/mlpack/tests/data/testResProba.csv @@ -0,0 +1,7 @@ +0.999999999998,1.86113163315e-12 +0.999999999996,3.82159650359e-12 +0.999999999998,2.42337174628e-12 +2.96941920603e-19,1.0 +2.96941920603e-19,1.0 +1.44611813707e-19,1.0 +2.28049205613e-19,1.0 From b671bc0c8483e2657308bee40a167d80bd07c88d Mon Sep 17 00:00:00 2001 From: Shihao Jing Date: Mon, 6 Mar 2017 14:51:22 -0500 Subject: [PATCH 4/6] Add JointLogLikelihood private function. Add overloads of Classify function. --- .../naive_bayes/naive_bayes_classifier.hpp | 76 ++++++++++- .../naive_bayes_classifier_impl.hpp | 126 +++++++++++++----- src/mlpack/methods/naive_bayes/nbc_main.cpp | 20 +-- 3 files changed, 173 insertions(+), 49 deletions(-) diff --git a/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp b/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp index be59b44d84..9eae589569 100644 --- a/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp +++ b/src/mlpack/methods/naive_bayes/naive_bayes_classifier.hpp @@ -1,6 +1,7 @@ /** * @file naive_bayes_classifier.hpp * @author Parikshit Ram (pram@cc.gatech.edu) + * @author Shihao Jing (shihao.jing810@gmail.com) * * A Naive Bayes Classifier which parametrically estimates the distribution of * the features. It is assumed that the features have been sampled from a @@ -110,20 +111,67 @@ class NaiveBayesClassifier void Train(const VecType& point, const size_t label); /** - * Given a bunch of data points, this function evaluates the class of each of - * those data points, and puts it in the vector 'results'. + * Classify the given point, using the training GaussianNB model. The predicted label is + * returned. + * + * @param point Point to classify. + */ + template + size_t Classify(const VecType& point) const; + + /** + * Classify the given point using the training GaussianNB model + * and also return estimates of the probability for + * each class in the given vector. + * + * @param point Point to classify. + * @param prediction This will be set to the predicted class of the point. + * @param probabilities This will be filled with class probabilities for the + * point. + */ + template + void Classify(const VecType& point, + size_t& prediction, + arma::vec& probabilities) const; + + /** + * Classify the given points using the training GaussianNB model. + * The predicted labels for each point are stored in the given vector. * * @code * arma::mat test_data; // each column is a test point * arma::Row results; * ... - * nbc.Classify(test_data, &results); + * nbc.Classify(test_data, results); * @endcode * * @param data List of data points. - * @param results Vector that class predictions will be placed into. + * @param predictions that class predictions will be placed into. */ - void Classify(const MatType& data, arma::Row& results, MatType& results_proba); + void Classify(const MatType& data, + arma::Row& predictions) const; + + /** + * Classify the given points using the training GaussianNB model + * and also return estimates of the probabilities for each class in the given matrix. + * The predicted labels for each point are stored in the given vector. + * + * @code + * arma::mat test_data; // each column is a test point + * arma::Row results; + * arma::mat resultsProbs; + * ... + * nbc.Classify(test_data, results, resultsProbs); + * @endcode + * + * @param data Set of points to classify. + * @param predictions This will be filled with predictions for each point. + * @param probabilities This will be filled with class probabilities for each + * point. Each row represents a point. + */ + void Classify(const MatType& data, + arma::Row& predictions, + arma::mat& probabilities) const; //! Get the sample means for each class. const MatType& Means() const { return means; } @@ -145,6 +193,7 @@ class NaiveBayesClassifier void Serialize(Archive& ar, const unsigned int /* version */); private: + //! Sample mean for each class. MatType means; //! Sample variances for each class. @@ -153,6 +202,23 @@ class NaiveBayesClassifier arma::vec probabilities; //! Number of training points seen so far. size_t trainingPoints; + + /** + * Compute the unnormalized posterior log probability of given point. + * + * @param point Data point to compute posterior log probability. + */ + template + arma::vec JointLogLikelihood(const VecType& point) const; + + /** + * Compute the unnormalized posterior log probability of given points. + * Results are returned as arma::mat, and each ling represents a points, + * each column represents posterior log probability of a class. + * + * @param @param data Set of points to compute posterior log probability. + */ + arma::mat JointLogLikelihood(const MatType& point) const; }; } // namespace naive_bayes diff --git a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp index b690cb7035..9d5587e631 100644 --- a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp +++ b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp @@ -2,6 +2,7 @@ * @file naive_bayes_classifier_impl.hpp * @author Parikshit Ram (pram@cc.gatech.edu) * @author Vahab Akbarzadeh (v.akbarzadeh@gmail.com) + * @author Shihao Jing (shihao.jing810@gmail.com) * * A Naive Bayes Classifier which parametrically estimates the distribution of * the features. This classifier makes its predictions based on the assumption @@ -162,23 +163,18 @@ void NaiveBayesClassifier::Train(const VecType& point, } template -void NaiveBayesClassifier::Classify(const MatType& data, arma::Row& results, MatType& results_proba) +template +arma::vec NaiveBayesClassifier::JointLogLikelihood( + const VecType& point) const { // Check that the number of features in the test data is same as in the // training data. - Log::Assert(data.n_rows == means.n_rows); + Log::Assert(point.n_rows == means.n_rows); - arma::vec probs = arma::log(probabilities); + arma::vec jll = arma::log(probabilities); arma::mat invVar = 1.0 / variances; - arma::mat testProbs = arma::repmat(probs.t(), data.n_cols, 1); - - results.set_size(data.n_cols); // No need to fill with anything yet. - - Log::Info << "Running Naive Bayes classifier on " << data.n_cols - << " data points with " << data.n_rows << " features each." << std::endl; - - // Calculate the joint probability for each of the data points for each of the + // Calculate the joint log likelihood of point for each of the // means.n_cols. // Loop over every class. @@ -186,37 +182,99 @@ void NaiveBayesClassifier::Classify(const MatType& data, arma::Row +arma::mat NaiveBayesClassifier::JointLogLikelihood( + const MatType& data) const +{ + arma::mat jll(data.n_cols, means.n_cols); for (size_t i = 0; i < data.n_cols; ++i) { - // Find the index of the class with maximum probability for this point. - arma::uword maxIndex = 0; - arma::vec pointProbs = testProbs.row(i).t(); - pointProbs.max(maxIndex); - results[i] = maxIndex; + jll.row(i) = JointLogLikelihood(data.col(i)).t(); } + return jll; +} - return; +template +template +size_t NaiveBayesClassifier::Classify(const VecType& point) const +{ + // find the label(class) with max joint log likelihood. + arma::vec jll = JointLogLikelihood(point); + arma::uword maxIndex = 0; + jll.max(maxIndex); + return maxIndex; +} + +template +template +void NaiveBayesClassifier::Classify(const VecType& point, + size_t& prediction, + arma::vec& probabilities) const +{ + // log(Prob(Y|X)) = Log(X|Y) + Log(Y) - Log(X); + // JointLogLikelihood = Log(X|Y) + Log(Y) + arma::vec jll = JointLogLikelihood(point); + double logProbX = log(arma::accu(exp(jll))); // Log(X) + jll -= logProbX; + + arma::uword maxIndex = 0; + jll.max(maxIndex); + prediction = maxIndex; + probabilities = exp(jll); // log(exp(value)) == value +} + +template +void NaiveBayesClassifier::Classify( + const MatType& data, + arma::Row& predictions) const +{ + predictions.set_size(data.n_cols); + + arma::mat jll = JointLogLikelihood(data); + + for (size_t i = 0; i < data.n_cols; ++i) + { + arma::uword maxIndex = 0; + jll.row(i).max(maxIndex); + predictions[i] = maxIndex; + } +} + +template +void NaiveBayesClassifier::Classify(const MatType& data, + arma::Row& predictions, + arma::mat& predictionProbs) const +{ + predictions.set_size(data.n_cols); + + arma::mat jll = JointLogLikelihood(data); + arma::vec logProbX(data.n_cols); // log(Prob(X)) + for (size_t j = 0; j < data.n_cols; ++j) + logProbX(j) = log(arma::accu(exp(jll.row(j)))); + + jll -= arma::repmat(logProbX, 1, data.n_cols); + + predictionProbs = exp(jll); + + for (size_t i = 0; i < data.n_cols; ++i) + { + arma::uword maxIndex = 0; + jll.row(i).max(maxIndex); + predictions[i] = maxIndex; + } } template diff --git a/src/mlpack/methods/naive_bayes/nbc_main.cpp b/src/mlpack/methods/naive_bayes/nbc_main.cpp index 95f55cc649..d38bc3af65 100644 --- a/src/mlpack/methods/naive_bayes/nbc_main.cpp +++ b/src/mlpack/methods/naive_bayes/nbc_main.cpp @@ -77,7 +77,7 @@ PARAM_FLAG("incremental_variance", "The variance of each class will be " PARAM_MATRIX_IN("test", "A matrix containing the test set.", "T"); PARAM_UMATRIX_OUT("output", "The matrix in which the predicted labels for the" " test set will be written.", "o"); -PARAM_MATRIX_OUT("output_proba", "The matrix in which the predicted probability of labels for the" +PARAM_MATRIX_OUT("output_probs", "The matrix in which the predicted probability of labels for the" " test set will be written.", "p"); int main(int argc, char* argv[]) @@ -100,7 +100,7 @@ int main(int argc, char* argv[]) Log::Warn << "--incremental_variance (-I) ignored because --training_file " << "(-t) is not specified." << endl; - if (!CLI::HasParam("output") && !CLI::HasParam("output_model") && !CLI::HasParam("output_proba")) + if (!CLI::HasParam("output") && !CLI::HasParam("output_model") && !CLI::HasParam("output_probs")) Log::Warn << "Neither --output_file (-o), nor --output_model_file (-M), nor --output_proba_file (-p)" << "specified; no output will be saved!" << endl; @@ -112,9 +112,9 @@ int main(int argc, char* argv[]) Log::Warn << "--test_file (-T) specified, but classification results will " << "not be saved because --output_file (-o) is not specified." << endl; - if (!CLI::HasParam("output_proba") && CLI::HasParam("test")) + if (!CLI::HasParam("output_probs") && CLI::HasParam("test")) Log::Warn << "--test_file (-T) specified, but predicted probability of labels will " - << "not be saved because --output_proba_file (-p) is not specified." << endl; + << "not be saved because --output_probs_file (-p) is not specified." << endl; // Either we have to train a model, or load a model. NBCModel model; @@ -173,24 +173,24 @@ int main(int argc, char* argv[]) << ")!" << std::endl; // Time the running of the Naive Bayes Classifier. - Row results; - mat results_proba = std::move(CLI::GetParam("output_proba"));; + Row predictions; + mat probabilities; Timer::Start("nbc_testing"); - model.nbc.Classify(testingData, results, results_proba); + model.nbc.Classify(testingData, predictions, probabilities); Timer::Stop("nbc_testing"); if (CLI::HasParam("output")) { // Un-normalize labels to prepare output. Row rawResults; - data::RevertLabels(results, model.mappings, rawResults); + data::RevertLabels(predictions, model.mappings, rawResults); // Output results. CLI::GetParam>("output") = std::move(rawResults); } - if (CLI::HasParam("output_proba")) - CLI::GetParam("output_proba") = std::move(results_proba); + if (CLI::HasParam("output_probs")) + CLI::GetParam("output_probs") = probabilities.t(); } if (CLI::HasParam("output_model")) From 7e2e330d0c07beff49fe02181815e6c1eb63d5ac Mon Sep 17 00:00:00 2001 From: Shihao Jing Date: Mon, 6 Mar 2017 16:16:52 -0500 Subject: [PATCH 5/6] fix bug in calling max() on row vector --- .../naive_bayes_classifier_impl.hpp | 6 +++-- src/mlpack/tests/nbc_test.cpp | 22 ++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp index 9d5587e631..4f16db9142 100644 --- a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp +++ b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp @@ -248,7 +248,8 @@ void NaiveBayesClassifier::Classify( for (size_t i = 0; i < data.n_cols; ++i) { arma::uword maxIndex = 0; - jll.row(i).max(maxIndex); + arma::vec pointProbs = jll.row(i).t(); + pointProbs.max(maxIndex); predictions[i] = maxIndex; } } @@ -272,7 +273,8 @@ void NaiveBayesClassifier::Classify(const MatType& data, for (size_t i = 0; i < data.n_cols; ++i) { arma::uword maxIndex = 0; - jll.row(i).max(maxIndex); + arma::vec pointProbs = jll.row(i).t(); + pointProbs.max(maxIndex); predictions[i] = maxIndex; } } diff --git a/src/mlpack/tests/nbc_test.cpp b/src/mlpack/tests/nbc_test.cpp index 3f46460ed0..c757c42c29 100644 --- a/src/mlpack/tests/nbc_test.cpp +++ b/src/mlpack/tests/nbc_test.cpp @@ -61,25 +61,26 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierTest) arma::mat testData; arma::Mat testRes; - arma::mat testResProba; + arma::mat testResProbs; arma::Row calcVec; - arma::mat calcProba; + arma::mat calcProbs; data::Load(testFilename, testData, true); data::Load(testResultFilename, testRes, true); - data::Load(testResultProbaFilename, testResProba, true); + data::Load(testResultProbaFilename, testResProbs, true); testData.shed_row(testData.n_rows - 1); // Remove the labels. - nbcTest.Classify(testData, calcVec, calcProba); + nbcTest.Classify(testData, calcVec, calcProbs); + calcProbs = calcProbs.t(); for (size_t i = 0; i < testData.n_cols; i++) BOOST_REQUIRE_EQUAL(testRes(i), calcVec(i)); - for (size_t i = 0; i < testResProba.n_cols; ++i) + for (size_t i = 0; i < testResProbs.n_cols; ++i) { - for (size_t j = 0; j < testResProba.n_rows; ++j) + for (size_t j = 0; j < testResProbs.n_rows; ++j) { - BOOST_REQUIRE_CLOSE(testResProba(i, j) + .00001, calcProba(i, j) + .00001, 0.01); + BOOST_REQUIRE_CLOSE(testResProbs(i, j) + 0.0001, calcProbs(i, j) + 0.0001, 0.01); } } } @@ -130,21 +131,22 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierIncrementalTest) arma::Mat testRes; arma::mat testResProba; arma::Row calcVec; - arma::mat calcProba; + arma::mat calcProbs; data::Load(testFilename, testData, true); data::Load(testResultFilename, testRes, true); data::Load(testResultProbaFilename, testResProba, true); testData.shed_row(testData.n_rows - 1); // Remove the labels. - nbcTest.Classify(testData, calcVec, calcProba); + nbcTest.Classify(testData, calcVec, calcProbs); + calcProbs = calcProbs.t(); for (size_t i = 0; i < testData.n_cols; i++) BOOST_REQUIRE_EQUAL(testRes(i), calcVec(i)); for (size_t i = 0; i < testResProba.n_cols; ++i) for (size_t j = 0; j < testResProba.n_rows; ++j) - BOOST_REQUIRE_CLOSE(testResProba(i, j) + .00001, calcProba(i, j) + .00001, 0.01); + BOOST_REQUIRE_CLOSE(testResProba(i, j) + .00001, calcProbs(i, j) + .00001, 0.01); } /** From ed05e713dd9f19a2e0cb0f75bc232325cc9d07bc Mon Sep 17 00:00:00 2001 From: Shihao Jing Date: Tue, 7 Mar 2017 10:01:27 -0500 Subject: [PATCH 6/6] changed names with 'proba' to 'probs' --- .../tests/data/{testResProba.csv => testResProbs.csv} | 0 src/mlpack/tests/nbc_test.cpp | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) rename src/mlpack/tests/data/{testResProba.csv => testResProbs.csv} (100%) diff --git a/src/mlpack/tests/data/testResProba.csv b/src/mlpack/tests/data/testResProbs.csv similarity index 100% rename from src/mlpack/tests/data/testResProba.csv rename to src/mlpack/tests/data/testResProbs.csv diff --git a/src/mlpack/tests/nbc_test.cpp b/src/mlpack/tests/nbc_test.cpp index c757c42c29..7cf3078eb4 100644 --- a/src/mlpack/tests/nbc_test.cpp +++ b/src/mlpack/tests/nbc_test.cpp @@ -25,7 +25,7 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierTest) const char* testFilename = "testSet.csv"; const char* trainResultFilename = "trainRes.csv"; const char* testResultFilename = "testRes.csv"; - const char* testResultProbaFilename = "testResProba.csv"; + const char* testResultProbsFilename = "testResProbs.csv"; size_t classes = 2; arma::mat trainData, trainRes, calcMat; @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierTest) arma::mat calcProbs; data::Load(testFilename, testData, true); data::Load(testResultFilename, testRes, true); - data::Load(testResultProbaFilename, testResProbs, true); + data::Load(testResultProbsFilename, testResProbs, true); testData.shed_row(testData.n_rows - 1); // Remove the labels. @@ -93,7 +93,7 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierIncrementalTest) const char* testFilename = "testSet.csv"; const char* trainResultFilename = "trainRes.csv"; const char* testResultFilename = "testRes.csv"; - const char* testResultProbaFilename = "testResProba.csv"; + const char* testResultProbsFilename = "testResProbs.csv"; size_t classes = 2; arma::mat trainData, trainRes, calcMat; @@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(NaiveBayesClassifierIncrementalTest) arma::mat calcProbs; data::Load(testFilename, testData, true); data::Load(testResultFilename, testRes, true); - data::Load(testResultProbaFilename, testResProba, true); + data::Load(testResultProbsFilename, testResProba, true); testData.shed_row(testData.n_rows - 1); // Remove the labels.