Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | List of all members
EnsembleNaiveBayes Class Reference

#include <naive_bayes.h>

Public Member Functions

 EnsembleNaiveBayes (const std::vector< MultidimArray< double > > &features, const Matrix1D< double > &priorProbs, int discreteLevels, int numberOfClassifiers, double samplingFeatures, double samplingIndividuals, const std::string &newJudgeCombination)
 Constructor. More...
 
 ~EnsembleNaiveBayes ()
 Destructor. More...
 
 EnsembleNaiveBayes (const EnsembleNaiveBayes &other)
 Copy constructor. More...
 
EnsembleNaiveBayesoperator= (const EnsembleNaiveBayes &other)
 Assignment. More...
 
void setCostMatrix (const Matrix2D< double > &cost)
 Set cost matrix. More...
 
int doInference (const Matrix1D< double > &newFeatures, double &cost, MultidimArray< int > &votes, Matrix1D< double > &classesProbs, Matrix1D< double > &allCosts)
 Returns the class with the largest probability given a set of features. More...
 

Public Attributes

std::vector< NaiveBayes *> ensemble
 Ensemble of classifiers. More...
 
std::vector< MultidimArray< int > > ensembleFeatures
 Ensemble of features for each classifier. More...
 
int K
 Number of classes. More...
 
std::string judgeCombination
 Judge combination. More...
 

Detailed Description

Ensemble NaiveBayes classifier.

Definition at line 127 of file naive_bayes.h.

Constructor & Destructor Documentation

◆ EnsembleNaiveBayes() [1/2]

EnsembleNaiveBayes::EnsembleNaiveBayes ( const std::vector< MultidimArray< double > > &  features,
const Matrix1D< double > &  priorProbs,
int  discreteLevels,
int  numberOfClassifiers,
double  samplingFeatures,
double  samplingIndividuals,
const std::string &  newJudgeCombination 
)

Constructor.

Definition at line 431 of file naive_bayes.cpp.

437 {
438  int NFeatures=XSIZE(features[0]);
439  int NsubFeatures=CEIL(NFeatures*samplingFeatures);
440  K=features.size();
441  judgeCombination=newJudgeCombination;
442 
443 #ifdef WEIGHTED_SAMPLING
444  // Measure the classification power of each variable
445  auto *nb_weights=new NaiveBayes(features, priorProbs, discreteLevels);
446  MultidimArray<double> weights=nb_weights->__weights;
447  delete nb_weights;
448  double sumWeights=weights.sum();
449 #endif
450 
451  for (int n=0; n<numberOfClassifiers; n++)
452  {
453  // Produce the set of features for this subclassifier
454  MultidimArray<int> subFeatures(NsubFeatures);
455  FOR_ALL_ELEMENTS_IN_ARRAY1D(subFeatures)
456  {
457 #ifdef WEIGHTED_SAMPLING
458  double random_sum_weight=rnd_unif(0,sumWeights);
459  int j=0;
460  do
461  {
462  double wj=DIRECT_A1D_ELEM(weights,j);
463  if (wj<random_sum_weight)
464  {
465  random_sum_weight-=wj;
466  j++;
467  if (j==NFeatures)
468  {
469  j=NFeatures-1;
470  break;
471  }
472  }
473  else
474  break;
475  }
476  while (true);
477  DIRECT_A1D_ELEM(subFeatures,i)=j;
478 #else
479 
480  DIRECT_A1D_ELEM(subFeatures,i)=round(rnd_unif(0,NFeatures-1));
481 #endif
482 
483  }
484 
485  // Container for the new training sample
486  std::vector< MultidimArray<double> > newFeatures;
487 
488  // Produce the data set for each class
489  for (int k=0; k<K; k++)
490  {
491  int NIndividuals=YSIZE(features[k]);
492  int NsubIndividuals=CEIL(NIndividuals*samplingIndividuals);
493  MultidimArray<int> subIndividuals(NsubIndividuals);
494  FOR_ALL_ELEMENTS_IN_ARRAY1D(subIndividuals)
495  subIndividuals(i)=ROUND(rnd_unif(0,NsubIndividuals-1));
496 
497  MultidimArray<double> newFeaturesK;
498  newFeaturesK.initZeros(NsubIndividuals,NsubFeatures);
499  const MultidimArray<double>& features_k=features[k];
500  FOR_ALL_ELEMENTS_IN_ARRAY2D(newFeaturesK)
501  DIRECT_A2D_ELEM(newFeaturesK,i,j)=DIRECT_A2D_ELEM(features_k,
502  DIRECT_A1D_ELEM(subIndividuals,i),
503  DIRECT_A1D_ELEM(subFeatures,j));
504 
505  newFeatures.push_back(newFeaturesK);
506  }
507 
508  // Create a Naive Bayes classifier with this data
509  auto *nb=new NaiveBayes(newFeatures, priorProbs, discreteLevels);
510  ensemble.push_back(nb);
511  ensembleFeatures.push_back(subFeatures);
512  }
513 }
#define YSIZE(v)
std::string judgeCombination
Judge combination.
Definition: naive_bayes.h:140
std::vector< NaiveBayes *> ensemble
Ensemble of classifiers.
Definition: naive_bayes.h:131
#define DIRECT_A2D_ELEM(v, i, j)
std::vector< MultidimArray< int > > ensembleFeatures
Ensemble of features for each classifier.
Definition: naive_bayes.h:134
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
double rnd_unif()
#define DIRECT_A1D_ELEM(v, i)
#define CEIL(x)
Definition: xmipp_macros.h:225
#define XSIZE(v)
#define ROUND(x)
Definition: xmipp_macros.h:210
#define j
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
int round(double x)
Definition: ap.cpp:7245
void initZeros(const MultidimArray< T1 > &op)
int * n
double sum() const
int K
Number of classes.
Definition: naive_bayes.h:137

