src/mlpack/tests/logistic_regression_test.cpp
BOOST_AUTO_TEST_CASE(LogisticRegressionLBFGSGaussianTest)
{
  // Generate a two-Gaussian dataset.
  GaussianDistribution g1(arma::vec("1.0 1.0 1.0"), arma::eye<arma::mat>(3, 3));
  GaussianDistribution g2(arma::vec("9.0 9.0 9.0"), arma::eye<arma::mat>(3, 3));

  arma::mat data(3, 1000);
  arma::Row<size_t> responses(1000);
  for (size_t i = 0; i < 500; ++i)
  {
    data.col(i) = g1.Random();
    responses[i] = 0;
  }
  for (size_t i = 500; i < 1000; ++i)
  {
    data.col(i) = g2.Random();
    responses[i] = 1;
  }

  // Now train a logistic regression object on it.
  LogisticRegression<> lr(data.n_rows, 0.5);
  lr.Train<ens::L_BFGS>(data, responses);

  // Ensure that the error is close to zero.
  const double acc = lr.ComputeAccuracy(data, responses);
  BOOST_REQUIRE_CLOSE(acc, 100.0, 0.3); // 0.3% error tolerance.

  // Create a test set.
  for (size_t i = 0; i < 500; ++i)
  {
    data.col(i) = g1.Random();
    responses[i] = 0;
  }
  for (size_t i = 500; i < 1000; ++i)
  {
    data.col(i) = g2.Random();
    responses[i] = 1;
  }

  // Ensure that the error is close to zero.
  const double testAcc = lr.ComputeAccuracy(data, responses);

  BOOST_REQUIRE_CLOSE(testAcc, 100.0, 0.6); // 0.6% error tolerance.
}
// Test training of logistic regression on two Gaussians and ensure it's
// properly separable.
Test training of logistic regression on two Gaussians and ensure it's
properly separable.
Test training of logistic regression on two Gaussians and ensure it ' s properly separable .
BOOST_AUTO_TEST_CASE(LogisticRegressionLBFGSGaussianTest)
{
  GaussianDistribution g1(arma::vec("1.0 1.0 1.0"), arma::eye<arma::mat>(3, 3));
  GaussianDistribution g2(arma::vec("9.0 9.0 9.0"), arma::eye<arma::mat>(3, 3));
  arma::mat data(3, 1000);
  arma::Row<size_t> responses(1000);
  for (size_t i = 0; i < 500; ++i)
  {
    data.col(i) = g1.Random();
    responses[i] = 0;
  }
  for (size_t i = 500; i < 1000; ++i)
  {
    data.col(i) = g2.Random();
    responses[i] = 1;
  }
  LogisticRegression<> lr(data.n_rows, 0.5);
  lr.Train<ens::L_BFGS>(data, responses);
  const double acc = lr.ComputeAccuracy(data, responses);
  BOOST_REQUIRE_CLOSE(acc, 100.0, 0.3); 
  for (size_t i = 0; i < 500; ++i)
  {
    data.col(i) = g1.Random();
    responses[i] = 0;
  }
  for (size_t i = 500; i < 1000; ++i)
  {
    data.col(i) = g2.Random();
    responses[i] = 1;
  }
  const double testAcc = lr.ComputeAccuracy(data, responses);
  BOOST_REQUIRE_CLOSE(testAcc, 100.0, 0.6); 
}
BOOST_AUTO_TEST_CASE ( LogisticRegressionLBFGSGaussianTest ) { GaussianDistribution g1 ( arma :: vec ( " " ) , arma :: eye < arma :: mat > ( 3 , 3 ) ) ; GaussianDistribution g2 ( arma :: vec ( " " ) , arma :: eye < arma :: mat > ( 3 , 3 ) ) ; arma :: mat data ( 3 , 1000 ) ; arma :: Row < size_t > responses ( 1000 ) ; for ( size_t i = 0 ; i < 500 ; ++ i ) { data . col ( i ) = g1 . Random ( ) ; responses [ i ] = 0 ; } for ( size_t i = 500 ; i < 1000 ; ++ i ) { data . col ( i ) = g2 . Random ( ) ; responses [ i ] = 1 ; } LogisticRegression < > lr ( data . n_rows , 0.5 ) ; lr . Train < ens :: L_BFGS > ( data , responses ) ; const double acc = lr . ComputeAccuracy ( data , responses ) ; BOOST_REQUIRE_CLOSE ( acc , 100.0 , 0.3 ) ; for ( size_t i = 0 ; i < 500 ; ++ i ) { data . col ( i ) = g1 . Random ( ) ; responses [ i ] = 0 ; } for ( size_t i = 500 ; i < 1000 ; ++ i ) { data . col ( i ) = g2 . Random ( ) ; responses [ i ] = 1 ; } const double testAcc = lr . ComputeAccuracy ( data , responses ) ; BOOST_REQUIRE_CLOSE ( testAcc , 100.0 , 0.6 ) ; }
Test training of logistic regression on two Gaussians and ensure it's
properly separable.
Test training of logistic regression on two Gaussians and ensure it ' s properly separable .
// Generate a two-Gaussian dataset. // Now train a logistic regression object on it. // Ensure that the error is close to zero. // 0.3% error tolerance. // Create a test set. // Ensure that the error is close to zero. // 0.6% error tolerance.