25 bool ANBC_Model::train( UINT classLabel,
const MatrixFloat &trainingData,
const VectorFloat &weightsVector ){
28 if( trainingData.
getNumCols() != weightsVector.size() ){
35 this->classLabel = classLabel;
38 weights = weightsVector;
45 for(UINT j=0; j<N; j++){
48 for(UINT i=0; i<M; i++){
49 mu[j] += trainingData[i][j];
56 for(UINT j=0; j<N; j++){
59 for(UINT i=0; i<M; i++){
60 sigma[j] += grt_sqr( trainingData[i][j]-mu[j] );
62 sigma[j] = grt_sqrt( sigma[j]/Float(M-1) );
70 Float meanPrediction = 0.0;
73 for(UINT i=0; i<M; i++){
75 for(UINT j=0; j<N; j++) {
76 testData[j] = trainingData[i][j];
79 predictions[i] = predict( testData );
80 meanPrediction += predictions[i];
84 meanPrediction /= Float(M);
88 for(UINT i=0; i<M; i++) {
89 stdDev += grt_sqr( predictions[i]-meanPrediction );
91 stdDev = grt_sqrt( stdDev / (Float(M)-1.0) );
93 threshold = meanPrediction-(stdDev*gamma);
96 trainingMu = meanPrediction;
97 trainingSigma = stdDev;
103 Float prediction = 0.0;
104 for(UINT j=0; j<N; j++){
106 prediction += grt_log(gauss(x[j],mu[j],sigma[j]) * weights[j]);
111 Float ANBC_Model::predictUnnormed(
const VectorFloat &x ){
112 Float prediction = 0.0;
113 for(UINT j=0; j<N; j++){
115 prediction += grt_log( unnormedGauss(x[j], mu[j], sigma[j]) * weights[j] );
120 inline Float ANBC_Model::gauss(
const Float x,
const Float mu,
const Float sigma){
121 return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
124 inline Float ANBC_Model::unnormedGauss(
const Float x,
const Float mu,
const Float sigma){
125 return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
128 void ANBC_Model::recomputeThresholdValue(
const Float gamma){
130 threshold = trainingMu-(trainingSigma*gamma);
virtual bool resize(const unsigned int size)
This class implements a container for an ANBC model.
unsigned int getNumRows() const
unsigned int getNumCols() const