◆ ~EnsembleNaiveBayes()

EnsembleNaiveBayes::~EnsembleNaiveBayes ( )

Destructor.

Definition at line 516 of file naive_bayes.cpp.

517 {
518  int nmax=ensemble.size();
519  for (int n=0; n<nmax; n++)
520  delete ensemble[n];
521 }
int * nmax
std::vector< NaiveBayes *> ensemble
Ensemble of classifiers.
Definition: naive_bayes.h:131
int * n

◆ EnsembleNaiveBayes() [2/2]

EnsembleNaiveBayes::EnsembleNaiveBayes ( const EnsembleNaiveBayes other)
inline

Copy constructor.

Definition at line 154 of file naive_bayes.h.

155  {
156  *this=other;
157  }

Member Function Documentation

◆ doInference()

int EnsembleNaiveBayes::doInference ( const Matrix1D< double > &  newFeatures,
double &  cost,
MultidimArray< int > &  votes,
Matrix1D< double > &  classesProbs,
Matrix1D< double > &  allCosts 
)

Returns the class with the largest probability given a set of features.

Definition at line 550 of file naive_bayes.cpp.

553 {
554  int nmax=ensemble.size();
555  MultidimArray<double> minCost;
556  MultidimArray<double> maxCost;
557  votes.initZeros(K);
558  minCost.initZeros(K);
559  minCost.initConstant(1);
560  maxCost.initZeros(K);
561  maxCost.initConstant(1);
562  double bestMinCost=0;
563  int bestClass=0;
564  MultidimArray<double> newFeaturesn;
565  for (int n=0; n<nmax; n++)
566  {
567  double costn;
568  newFeaturesn.initZeros(XSIZE(ensembleFeatures[n]));
569  FOR_ALL_ELEMENTS_IN_ARRAY1D(newFeaturesn)
570  newFeaturesn(i)=newFeatures(ensembleFeatures[n](i));
571  int k=ensemble[n]->doInference(newFeaturesn, costn, classesProbs, allCosts);
572  votes(k)++;
573  if (minCost(k)>0 || minCost(k)>costn)
574  minCost(k)=costn;
575  if (maxCost(k)>0 || maxCost(k)<costn)
576  maxCost(k)=costn;
577  if (minCost(k)<bestMinCost)
578  {
579  bestMinCost=minCost(k);
580  bestClass=k;
581  }
582  }
583  if (judgeCombination[bestClass]=='m')
584  cost=minCost(bestClass);
585  else if (judgeCombination[bestClass]=='M')
586  cost=maxCost(bestClass);
587  else
588  cost=minCost(bestClass);
589  return bestClass;
590 }
int * nmax
std::string judgeCombination
Judge combination.
Definition: naive_bayes.h:140
std::vector< NaiveBayes *> ensemble
Ensemble of classifiers.
Definition: naive_bayes.h:131
void initConstant(T val)
std::vector< MultidimArray< int > > ensembleFeatures
Ensemble of features for each classifier.
Definition: naive_bayes.h:134
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define XSIZE(v)
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
void initZeros(const MultidimArray< T1 > &op)
int * n
int K
Number of classes.
Definition: naive_bayes.h:137

◆ operator=()

EnsembleNaiveBayes & EnsembleNaiveBayes::operator= ( const EnsembleNaiveBayes other)

Assignment.

Definition at line 524 of file naive_bayes.cpp.

525 {
526  size_t imax=ensemble.size();
527  for (size_t i=0; i<imax; ++i)
528  delete ensemble[i];
529  ensemble.clear();
530  imax=other.ensemble.size();
531  for (size_t i=0; i<imax; ++i)
532  ensemble.emplace_back(new NaiveBayes(*(other.ensemble[i])));
533 
535  K=other.K;
537  return *this;
538 }
std::string judgeCombination
Judge combination.
Definition: naive_bayes.h:140
std::vector< NaiveBayes *> ensemble
Ensemble of classifiers.
Definition: naive_bayes.h:131
std::vector< MultidimArray< int > > ensembleFeatures
Ensemble of features for each classifier.
Definition: naive_bayes.h:134
#define i
int K
Number of classes.
Definition: naive_bayes.h:137

◆ setCostMatrix()

void EnsembleNaiveBayes::setCostMatrix ( const Matrix2D< double > &  cost)

Set cost matrix.

Definition at line 542 of file naive_bayes.cpp.

543 {
544  int nmax=ensemble.size();
545  for (int n=0; n<nmax; n++)
546  ensemble[n]->setCostMatrix(cost);
547 }
int * nmax
std::vector< NaiveBayes *> ensemble
Ensemble of classifiers.
Definition: naive_bayes.h:131
void setCostMatrix(const Matrix2D< double > &cost)
Set cost matrix.
int * n

Member Data Documentation

◆ ensemble

std::vector< NaiveBayes * > EnsembleNaiveBayes::ensemble

Ensemble of classifiers.

Definition at line 131 of file naive_bayes.h.

◆ ensembleFeatures

std::vector< MultidimArray<int> > EnsembleNaiveBayes::ensembleFeatures

Ensemble of features for each classifier.

Definition at line 134 of file naive_bayes.h.

◆ judgeCombination

std::string EnsembleNaiveBayes::judgeCombination

Judge combination.

Definition at line 140 of file naive_bayes.h.

◆ K

int EnsembleNaiveBayes::K

Number of classes.

Definition at line 137 of file naive_bayes.h.


The documentation for this class was generated from the following files: