mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
Merge pull request #18126 from danielenricocahall:add-oob-error-sample-weighting
Account for sample weights in calculating OOB Error * account for sample weights in oob error calculation * redefine oob error functions * fix ABI compatibility
This commit is contained in:
@@ -216,13 +216,14 @@ public:
|
||||
sample = Mat( nallvars, 1, CV_32F, psamples + sstep0*w->sidx[j], sstep1*sizeof(psamples[0]) );
|
||||
|
||||
double val = predictTrees(Range(treeidx, treeidx+1), sample, predictFlags);
|
||||
double sample_weight = w->sample_weights[w->sidx[j]];
|
||||
if( !_isClassifier )
|
||||
{
|
||||
oobres[j] += val;
|
||||
oobcount[j]++;
|
||||
double true_val = w->ord_responses[w->sidx[j]];
|
||||
double a = oobres[j]/oobcount[j] - true_val;
|
||||
oobError += a*a;
|
||||
oobError += sample_weight * a*a;
|
||||
val = (val - true_val)/max_response;
|
||||
ncorrect_responses += std::exp( -val*val );
|
||||
}
|
||||
@@ -237,7 +238,7 @@ public:
|
||||
if( votes[best_class] < votes[k] )
|
||||
best_class = k;
|
||||
int diff = best_class != w->cat_responses[w->sidx[j]];
|
||||
oobError += diff;
|
||||
oobError += sample_weight * diff;
|
||||
ncorrect_responses += diff == 0;
|
||||
}
|
||||
}
|
||||
@@ -421,6 +422,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
double getOOBError() const {
|
||||
return oobError;
|
||||
}
|
||||
|
||||
RTreeParams rparams;
|
||||
double oobError;
|
||||
vector<float> varImportance;
|
||||
@@ -505,6 +510,12 @@ public:
|
||||
const vector<Node>& getNodes() const CV_OVERRIDE { return impl.getNodes(); }
|
||||
const vector<Split>& getSplits() const CV_OVERRIDE { return impl.getSplits(); }
|
||||
const vector<int>& getSubsets() const CV_OVERRIDE { return impl.getSubsets(); }
|
||||
#if CV_VERSION_MAJOR == 3
|
||||
double getOOBError_() const { return impl.getOOBError(); }
|
||||
#else
|
||||
double getOOBError() const CV_OVERRIDE { return impl.getOOBError(); }
|
||||
#endif
|
||||
|
||||
|
||||
DTreesImplForRTrees impl;
|
||||
};
|
||||
@@ -532,6 +543,17 @@ void RTrees::getVotes(InputArray input, OutputArray output, int flags) const
|
||||
return this_->getVotes_(input, output, flags);
|
||||
}
|
||||
|
||||
#if CV_VERSION_MAJOR == 3
|
||||
double RTrees::getOOBError() const
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
const RTreesImpl* this_ = dynamic_cast<const RTreesImpl*>(this);
|
||||
if(!this_)
|
||||
CV_Error(Error::StsNotImplemented, "the class is not RTreesImpl");
|
||||
return this_->getOOBError_();
|
||||
}
|
||||
#endif
|
||||
|
||||
}}
|
||||
|
||||
// End of file.
|
||||
|
||||
Reference in New Issue
Block a user