21 #define GRT_DLL_EXPORTS 26 bool ANBC_Model::train( UINT classLabel,
const MatrixFloat &trainingData,
const VectorFloat &weightsVector ){
29 if( trainingData.
getNumCols() != weightsVector.size() ){
36 this->classLabel = classLabel;
39 weights = weightsVector;
46 for(UINT j=0; j<N; j++){
49 for(UINT i=0; i<M; i++){
50 mu[j] += trainingData[i][j];
57 for(UINT j=0; j<N; j++){
60 for(UINT i=0; i<M; i++){
61 sigma[j] += grt_sqr( trainingData[i][j]-mu[j] );
63 sigma[j] = grt_sqrt( sigma[j]/Float(M-1) );
71 Float meanPrediction = 0.0;
74 for(UINT i=0; i<M; i++){
76 for(UINT j=0; j<N; j++) {
77 testData[j] = trainingData[i][j];
80 predictions[i] = predict( testData );
81 meanPrediction += predictions[i];
85 meanPrediction /= Float(M);
89 for(UINT i=0; i<M; i++) {
90 stdDev += grt_sqr( predictions[i]-meanPrediction );
92 stdDev = grt_sqrt( stdDev / (Float(M)-1.0) );
94 threshold = meanPrediction-(stdDev*gamma);
97 trainingMu = meanPrediction;
98 trainingSigma = stdDev;
104 Float prediction = 0.0;
105 for(UINT j=0; j<N; j++){
107 prediction += grt_log(gauss(x[j],mu[j],sigma[j]) * weights[j]);
112 Float ANBC_Model::predictUnnormed(
const VectorFloat &x ){
113 Float prediction = 0.0;
114 for(UINT j=0; j<N; j++){
116 prediction += grt_log( unnormedGauss(x[j], mu[j], sigma[j]) * weights[j] );
121 inline Float ANBC_Model::gauss(
const Float x,
const Float mu,
const Float sigma){
122 return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
125 inline Float ANBC_Model::unnormedGauss(
const Float x,
const Float mu,
const Float sigma){
126 return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
129 void ANBC_Model::recomputeThresholdValue(
const Float gamma){
131 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