Create a high dimensions test case and fix the build error
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
### mlpack ?.?.?
|
||||
###### ????-??-??
|
||||
<<<<<<< HEAD
|
||||
* Enforce CMake version check for ensmallen (#2032).
|
||||
|
||||
* Fix CMake check for Armadillo version (#2029).
|
||||
|
||||
### mlpack 3.2.0
|
||||
###### 2019-09-25
|
||||
=======
|
||||
* Fix some potential infinity errors in Naive Bayes Classifier (#2022).
|
||||
|
||||
>>>>>>> a16d55f... Edit according to comments of Marcus and Ryan.
|
||||
* Fix occasionally-failing RADICAL test (#1924).
|
||||
|
||||
* Fix gcc 9 OpenMP compilation issue (#1970).
|
||||
|
||||
@@ -272,8 +272,9 @@ void NaiveBayesClassifier<ModelMatType>::Classify(
|
||||
// To prevent underflow in log of sum of exp of x operation (where x is a
|
||||
// small negative value), we use logsumexp(x - max(x)) + max(x).
|
||||
const double maxValue = arma::max(logLikelihoods);
|
||||
probabilities = exp(logLikelihoods - log(arma::accu(exp(logLikelihoods -
|
||||
maxValue))) + maxValue); // log(exp(value)) == value.
|
||||
const double logProbX = log(arma::accu(exp(logLikelihoods - maxValue))) +
|
||||
maxValue;
|
||||
probabilities = exp(logLikelihoods - logProbX); // log(exp(value)) == value.
|
||||
|
||||
arma::uword maxIndex = 0;
|
||||
logLikelihoods.max(maxIndex);
|
||||
@@ -324,7 +325,7 @@ void NaiveBayesClassifier<ModelMatType>::Classify(
|
||||
LogLikelihood(data, logLikelihoods);
|
||||
|
||||
predictionProbs.set_size(arma::size(logLikelihoods));
|
||||
double maxValue;
|
||||
double maxValue, logProbX;
|
||||
for (size_t j = 0; j < data.n_cols; ++j)
|
||||
{
|
||||
// The LogLikelihood() gives us the unnormalized log likelihood which is
|
||||
@@ -332,8 +333,9 @@ void NaiveBayesClassifier<ModelMatType>::Classify(
|
||||
// Besides, to prevent underflow in log of sum of exp of x operation (where
|
||||
// x is a small negative value), we use logsumexp(x - max(x)) + max(x).
|
||||
maxValue = arma::max(logLikelihoods.col(j));
|
||||
predictionProbs.col(j) = arma::exp(logLikelihoods.col(j) -
|
||||
log(arma::accu(exp(logLikelihoods.col(j) - maxValue))) + maxValue);
|
||||
logProbX = log(arma::accu(exp(logLikelihoods.col(j) -
|
||||
maxValue))) + maxValue;
|
||||
predictionProbs.col(j) = arma::exp(logLikelihoods.col(j) - logProbX);
|
||||
}
|
||||
|
||||
// Now calculate maximum probabilities for each point.
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,50 @@
|
||||
3
|
||||
2
|
||||
0
|
||||
0
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
3
|
||||
2
|
||||
4
|
||||
2
|
||||
1
|
||||
2
|
||||
3
|
||||
1
|
||||
2
|
||||
4
|
||||
4
|
||||
1
|
||||
3
|
||||
0
|
||||
2
|
||||
0
|
||||
0
|
||||
2
|
||||
0
|
||||
1
|
||||
3
|
||||
3
|
||||
2
|
||||
2
|
||||
2
|
||||
3
|
||||
3
|
||||
3
|
||||
3
|
||||
3
|
||||
0
|
||||
0
|
||||
4
|
||||
3
|
||||
3
|
||||
0
|
||||
3
|
||||
2
|
||||
3
|
||||
2
|
||||
1
|
||||
1
|
||||
|
File diff suppressed because one or more lines are too long
@@ -0,0 +1,200 @@
|
||||
1
|
||||
4
|
||||
2
|
||||
2
|
||||
1
|
||||
0
|
||||
1
|
||||
0
|
||||
0
|
||||
4
|
||||
0
|
||||
4
|
||||
3
|
||||
4
|
||||
3
|
||||
2
|
||||
4
|
||||
2
|
||||
2
|
||||
2
|
||||
4
|
||||
1
|
||||
2
|
||||
1
|
||||
3
|
||||
0
|
||||
4
|
||||
1
|
||||
4
|
||||
4
|
||||
4
|
||||
0
|
||||
3
|
||||
4
|
||||
3
|
||||
1
|
||||
3
|
||||
2
|
||||
3
|
||||
0
|
||||
4
|
||||
1
|
||||
4
|
||||
1
|
||||
4
|
||||
2
|
||||
1
|
||||
4
|
||||
2
|
||||
1
|
||||
2
|
||||
0
|
||||
2
|
||||
2
|
||||
4
|
||||
2
|
||||
0
|
||||
2
|
||||
0
|
||||
3
|
||||
3
|
||||
3
|
||||
0
|
||||
2
|
||||
1
|
||||
4
|
||||
3
|
||||
1
|
||||
2
|
||||
2
|
||||
4
|
||||
0
|
||||
1
|
||||
3
|
||||
4
|
||||
4
|
||||
4
|
||||
2
|
||||
4
|
||||
2
|
||||
3
|
||||
4
|
||||
4
|
||||
3
|
||||
2
|
||||
3
|
||||
3
|
||||
4
|
||||
3
|
||||
4
|
||||
2
|
||||
4
|
||||
0
|
||||
3
|
||||
3
|
||||
1
|
||||
3
|
||||
4
|
||||
2
|
||||
1
|
||||
2
|
||||
3
|
||||
1
|
||||
3
|
||||
3
|
||||
0
|
||||
4
|
||||
0
|
||||
0
|
||||
3
|
||||
2
|
||||
1
|
||||
0
|
||||
3
|
||||
2
|
||||
1
|
||||
0
|
||||
0
|
||||
1
|
||||
0
|
||||
2
|
||||
2
|
||||
4
|
||||
2
|
||||
3
|
||||
1
|
||||
4
|
||||
4
|
||||
2
|
||||
3
|
||||
4
|
||||
0
|
||||
2
|
||||
2
|
||||
0
|
||||
4
|
||||
0
|
||||
3
|
||||
1
|
||||
4
|
||||
4
|
||||
2
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
3
|
||||
4
|
||||
3
|
||||
2
|
||||
0
|
||||
4
|
||||
3
|
||||
3
|
||||
4
|
||||
0
|
||||
3
|
||||
1
|
||||
3
|
||||
4
|
||||
3
|
||||
2
|
||||
2
|
||||
4
|
||||
0
|
||||
0
|
||||
0
|
||||
0
|
||||
1
|
||||
4
|
||||
0
|
||||
3
|
||||
4
|
||||
3
|
||||
1
|
||||
4
|
||||
0
|
||||
1
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
||||
3
|
||||
2
|
||||
4
|
||||
3
|
||||
2
|
||||
0
|
||||
1
|
||||
4
|
||||
2
|
||||
0
|
||||
2
|
||||
3
|
||||
0
|
||||
0
|
||||
2
|
||||
1
|
||||
3
|
||||
1
|
||||
|
@@ -314,4 +314,45 @@ BOOST_AUTO_TEST_CASE(SeparateTrainIndividualIncrementalTest)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if NaiveBayesClassifier::Classify() works properly for a high
|
||||
* dimension datasets.
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(NaiveBayesClassifierHighDimensionsTest)
|
||||
{
|
||||
// Set file names of dataset of training and test.
|
||||
// The training dataset has 5 classes and each class has 1,000 dimensions.
|
||||
const char* trainFilename = "nbc_high_dim_train.csv";
|
||||
const char* testFilename = "nbc_high_dim_test.csv";
|
||||
const char* trainLabelsFileName = "nbc_high_dim_train_labels.csv";
|
||||
const char* testLabelsFilename = "nbc_high_dim_test_labels.csv";
|
||||
|
||||
size_t classes = 5;
|
||||
|
||||
// Create variables for training and assign data to them.
|
||||
arma::mat trainData;
|
||||
arma::Row<size_t> trainLabels;
|
||||
data::Load(trainFilename, trainData, true);
|
||||
data::Load(trainLabelsFileName, trainLabels, true);
|
||||
|
||||
// Initialize and train a NBC model.
|
||||
NaiveBayesClassifier<> nbcTest(trainData, trainLabels, classes);
|
||||
|
||||
// Create variables for test and assign data to them.
|
||||
arma::mat testData, calcProbs;
|
||||
arma::Row<size_t> testLabels;
|
||||
arma::Row<size_t> calcVec;
|
||||
data::Load(testFilename, testData, true);
|
||||
data::Load(testLabelsFilename, testLabels, true);
|
||||
|
||||
// Classify observations in the test dataset. To use Classify() method with
|
||||
// a parameter for probabilities of predictions, we pass 'calcProbs' to the
|
||||
// method.
|
||||
nbcTest.Classify(testData, calcVec, calcProbs);
|
||||
|
||||
// Check the results.
|
||||
for (size_t i = 0; i < calcVec.n_cols; i++)
|
||||
BOOST_REQUIRE_EQUAL(calcVec(i), testLabels(i));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
Reference in New Issue
Block a user