2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
2014-07-30 03:54:23 +08:00
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
2010-05-12 01:44:00 +08:00
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
2014-07-30 03:54:23 +08:00
|
|
|
// Copyright (C) 2014, Itseez Inc, all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
2014-07-30 03:54:23 +08:00
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
2010-05-12 01:44:00 +08:00
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
namespace cv {
|
|
|
|
namespace ml {
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
using std::vector;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
TreeParams::TreeParams()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
maxDepth = INT_MAX;
|
|
|
|
minSampleCount = 10;
|
|
|
|
regressionAccuracy = 0.01f;
|
|
|
|
useSurrogates = false;
|
|
|
|
maxCategories = 10;
|
|
|
|
CVFolds = 10;
|
|
|
|
use1SERule = true;
|
|
|
|
truncatePrunedTree = true;
|
|
|
|
priors = Mat();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
TreeParams::TreeParams(int _maxDepth, int _minSampleCount,
|
|
|
|
double _regressionAccuracy, bool _useSurrogates,
|
|
|
|
int _maxCategories, int _CVFolds,
|
|
|
|
bool _use1SERule, bool _truncatePrunedTree,
|
|
|
|
const Mat& _priors)
|
2013-03-28 20:12:13 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
maxDepth = _maxDepth;
|
|
|
|
minSampleCount = _minSampleCount;
|
|
|
|
regressionAccuracy = (float)_regressionAccuracy;
|
|
|
|
useSurrogates = _useSurrogates;
|
|
|
|
maxCategories = _maxCategories;
|
|
|
|
CVFolds = _CVFolds;
|
|
|
|
use1SERule = _use1SERule;
|
|
|
|
truncatePrunedTree = _truncatePrunedTree;
|
|
|
|
priors = _priors;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
DTrees::Node::Node()
|
2013-03-28 20:12:13 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
classIdx = 0;
|
|
|
|
value = 0;
|
|
|
|
parent = left = right = split = defaultDir = -1;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
DTrees::Split::Split()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
varIdx = 0;
|
|
|
|
inversed = false;
|
|
|
|
quality = 0.f;
|
|
|
|
next = -1;
|
|
|
|
c = 0.f;
|
|
|
|
subsetOfs = 0;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
DTreesImpl::WorkData::WorkData(const Ptr<TrainData>& _data)
|
|
|
|
{
|
|
|
|
data = _data;
|
|
|
|
vector<int> subsampleIdx;
|
|
|
|
Mat sidx0 = _data->getTrainSampleIdx();
|
|
|
|
if( !sidx0.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
sidx0.copyTo(sidx);
|
|
|
|
std::sort(sidx.begin(), sidx.end());
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int n = _data->getNSamples();
|
|
|
|
setRangeVector(sidx, n);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
maxSubsetSize = 0;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2017-06-26 19:09:21 +08:00
|
|
|
DTreesImpl::DTreesImpl() : _isClassifier(false) {}
|
2014-07-30 03:54:23 +08:00
|
|
|
DTreesImpl::~DTreesImpl() {}
|
|
|
|
void DTreesImpl::clear()
|
|
|
|
{
|
|
|
|
varIdx.clear();
|
|
|
|
compVarIdx.clear();
|
|
|
|
varType.clear();
|
|
|
|
catOfs.clear();
|
|
|
|
catMap.clear();
|
|
|
|
roots.clear();
|
|
|
|
nodes.clear();
|
|
|
|
splits.clear();
|
|
|
|
subsets.clear();
|
|
|
|
classLabels.clear();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w.release();
|
|
|
|
_isClassifier = false;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::startTraining( const Ptr<TrainData>& data, int )
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
w = makePtr<WorkData>(data);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat vtype = data->getVarType();
|
|
|
|
vtype.copyTo(varType);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
data->getCatOfs().copyTo(catOfs);
|
|
|
|
data->getCatMap().copyTo(catMap);
|
|
|
|
data->getDefaultSubstValues().copyTo(missingSubst);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int nallvars = data->getNAllVars();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat vidx0 = data->getVarIdx();
|
|
|
|
if( !vidx0.empty() )
|
|
|
|
vidx0.copyTo(varIdx);
|
|
|
|
else
|
|
|
|
setRangeVector(varIdx, nallvars);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
initCompVarIdx();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w->maxSubsetSize = 0;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, nvars = (int)varIdx.size();
|
|
|
|
for( i = 0; i < nvars; i++ )
|
|
|
|
w->maxSubsetSize = std::max(w->maxSubsetSize, getCatCount(varIdx[i]));
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w->maxSubsetSize = std::max((w->maxSubsetSize + 31)/32, 1);
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
data->getSampleWeights().copyTo(w->sample_weights);
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
_isClassifier = data->getResponseType() == VAR_CATEGORICAL;
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _isClassifier )
|
2013-01-30 01:11:52 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
data->getNormCatResponses().copyTo(w->cat_responses);
|
|
|
|
data->getClassLabels().copyTo(classLabels);
|
|
|
|
int nclasses = (int)classLabels.size();
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat class_weights = params.priors;
|
|
|
|
if( !class_weights.empty() )
|
|
|
|
{
|
|
|
|
if( class_weights.type() != CV_64F || !class_weights.isContinuous() )
|
|
|
|
{
|
|
|
|
Mat temp;
|
|
|
|
class_weights.convertTo(temp, CV_64F);
|
|
|
|
class_weights = temp;
|
|
|
|
}
|
|
|
|
CV_Assert( class_weights.checkVector(1, CV_64F) == nclasses );
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int nsamples = (int)w->cat_responses.size();
|
|
|
|
const double* cw = class_weights.ptr<double>();
|
|
|
|
CV_Assert( (int)w->sample_weights.size() == nsamples );
|
2013-01-30 01:11:52 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < nsamples; i++ )
|
|
|
|
{
|
|
|
|
int ci = w->cat_responses[i];
|
|
|
|
CV_Assert( 0 <= ci && ci < nclasses );
|
|
|
|
w->sample_weights[i] *= cw[ci];
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
2014-07-30 03:54:23 +08:00
|
|
|
data->getResponses().copyTo(w->ord_responses);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DTreesImpl::initCompVarIdx()
|
|
|
|
{
|
|
|
|
int nallvars = (int)varType.size();
|
|
|
|
compVarIdx.assign(nallvars, -1);
|
|
|
|
int i, nvars = (int)varIdx.size(), prevIdx = -1;
|
|
|
|
for( i = 0; i < nvars; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int vi = varIdx[i];
|
|
|
|
CV_Assert( 0 <= vi && vi < nallvars && vi > prevIdx );
|
|
|
|
prevIdx = vi;
|
|
|
|
compVarIdx[vi] = i;
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DTreesImpl::endTraining()
|
|
|
|
{
|
|
|
|
w.release();
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
bool DTreesImpl::train( const Ptr<TrainData>& trainData, int flags )
|
|
|
|
{
|
|
|
|
startTraining(trainData, flags);
|
|
|
|
bool ok = addTree( w->sidx ) >= 0;
|
|
|
|
w.release();
|
|
|
|
endTraining();
|
|
|
|
return ok;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
const vector<int>& DTreesImpl::getActiveVars()
|
|
|
|
{
|
|
|
|
return varIdx;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::addTree(const vector<int>& sidx )
|
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
size_t n = (params.getMaxDepth() > 0 ? (1 << params.getMaxDepth()) : 1024) + w->wnodes.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w->wnodes.reserve(n);
|
|
|
|
w->wsplits.reserve(n);
|
|
|
|
w->wsubsets.reserve(n*w->maxSubsetSize);
|
|
|
|
w->wnodes.clear();
|
|
|
|
w->wsplits.clear();
|
|
|
|
w->wsubsets.clear();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
int cv_n = params.getCVFolds();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( cv_n > 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
w->cv_Tn.resize(n*cv_n);
|
|
|
|
w->cv_node_error.resize(n*cv_n);
|
|
|
|
w->cv_node_risk.resize(n*cv_n);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// build the tree recursively
|
|
|
|
int w_root = addNodeAndTrySplit(-1, sidx);
|
|
|
|
int maxdepth = INT_MAX;//pruneCV(root);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int w_nidx = w_root, pidx = -1, depth = 0;
|
|
|
|
int root = (int)nodes.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for(;;)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const WNode& wnode = w->wnodes[w_nidx];
|
|
|
|
Node node;
|
|
|
|
node.parent = pidx;
|
|
|
|
node.classIdx = wnode.class_idx;
|
|
|
|
node.value = wnode.value;
|
|
|
|
node.defaultDir = wnode.defaultDir;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int wsplit_idx = wnode.split;
|
|
|
|
if( wsplit_idx >= 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const WSplit& wsplit = w->wsplits[wsplit_idx];
|
|
|
|
Split split;
|
|
|
|
split.c = wsplit.c;
|
|
|
|
split.quality = wsplit.quality;
|
|
|
|
split.inversed = wsplit.inversed;
|
|
|
|
split.varIdx = wsplit.varIdx;
|
|
|
|
split.subsetOfs = -1;
|
|
|
|
if( wsplit.subsetOfs >= 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int ssize = getSubsetSize(split.varIdx);
|
|
|
|
split.subsetOfs = (int)subsets.size();
|
|
|
|
subsets.resize(split.subsetOfs + ssize);
|
2015-03-20 15:10:32 +08:00
|
|
|
// This check verifies that subsets index is in the correct range
|
|
|
|
// as in case ssize == 0 no real resize performed.
|
|
|
|
// Thus memory kept safe.
|
|
|
|
// Also this skips useless memcpy call when size parameter is zero
|
|
|
|
if(ssize > 0)
|
|
|
|
{
|
|
|
|
memcpy(&subsets[split.subsetOfs], &w->wsubsets[wsplit.subsetOfs], ssize*sizeof(int));
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
node.split = (int)splits.size();
|
|
|
|
splits.push_back(split);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
int nidx = (int)nodes.size();
|
|
|
|
nodes.push_back(node);
|
|
|
|
if( pidx >= 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int w_pidx = w->wnodes[w_nidx].parent;
|
|
|
|
if( w->wnodes[w_pidx].left == w_nidx )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
nodes[pidx].left = nidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Assert(w->wnodes[w_pidx].right == w_nidx);
|
|
|
|
nodes[pidx].right = nidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( wnode.left >= 0 && depth+1 < maxdepth )
|
|
|
|
{
|
|
|
|
w_nidx = wnode.left;
|
|
|
|
pidx = nidx;
|
|
|
|
depth++;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int w_pidx = wnode.parent;
|
|
|
|
while( w_pidx >= 0 && w->wnodes[w_pidx].right == w_nidx )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
w_nidx = w_pidx;
|
|
|
|
w_pidx = w->wnodes[w_pidx].parent;
|
|
|
|
nidx = pidx;
|
|
|
|
pidx = nodes[pidx].parent;
|
|
|
|
depth--;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( w_pidx < 0 )
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w_nidx = w->wnodes[w_pidx].right;
|
2014-08-03 17:15:43 +08:00
|
|
|
CV_Assert( w_nidx >= 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
roots.push_back(root);
|
|
|
|
return root;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
void DTreesImpl::setDParams(const TreeParams& _params)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
params = _params;
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::addNodeAndTrySplit( int parent, const vector<int>& sidx )
|
|
|
|
{
|
|
|
|
w->wnodes.push_back(WNode());
|
|
|
|
int nidx = (int)(w->wnodes.size() - 1);
|
|
|
|
WNode& node = w->wnodes.back();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
node.parent = parent;
|
|
|
|
node.depth = parent >= 0 ? w->wnodes[parent].depth + 1 : 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
int nfolds = params.getCVFolds();
|
2010-11-29 17:34:06 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( nfolds > 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
w->cv_Tn.resize((nidx+1)*nfolds);
|
|
|
|
w->cv_node_error.resize((nidx+1)*nfolds);
|
|
|
|
w->cv_node_risk.resize((nidx+1)*nfolds);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, n = node.sample_count = (int)sidx.size();
|
|
|
|
bool can_split = true;
|
|
|
|
vector<int> sleft, sright;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
calcValue( nidx, sidx );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
if( n <= params.getMinSampleCount() || node.depth >= params.getMaxDepth() )
|
2014-07-30 03:54:23 +08:00
|
|
|
can_split = false;
|
|
|
|
else if( _isClassifier )
|
|
|
|
{
|
|
|
|
const int* responses = &w->cat_responses[0];
|
|
|
|
const int* s = &sidx[0];
|
|
|
|
int first = responses[s[0]];
|
|
|
|
for( i = 1; i < n; i++ )
|
|
|
|
if( responses[s[i]] != first )
|
|
|
|
break;
|
|
|
|
if( i == n )
|
|
|
|
can_split = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
if( sqrt(node.node_risk) < params.getRegressionAccuracy() )
|
2014-07-30 03:54:23 +08:00
|
|
|
can_split = false;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( can_split )
|
|
|
|
node.split = findBestSplit( sidx );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
//printf("depth=%d, nidx=%d, parent=%d, n=%d, %s, value=%.1f, risk=%.1f\n", node.depth, nidx, node.parent, n, (node.split < 0 ? "leaf" : varType[w->wsplits[node.split].varIdx] == VAR_CATEGORICAL ? "cat" : "ord"), node.value, node.node_risk);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( node.split >= 0 )
|
|
|
|
{
|
|
|
|
node.defaultDir = calcDir( node.split, sidx, sleft, sright );
|
|
|
|
if( params.useSurrogates )
|
|
|
|
CV_Error( CV_StsNotImplemented, "surrogate splits are not implemented yet");
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-08-04 05:16:45 +08:00
|
|
|
int left = addNodeAndTrySplit( nidx, sleft );
|
|
|
|
int right = addNodeAndTrySplit( nidx, sright );
|
|
|
|
w->wnodes[nidx].left = left;
|
|
|
|
w->wnodes[nidx].right = right;
|
2014-08-04 01:21:11 +08:00
|
|
|
CV_Assert( w->wnodes[nidx].left > 0 && w->wnodes[nidx].right > 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return nidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::findBestSplit( const vector<int>& _sidx )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const vector<int>& activeVars = getActiveVars();
|
|
|
|
int splitidx = -1;
|
|
|
|
int vi_, nv = (int)activeVars.size();
|
|
|
|
AutoBuffer<int> buf(w->maxSubsetSize*2);
|
2018-06-11 06:42:00 +08:00
|
|
|
int *subset = buf.data(), *best_subset = subset + w->maxSubsetSize;
|
2014-07-30 03:54:23 +08:00
|
|
|
WSplit split, best_split;
|
|
|
|
best_split.quality = 0.;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( vi_ = 0; vi_ < nv; vi_++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int vi = activeVars[vi_];
|
|
|
|
if( varType[vi] == VAR_CATEGORICAL )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _isClassifier )
|
|
|
|
split = findSplitCatClass(vi, _sidx, 0, subset);
|
|
|
|
else
|
|
|
|
split = findSplitCatReg(vi, _sidx, 0, subset);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _isClassifier )
|
|
|
|
split = findSplitOrdClass(vi, _sidx, 0);
|
|
|
|
else
|
|
|
|
split = findSplitOrdReg(vi, _sidx, 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
if( split.quality > best_split.quality )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
best_split = split;
|
|
|
|
std::swap(subset, best_subset);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( best_split.quality > 0 )
|
|
|
|
{
|
|
|
|
int best_vi = best_split.varIdx;
|
|
|
|
CV_Assert( compVarIdx[best_split.varIdx] >= 0 && best_vi >= 0 );
|
|
|
|
int i, prevsz = (int)w->wsubsets.size(), ssize = getSubsetSize(best_vi);
|
|
|
|
w->wsubsets.resize(prevsz + ssize);
|
|
|
|
for( i = 0; i < ssize; i++ )
|
|
|
|
w->wsubsets[prevsz + i] = best_subset[i];
|
|
|
|
best_split.subsetOfs = prevsz;
|
|
|
|
w->wsplits.push_back(best_split);
|
|
|
|
splitidx = (int)(w->wsplits.size()-1);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return splitidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::calcValue( int nidx, const vector<int>& _sidx )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
WNode* node = &w->wnodes[nidx];
|
2015-02-11 18:24:14 +08:00
|
|
|
int i, j, k, n = (int)_sidx.size(), cv_n = params.getCVFolds();
|
2014-07-30 03:54:23 +08:00
|
|
|
int m = (int)classLabels.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
cv::AutoBuffer<double> buf(std::max(m, 3)*(cv_n+1));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( cv_n > 0 )
|
|
|
|
{
|
|
|
|
size_t sz = w->cv_Tn.size();
|
|
|
|
w->cv_Tn.resize(sz + cv_n);
|
|
|
|
w->cv_node_risk.resize(sz + cv_n);
|
|
|
|
w->cv_node_error.resize(sz + cv_n);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _isClassifier )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
// in case of classification tree:
|
|
|
|
// * node value is the label of the class that has the largest weight in the node.
|
|
|
|
// * node risk is the weighted number of misclassified samples,
|
|
|
|
// * j-th cross-validation fold value and risk are calculated as above,
|
|
|
|
// but using the samples with cv_labels(*)!=j.
|
|
|
|
// * j-th cross-validation fold error is calculated as the weighted number of
|
|
|
|
// misclassified samples with cv_labels(*)==j.
|
|
|
|
|
|
|
|
// compute the number of instances of each class
|
2018-06-11 06:42:00 +08:00
|
|
|
double* cls_count = buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
double* cv_cls_count = cls_count + m;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
double max_val = -1, total_weight = 0;
|
|
|
|
int max_k = -1;
|
|
|
|
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
cls_count[k] = 0;
|
|
|
|
|
|
|
|
if( cv_n == 0 )
|
|
|
|
{
|
|
|
|
for( i = 0; i < n; i++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
{
|
|
|
|
int si = _sidx[i];
|
|
|
|
cls_count[w->cat_responses[si]] += w->sample_weights[si];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
cv_cls_count[j*m + k] = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int si = _sidx[i];
|
|
|
|
j = w->cv_labels[si]; k = w->cat_responses[si];
|
|
|
|
cv_cls_count[j*m + k] += w->sample_weights[si];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
cls_count[k] += cv_cls_count[j*m + k];
|
|
|
|
}
|
|
|
|
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double val = cls_count[k];
|
2010-05-12 01:44:00 +08:00
|
|
|
total_weight += val;
|
|
|
|
if( max_val < val )
|
|
|
|
{
|
|
|
|
max_val = val;
|
|
|
|
max_k = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node->class_idx = max_k;
|
2014-07-30 03:54:23 +08:00
|
|
|
node->value = classLabels[max_k];
|
2010-05-12 01:44:00 +08:00
|
|
|
node->node_risk = total_weight - max_val;
|
|
|
|
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
{
|
|
|
|
double sum_k = 0, sum = 0, max_val_k = 0;
|
|
|
|
max_val = -1; max_k = -1;
|
|
|
|
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double val_k = cv_cls_count[j*m + k];
|
|
|
|
double val = cls_count[k] - val_k;
|
2010-05-12 01:44:00 +08:00
|
|
|
sum_k += val_k;
|
|
|
|
sum += val;
|
|
|
|
if( max_val < val )
|
|
|
|
{
|
|
|
|
max_val = val;
|
|
|
|
max_val_k = val_k;
|
|
|
|
max_k = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w->cv_Tn[nidx*cv_n + j] = INT_MAX;
|
|
|
|
w->cv_node_risk[nidx*cv_n + j] = sum - max_val;
|
|
|
|
w->cv_node_error[nidx*cv_n + j] = sum_k - max_val_k;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// in case of regression tree:
|
|
|
|
// * node value is 1/n*sum_i(Y_i), where Y_i is i-th response,
|
|
|
|
// n is the number of samples in the node.
|
|
|
|
// * node risk is the sum of squared errors: sum_i((Y_i - <node_value>)^2)
|
|
|
|
// * j-th cross-validation fold value and risk are calculated as above,
|
|
|
|
// but using the samples with cv_labels(*)!=j.
|
|
|
|
// * j-th cross-validation fold error is calculated
|
|
|
|
// using samples with cv_labels(*)==j as the test subset:
|
|
|
|
// error_j = sum_(i,cv_labels(i)==j)((Y_i - <node_value_j>)^2),
|
|
|
|
// where node_value_j is the node value calculated
|
|
|
|
// as described in the previous bullet, and summation is done
|
|
|
|
// over the samples with cv_labels(*)==j.
|
2014-07-30 03:54:23 +08:00
|
|
|
double sum = 0, sum2 = 0, sumw = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( cv_n == 0 )
|
|
|
|
{
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int si = _sidx[i];
|
|
|
|
double wval = w->sample_weights[si];
|
|
|
|
double t = w->ord_responses[si];
|
|
|
|
sum += t*wval;
|
|
|
|
sum2 += t*t*wval;
|
|
|
|
sumw += wval;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-11 06:42:00 +08:00
|
|
|
double *cv_sum = buf.data(), *cv_sum2 = cv_sum + cv_n;
|
2014-07-30 03:54:23 +08:00
|
|
|
double* cv_count = (double*)(cv_sum2 + cv_n);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
{
|
|
|
|
cv_sum[j] = cv_sum2[j] = 0.;
|
|
|
|
cv_count[j] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int si = _sidx[i];
|
|
|
|
j = w->cv_labels[si];
|
|
|
|
double wval = w->sample_weights[si];
|
|
|
|
double t = w->ord_responses[si];
|
|
|
|
cv_sum[j] += t*wval;
|
|
|
|
cv_sum2[j] += t*t*wval;
|
|
|
|
cv_count[j] += wval;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
{
|
|
|
|
sum += cv_sum[j];
|
|
|
|
sum2 += cv_sum2[j];
|
2014-07-30 03:54:23 +08:00
|
|
|
sumw += cv_count[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
{
|
|
|
|
double s = sum - cv_sum[j], si = sum - s;
|
|
|
|
double s2 = sum2 - cv_sum2[j], s2i = sum2 - s2;
|
|
|
|
double c = cv_count[j], ci = sumw - c;
|
|
|
|
double r = si/std::max(ci, DBL_EPSILON);
|
|
|
|
w->cv_node_risk[nidx*cv_n + j] = s2i - r*r*ci;
|
|
|
|
w->cv_node_error[nidx*cv_n + j] = s2 - 2*r*s + c*r*r;
|
|
|
|
w->cv_Tn[nidx*cv_n + j] = INT_MAX;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
node->node_risk = sum2 - (sum/sumw)*sum;
|
2018-03-27 21:39:36 +08:00
|
|
|
node->node_risk /= sumw;
|
2014-07-30 03:54:23 +08:00
|
|
|
node->value = sum/sumw;
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
DTreesImpl::WSplit DTreesImpl::findSplitOrdClass( int vi, const vector<int>& _sidx, double initQuality )
|
|
|
|
{
|
|
|
|
int n = (int)_sidx.size();
|
|
|
|
int m = (int)classLabels.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
cv::AutoBuffer<uchar> buf(n*(sizeof(float) + sizeof(int)) + m*2*sizeof(double));
|
|
|
|
const int* sidx = &_sidx[0];
|
|
|
|
const int* responses = &w->cat_responses[0];
|
|
|
|
const double* weights = &w->sample_weights[0];
|
2018-06-11 06:42:00 +08:00
|
|
|
double* lcw = (double*)buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
double* rcw = lcw + m;
|
|
|
|
float* values = (float*)(rcw + m);
|
|
|
|
int* sorted_idx = (int*)(values + n);
|
|
|
|
int i, best_i = -1;
|
|
|
|
double best_val = initQuality;
|
|
|
|
|
|
|
|
for( i = 0; i < m; i++ )
|
|
|
|
lcw[i] = rcw[i] = 0.;
|
|
|
|
|
|
|
|
w->data->getValues( vi, _sidx, values );
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
sorted_idx[i] = i;
|
|
|
|
int si = sidx[i];
|
|
|
|
rcw[responses[si]] += weights[si];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(sorted_idx, sorted_idx + n, cmp_lt_idx<float>(values));
|
|
|
|
|
|
|
|
double L = 0, R = 0, lsum2 = 0, rsum2 = 0;
|
|
|
|
for( i = 0; i < m; i++ )
|
|
|
|
{
|
|
|
|
double wval = rcw[i];
|
|
|
|
R += wval;
|
|
|
|
rsum2 += wval*wval;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < n - 1; i++ )
|
|
|
|
{
|
|
|
|
int curr = sorted_idx[i];
|
|
|
|
int next = sorted_idx[i+1];
|
|
|
|
int si = sidx[curr];
|
|
|
|
double wval = weights[si], w2 = wval*wval;
|
|
|
|
L += wval; R -= wval;
|
|
|
|
int idx = responses[si];
|
|
|
|
double lv = lcw[idx], rv = rcw[idx];
|
|
|
|
lsum2 += 2*lv*wval + w2;
|
|
|
|
rsum2 -= 2*rv*wval - w2;
|
|
|
|
lcw[idx] = lv + wval; rcw[idx] = rv - wval;
|
|
|
|
|
2016-03-22 08:33:36 +08:00
|
|
|
float value_between = (values[next] + values[curr]) * 0.5f;
|
|
|
|
if( value_between > values[curr] && value_between < values[next] )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double val = (lsum2*R + rsum2*L)/(L*R);
|
|
|
|
if( best_val < val )
|
|
|
|
{
|
|
|
|
best_val = val;
|
|
|
|
best_i = i;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
WSplit split;
|
|
|
|
if( best_i >= 0 )
|
|
|
|
{
|
|
|
|
split.varIdx = vi;
|
|
|
|
split.c = (values[sorted_idx[best_i]] + values[sorted_idx[best_i+1]])*0.5f;
|
2014-08-03 16:46:28 +08:00
|
|
|
split.inversed = false;
|
2014-07-30 03:54:23 +08:00
|
|
|
split.quality = (float)best_val;
|
|
|
|
}
|
|
|
|
return split;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// simple k-means, slightly modified to take into account the "weight" (L1-norm) of each vector.
|
|
|
|
void DTreesImpl::clusterCategories( const double* vectors, int n, int m, double* csums, int k, int* labels )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int iters = 0, max_iters = 100;
|
|
|
|
int i, j, idx;
|
|
|
|
cv::AutoBuffer<double> buf(n + k);
|
2018-06-11 06:42:00 +08:00
|
|
|
double *v_weights = buf.data(), *c_weights = buf.data() + n;
|
2014-07-30 03:54:23 +08:00
|
|
|
bool modified = true;
|
2014-08-03 16:46:28 +08:00
|
|
|
RNG r((uint64)-1);
|
2014-07-30 03:54:23 +08:00
|
|
|
|
|
|
|
// assign labels randomly
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
2014-08-03 16:46:28 +08:00
|
|
|
double sum = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
const double* v = vectors + i*m;
|
|
|
|
labels[i] = i < k ? i : r.uniform(0, k);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// compute weight of each vector
|
|
|
|
for( j = 0; j < m; j++ )
|
|
|
|
sum += v[j];
|
|
|
|
v_weights[i] = sum ? 1./sum : 0.;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
int i1 = r.uniform(0, n);
|
|
|
|
int i2 = r.uniform(0, n);
|
|
|
|
std::swap( labels[i1], labels[i2] );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( iters = 0; iters <= max_iters; iters++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
// calculate csums
|
|
|
|
for( i = 0; i < k; i++ )
|
|
|
|
{
|
|
|
|
for( j = 0; j < m; j++ )
|
|
|
|
csums[i*m + j] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const double* v = vectors + i*m;
|
|
|
|
double* s = csums + labels[i]*m;
|
|
|
|
for( j = 0; j < m; j++ )
|
|
|
|
s[j] += v[j];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// exit the loop here, when we have up-to-date csums
|
|
|
|
if( iters == max_iters || !modified )
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
modified = false;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// calculate weight of each cluster
|
|
|
|
for( i = 0; i < k; i++ )
|
|
|
|
{
|
|
|
|
const double* s = csums + i*m;
|
|
|
|
double sum = 0;
|
|
|
|
for( j = 0; j < m; j++ )
|
|
|
|
sum += s[j];
|
|
|
|
c_weights[i] = sum ? 1./sum : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now for each vector determine the closest cluster
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
const double* v = vectors + i*m;
|
|
|
|
double alpha = v_weights[i];
|
|
|
|
double min_dist2 = DBL_MAX;
|
|
|
|
int min_idx = -1;
|
|
|
|
|
|
|
|
for( idx = 0; idx < k; idx++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const double* s = csums + idx*m;
|
|
|
|
double dist2 = 0., beta = c_weights[idx];
|
|
|
|
for( j = 0; j < m; j++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double t = v[j]*alpha - s[j]*beta;
|
|
|
|
dist2 += t*t;
|
|
|
|
}
|
|
|
|
if( min_dist2 > dist2 )
|
|
|
|
{
|
|
|
|
min_dist2 = dist2;
|
|
|
|
min_idx = idx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( min_idx != labels[i] )
|
|
|
|
modified = true;
|
|
|
|
labels[i] = min_idx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
DTreesImpl::WSplit DTreesImpl::findSplitCatClass( int vi, const vector<int>& _sidx,
|
|
|
|
double initQuality, int* subset )
|
|
|
|
{
|
|
|
|
int _mi = getCatCount(vi), mi = _mi;
|
|
|
|
int n = (int)_sidx.size();
|
|
|
|
int m = (int)classLabels.size();
|
|
|
|
|
|
|
|
int base_size = m*(3 + mi) + mi + 1;
|
2015-02-11 18:24:14 +08:00
|
|
|
if( m > 2 && mi > params.getMaxCategories() )
|
|
|
|
base_size += m*std::min(params.getMaxCategories(), n) + mi;
|
2014-07-30 03:54:23 +08:00
|
|
|
else
|
|
|
|
base_size += mi;
|
|
|
|
AutoBuffer<double> buf(base_size + n);
|
|
|
|
|
2018-06-11 06:42:00 +08:00
|
|
|
double* lc = buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
double* rc = lc + m;
|
|
|
|
double* _cjk = rc + m*2, *cjk = _cjk;
|
|
|
|
double* c_weights = cjk + m*mi;
|
|
|
|
|
2018-06-11 06:42:00 +08:00
|
|
|
int* labels = (int*)(buf.data() + base_size);
|
2014-07-30 03:54:23 +08:00
|
|
|
w->data->getNormCatValues(vi, _sidx, labels);
|
|
|
|
const int* responses = &w->cat_responses[0];
|
|
|
|
const double* weights = &w->sample_weights[0];
|
|
|
|
|
|
|
|
int* cluster_labels = 0;
|
|
|
|
double** dbl_ptr = 0;
|
|
|
|
int i, j, k, si, idx;
|
|
|
|
double L = 0, R = 0;
|
|
|
|
double best_val = initQuality;
|
|
|
|
int prevcode = 0, best_subset = -1, subset_i, subset_n, subtract = 0;
|
|
|
|
|
|
|
|
// init array of counters:
|
|
|
|
// c_{jk} - number of samples that have vi-th input variable = j and response = k.
|
|
|
|
for( j = -1; j < mi; j++ )
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
cjk[j*m + k] = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
si = _sidx[i];
|
|
|
|
j = labels[i];
|
|
|
|
k = responses[si];
|
|
|
|
cjk[j*m + k] += weights[si];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( m > 2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
if( mi > params.getMaxCategories() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
mi = std::min(params.getMaxCategories(), n);
|
2014-07-30 03:54:23 +08:00
|
|
|
cjk = c_weights + _mi;
|
|
|
|
cluster_labels = (int*)(cjk + m*mi);
|
|
|
|
clusterCategories( _cjk, _mi, m, cjk, mi, cluster_labels );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
subset_i = 1;
|
|
|
|
subset_n = 1 << mi;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
assert( m == 2 );
|
|
|
|
dbl_ptr = (double**)(c_weights + _mi);
|
|
|
|
for( j = 0; j < mi; j++ )
|
|
|
|
dbl_ptr[j] = cjk + j*2 + 1;
|
|
|
|
std::sort(dbl_ptr, dbl_ptr + mi, cmp_lt_ptr<double>());
|
|
|
|
subset_i = 0;
|
|
|
|
subset_n = mi;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
{
|
|
|
|
double sum = 0;
|
|
|
|
for( j = 0; j < mi; j++ )
|
|
|
|
sum += cjk[j*m + k];
|
|
|
|
CV_Assert(sum > 0);
|
|
|
|
rc[k] = sum;
|
|
|
|
lc[k] = 0;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( j = 0; j < mi; j++ )
|
|
|
|
{
|
|
|
|
double sum = 0;
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
sum += cjk[j*m + k];
|
|
|
|
c_weights[j] = sum;
|
|
|
|
R += c_weights[j];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( ; subset_i < subset_n; subset_i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double lsum2 = 0, rsum2 = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( m == 2 )
|
|
|
|
idx = (int)(dbl_ptr[subset_i] - cjk)/2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int graycode = (subset_i>>1)^subset_i;
|
|
|
|
int diff = graycode ^ prevcode;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// determine index of the changed bit.
|
|
|
|
Cv32suf u;
|
|
|
|
idx = diff >= (1 << 16) ? 16 : 0;
|
|
|
|
u.f = (float)(((diff >> 16) | diff) & 65535);
|
|
|
|
idx += (u.i >> 23) - 127;
|
|
|
|
subtract = graycode < prevcode;
|
|
|
|
prevcode = graycode;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
double* crow = cjk + idx*m;
|
|
|
|
double weight = c_weights[idx];
|
|
|
|
if( weight < FLT_EPSILON )
|
|
|
|
continue;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( !subtract )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
for( k = 0; k < m; k++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double t = crow[k];
|
|
|
|
double lval = lc[k] + t;
|
|
|
|
double rval = rc[k] - t;
|
|
|
|
lsum2 += lval*lval;
|
|
|
|
rsum2 += rval*rval;
|
|
|
|
lc[k] = lval; rc[k] = rval;
|
|
|
|
}
|
|
|
|
L += weight;
|
|
|
|
R -= weight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( k = 0; k < m; k++ )
|
|
|
|
{
|
|
|
|
double t = crow[k];
|
|
|
|
double lval = lc[k] - t;
|
|
|
|
double rval = rc[k] + t;
|
|
|
|
lsum2 += lval*lval;
|
|
|
|
rsum2 += rval*rval;
|
|
|
|
lc[k] = lval; rc[k] = rval;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
L -= weight;
|
|
|
|
R += weight;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( L > FLT_EPSILON && R > FLT_EPSILON )
|
|
|
|
{
|
|
|
|
double val = (lsum2*R + rsum2*L)/(L*R);
|
|
|
|
if( best_val < val )
|
|
|
|
{
|
|
|
|
best_val = val;
|
|
|
|
best_subset = subset_i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
WSplit split;
|
|
|
|
if( best_subset >= 0 )
|
|
|
|
{
|
|
|
|
split.varIdx = vi;
|
|
|
|
split.quality = (float)best_val;
|
|
|
|
memset( subset, 0, getSubsetSize(vi) * sizeof(int) );
|
|
|
|
if( m == 2 )
|
|
|
|
{
|
|
|
|
for( i = 0; i <= best_subset; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
idx = (int)(dbl_ptr[i] - cjk) >> 1;
|
|
|
|
subset[idx >> 5] |= 1 << (idx & 31);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < _mi; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
idx = cluster_labels ? cluster_labels[i] : i;
|
|
|
|
if( best_subset & (1 << idx) )
|
|
|
|
subset[i >> 5] |= 1 << (i & 31);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return split;
|
|
|
|
}
|
|
|
|
|
|
|
|
DTreesImpl::WSplit DTreesImpl::findSplitOrdReg( int vi, const vector<int>& _sidx, double initQuality )
|
|
|
|
{
|
|
|
|
const double* weights = &w->sample_weights[0];
|
|
|
|
int n = (int)_sidx.size();
|
|
|
|
|
|
|
|
AutoBuffer<uchar> buf(n*(sizeof(int) + sizeof(float)));
|
|
|
|
|
2018-06-11 06:42:00 +08:00
|
|
|
float* values = (float*)buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
int* sorted_idx = (int*)(values + n);
|
|
|
|
w->data->getValues(vi, _sidx, values);
|
|
|
|
const double* responses = &w->ord_responses[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, si, best_i = -1;
|
|
|
|
double L = 0, R = 0;
|
|
|
|
double best_val = initQuality, lsum = 0, rsum = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
sorted_idx[i] = i;
|
|
|
|
si = _sidx[i];
|
|
|
|
R += weights[si];
|
|
|
|
rsum += weights[si]*responses[si];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(sorted_idx, sorted_idx + n, cmp_lt_idx<float>(values));
|
|
|
|
|
|
|
|
// find the optimal split
|
|
|
|
for( i = 0; i < n - 1; i++ )
|
|
|
|
{
|
|
|
|
int curr = sorted_idx[i];
|
|
|
|
int next = sorted_idx[i+1];
|
|
|
|
si = _sidx[curr];
|
|
|
|
double wval = weights[si];
|
|
|
|
double t = responses[si]*wval;
|
|
|
|
L += wval; R -= wval;
|
|
|
|
lsum += t; rsum -= t;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-03-22 08:33:36 +08:00
|
|
|
float value_between = (values[next] + values[curr]) * 0.5f;
|
|
|
|
if( value_between > values[curr] && value_between < values[next] )
|
2014-07-30 03:54:23 +08:00
|
|
|
{
|
|
|
|
double val = (lsum*lsum*R + rsum*rsum*L)/(L*R);
|
|
|
|
if( best_val < val )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
best_val = val;
|
|
|
|
best_i = i;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
WSplit split;
|
|
|
|
if( best_i >= 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
split.varIdx = vi;
|
|
|
|
split.c = (values[sorted_idx[best_i]] + values[sorted_idx[best_i+1]])*0.5f;
|
2014-08-03 16:46:28 +08:00
|
|
|
split.inversed = false;
|
2014-07-30 03:54:23 +08:00
|
|
|
split.quality = (float)best_val;
|
|
|
|
}
|
|
|
|
return split;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
DTreesImpl::WSplit DTreesImpl::findSplitCatReg( int vi, const vector<int>& _sidx,
|
|
|
|
double initQuality, int* subset )
|
|
|
|
{
|
|
|
|
const double* weights = &w->sample_weights[0];
|
|
|
|
const double* responses = &w->ord_responses[0];
|
|
|
|
int n = (int)_sidx.size();
|
|
|
|
int mi = getCatCount(vi);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
AutoBuffer<double> buf(3*mi + 3 + n);
|
2018-06-11 06:42:00 +08:00
|
|
|
double* sum = buf.data() + 1;
|
2014-07-30 03:54:23 +08:00
|
|
|
double* counts = sum + mi + 1;
|
|
|
|
double** sum_ptr = (double**)(counts + mi);
|
|
|
|
int* cat_labels = (int*)(sum_ptr + mi);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
w->data->getNormCatValues(vi, _sidx, cat_labels);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
double L = 0, R = 0, best_val = initQuality, lsum = 0, rsum = 0;
|
|
|
|
int i, si, best_subset = -1, subset_i;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = -1; i < mi; i++ )
|
|
|
|
sum[i] = counts[i] = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// calculate sum response and weight of each category of the input var
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
int idx = cat_labels[i];
|
|
|
|
si = _sidx[i];
|
|
|
|
double wval = weights[si];
|
|
|
|
sum[idx] += responses[si]*wval;
|
|
|
|
counts[idx] += wval;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// calculate average response in each category
|
|
|
|
for( i = 0; i < mi; i++ )
|
|
|
|
{
|
|
|
|
R += counts[i];
|
|
|
|
rsum += sum[i];
|
|
|
|
sum[i] = fabs(counts[i]) > DBL_EPSILON ? sum[i]/counts[i] : 0;
|
|
|
|
sum_ptr[i] = sum + i;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
std::sort(sum_ptr, sum_ptr + mi, cmp_lt_ptr<double>());
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
// revert back to unnormalized sums
|
|
|
|
// (there should be a very little loss in accuracy)
|
|
|
|
for( i = 0; i < mi; i++ )
|
|
|
|
sum[i] *= counts[i];
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( subset_i = 0; subset_i < mi-1; subset_i++ )
|
|
|
|
{
|
|
|
|
int idx = (int)(sum_ptr[subset_i] - sum);
|
|
|
|
double ni = counts[idx];
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( ni > FLT_EPSILON )
|
|
|
|
{
|
|
|
|
double s = sum[idx];
|
|
|
|
lsum += s; L += ni;
|
|
|
|
rsum -= s; R -= ni;
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( L > FLT_EPSILON && R > FLT_EPSILON )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double val = (lsum*lsum*R + rsum*rsum*L)/(L*R);
|
|
|
|
if( best_val < val )
|
|
|
|
{
|
|
|
|
best_val = val;
|
|
|
|
best_subset = subset_i;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
WSplit split;
|
|
|
|
if( best_subset >= 0 )
|
|
|
|
{
|
|
|
|
split.varIdx = vi;
|
|
|
|
split.quality = (float)best_val;
|
|
|
|
memset( subset, 0, getSubsetSize(vi) * sizeof(int));
|
|
|
|
for( i = 0; i <= best_subset; i++ )
|
|
|
|
{
|
|
|
|
int idx = (int)(sum_ptr[i] - sum);
|
|
|
|
subset[idx >> 5] |= 1 << (idx & 31);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return split;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::calcDir( int splitidx, const vector<int>& _sidx,
|
|
|
|
vector<int>& _sleft, vector<int>& _sright )
|
|
|
|
{
|
|
|
|
WSplit split = w->wsplits[splitidx];
|
|
|
|
int i, si, n = (int)_sidx.size(), vi = split.varIdx;
|
|
|
|
_sleft.reserve(n);
|
|
|
|
_sright.reserve(n);
|
|
|
|
_sleft.clear();
|
|
|
|
_sright.clear();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
AutoBuffer<float> buf(n);
|
|
|
|
int mi = getCatCount(vi);
|
|
|
|
double wleft = 0, wright = 0;
|
|
|
|
const double* weights = &w->sample_weights[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( mi <= 0 ) // split on an ordered variable
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
float c = split.c;
|
2018-06-11 06:42:00 +08:00
|
|
|
float* values = buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
w->data->getValues(vi, _sidx, values);
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
si = _sidx[i];
|
|
|
|
if( values[i] <= c )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
_sleft.push_back(si);
|
|
|
|
wleft += weights[si];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
_sright.push_back(si);
|
|
|
|
wright += weights[si];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const int* subset = &w->wsubsets[split.subsetOfs];
|
2018-06-11 06:42:00 +08:00
|
|
|
int* cat_labels = (int*)buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
w->data->getNormCatValues(vi, _sidx, cat_labels);
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
si = _sidx[i];
|
|
|
|
unsigned u = cat_labels[i];
|
|
|
|
if( CV_DTREE_CAT_DIR(u, subset) < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
_sleft.push_back(si);
|
|
|
|
wleft += weights[si];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
_sright.push_back(si);
|
|
|
|
wright += weights[si];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Assert( (int)_sleft.size() < n && (int)_sright.size() < n );
|
|
|
|
return wleft > wright ? -1 : 1;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::pruneCV( int root )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
vector<double> ab;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// 1. build tree sequence for each cv fold, calculate error_{Tj,beta_k}.
|
|
|
|
// 2. choose the best tree index (if need, apply 1SE rule).
|
|
|
|
// 3. store the best index and cut the branches.
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
int ti, tree_count = 0, j, cv_n = params.getCVFolds(), n = w->wnodes[root].sample_count;
|
2010-05-12 01:44:00 +08:00
|
|
|
// currently, 1SE for regression is not implemented
|
2014-07-30 03:54:23 +08:00
|
|
|
bool use_1se = params.use1SERule != 0 && _isClassifier;
|
2010-05-12 01:44:00 +08:00
|
|
|
double min_err = 0, min_err_se = 0;
|
|
|
|
int min_idx = -1;
|
|
|
|
|
|
|
|
// build the main tree sequence, calculate alpha's
|
|
|
|
for(;;tree_count++)
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double min_alpha = updateTreeRNC(root, tree_count, -1);
|
|
|
|
if( cutTree(root, tree_count, -1, min_alpha) )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
ab.push_back(min_alpha);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( tree_count > 0 )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
ab[0] = 0.;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( ti = 1; ti < tree_count-1; ti++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
ab[ti] = std::sqrt(ab[ti]*ab[ti+1]);
|
|
|
|
ab[tree_count-1] = DBL_MAX*0.5;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
Mat err_jk(cv_n, tree_count, CV_64F);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
|
|
|
{
|
|
|
|
int tj = 0, tk = 0;
|
2014-07-30 03:54:23 +08:00
|
|
|
for( ; tj < tree_count; tj++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
double min_alpha = updateTreeRNC(root, tj, j);
|
|
|
|
if( cutTree(root, tj, j, min_alpha) )
|
2010-05-12 01:44:00 +08:00
|
|
|
min_alpha = DBL_MAX;
|
|
|
|
|
|
|
|
for( ; tk < tree_count; tk++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
if( ab[tk] > min_alpha )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2014-07-30 03:54:23 +08:00
|
|
|
err_jk.at<double>(j, tk) = w->wnodes[root].tree_error;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ti = 0; ti < tree_count; ti++ )
|
|
|
|
{
|
|
|
|
double sum_err = 0;
|
|
|
|
for( j = 0; j < cv_n; j++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
sum_err += err_jk.at<double>(j, ti);
|
2010-05-12 01:44:00 +08:00
|
|
|
if( ti == 0 || sum_err < min_err )
|
|
|
|
{
|
|
|
|
min_err = sum_err;
|
|
|
|
min_idx = ti;
|
|
|
|
if( use_1se )
|
|
|
|
min_err_se = sqrt( sum_err*(n - sum_err) );
|
|
|
|
}
|
|
|
|
else if( sum_err < min_err + min_err_se )
|
|
|
|
min_idx = ti;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return min_idx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
double DTreesImpl::updateTreeRNC( int root, double T, int fold )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
int nidx = root, pidx = -1, cv_n = params.getCVFolds();
|
2010-05-12 01:44:00 +08:00
|
|
|
double min_alpha = DBL_MAX;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
WNode *node = 0, *parent = 0;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
node = &w->wnodes[nidx];
|
|
|
|
double t = fold >= 0 ? w->cv_Tn[nidx*cv_n + fold] : node->Tn;
|
|
|
|
if( t <= T || node->left < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
node->complexity = 1;
|
|
|
|
node->tree_risk = node->node_risk;
|
|
|
|
node->tree_error = 0.;
|
|
|
|
if( fold >= 0 )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
node->tree_risk = w->cv_node_risk[nidx*cv_n + fold];
|
|
|
|
node->tree_error = w->cv_node_error[nidx*cv_n + fold];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
nidx = node->left;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( pidx = node->parent; pidx >= 0 && w->wnodes[pidx].right == nidx;
|
|
|
|
nidx = pidx, pidx = w->wnodes[pidx].parent )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
node = &w->wnodes[nidx];
|
|
|
|
parent = &w->wnodes[pidx];
|
2010-05-12 01:44:00 +08:00
|
|
|
parent->complexity += node->complexity;
|
|
|
|
parent->tree_risk += node->tree_risk;
|
|
|
|
parent->tree_error += node->tree_error;
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
parent->alpha = ((fold >= 0 ? w->cv_node_risk[pidx*cv_n + fold] : parent->node_risk)
|
|
|
|
- parent->tree_risk)/(parent->complexity - 1);
|
|
|
|
min_alpha = std::min( min_alpha, parent->alpha );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( pidx < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
node = &w->wnodes[nidx];
|
|
|
|
parent = &w->wnodes[pidx];
|
2010-05-12 01:44:00 +08:00
|
|
|
parent->complexity = node->complexity;
|
|
|
|
parent->tree_risk = node->tree_risk;
|
|
|
|
parent->tree_error = node->tree_error;
|
2014-07-30 03:54:23 +08:00
|
|
|
nidx = parent->right;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return min_alpha;
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
bool DTreesImpl::cutTree( int root, double T, int fold, double min_alpha )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
int cv_n = params.getCVFolds(), nidx = root, pidx = -1;
|
2014-07-30 03:54:23 +08:00
|
|
|
WNode* node = &w->wnodes[root];
|
|
|
|
if( node->left < 0 )
|
|
|
|
return true;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
node = &w->wnodes[nidx];
|
|
|
|
double t = fold >= 0 ? w->cv_Tn[nidx*cv_n + fold] : node->Tn;
|
|
|
|
if( t <= T || node->left < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
if( node->alpha <= min_alpha + FLT_EPSILON )
|
|
|
|
{
|
|
|
|
if( fold >= 0 )
|
2014-07-30 03:54:23 +08:00
|
|
|
w->cv_Tn[nidx*cv_n + fold] = T;
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
node->Tn = T;
|
2014-07-30 03:54:23 +08:00
|
|
|
if( nidx == root )
|
|
|
|
return true;
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
nidx = node->left;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( pidx = node->parent; pidx >= 0 && w->wnodes[pidx].right == nidx;
|
|
|
|
nidx = pidx, pidx = w->wnodes[pidx].parent )
|
2010-05-12 01:44:00 +08:00
|
|
|
;
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( pidx < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
nidx = w->wnodes[pidx].right;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
float DTreesImpl::predictTrees( const Range& range, const Mat& sample, int flags ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Assert( sample.type() == CV_32F );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int predictType = flags & PREDICT_MASK;
|
|
|
|
int nvars = (int)varIdx.size();
|
|
|
|
if( nvars == 0 )
|
|
|
|
nvars = (int)varType.size();
|
|
|
|
int i, ncats = (int)catOfs.size(), nclasses = (int)classLabels.size();
|
|
|
|
int catbufsize = ncats > 0 ? nvars : 0;
|
|
|
|
AutoBuffer<int> buf(nclasses + catbufsize + 1);
|
2018-06-11 06:42:00 +08:00
|
|
|
int* votes = buf.data();
|
2014-07-30 03:54:23 +08:00
|
|
|
int* catbuf = votes + nclasses;
|
|
|
|
const int* cvidx = (flags & (COMPRESSED_INPUT|PREPROCESSED_INPUT)) == 0 && !varIdx.empty() ? &compVarIdx[0] : 0;
|
|
|
|
const uchar* vtype = &varType[0];
|
|
|
|
const Vec2i* cofs = !catOfs.empty() ? &catOfs[0] : 0;
|
|
|
|
const int* cmap = !catMap.empty() ? &catMap[0] : 0;
|
|
|
|
const float* psample = sample.ptr<float>();
|
|
|
|
const float* missingSubstPtr = !missingSubst.empty() ? &missingSubst[0] : 0;
|
|
|
|
size_t sstep = sample.isContinuous() ? 1 : sample.step/sizeof(float);
|
|
|
|
double sum = 0.;
|
|
|
|
int lastClassIdx = -1;
|
|
|
|
const float MISSED_VAL = TrainData::missingValue();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < catbufsize; i++ )
|
|
|
|
catbuf[i] = -1;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( predictType == PREDICT_AUTO )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
predictType = !_isClassifier || (classLabels.size() == 2 && (flags & RAW_OUTPUT) != 0) ?
|
|
|
|
PREDICT_SUM : PREDICT_MAX_VOTE;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( predictType == PREDICT_MAX_VOTE )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < nclasses; i++ )
|
|
|
|
votes[i] = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( int ridx = range.start; ridx < range.end; ridx++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int nidx = roots[ridx], prev = nidx, c = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for(;;)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
prev = nidx;
|
|
|
|
const Node& node = nodes[nidx];
|
|
|
|
if( node.split < 0 )
|
|
|
|
break;
|
|
|
|
const Split& split = splits[node.split];
|
|
|
|
int vi = split.varIdx;
|
|
|
|
int ci = cvidx ? cvidx[vi] : vi;
|
|
|
|
float val = psample[ci*sstep];
|
|
|
|
if( val == MISSED_VAL )
|
|
|
|
{
|
|
|
|
if( !missingSubstPtr )
|
|
|
|
{
|
|
|
|
nidx = node.defaultDir < 0 ? node.left : node.right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
val = missingSubstPtr[vi];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( vtype[vi] == VAR_ORDERED )
|
|
|
|
nidx = val <= split.c ? node.left : node.right;
|
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
if( flags & PREPROCESSED_INPUT )
|
2010-05-12 01:44:00 +08:00
|
|
|
c = cvRound(val);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c = catbuf[ci];
|
|
|
|
if( c < 0 )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int a = c = cofs[vi][0];
|
|
|
|
int b = cofs[vi][1];
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int ival = cvRound(val);
|
|
|
|
if( ival != val )
|
2011-02-18 18:29:57 +08:00
|
|
|
CV_Error( CV_StsBadArg,
|
2014-07-30 03:54:23 +08:00
|
|
|
"one of input categorical variable is not an integer" );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2017-06-28 21:26:55 +08:00
|
|
|
CV_Assert(cmap != NULL);
|
2010-05-12 01:44:00 +08:00
|
|
|
while( a < b )
|
|
|
|
{
|
|
|
|
c = (a + b) >> 1;
|
|
|
|
if( ival < cmap[c] )
|
|
|
|
b = c;
|
|
|
|
else if( ival > cmap[c] )
|
|
|
|
a = c+1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Assert( c >= 0 && ival == cmap[c] );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
c -= cofs[vi][0];
|
|
|
|
catbuf[ci] = c;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
const int* subset = &subsets[split.subsetOfs];
|
|
|
|
unsigned u = c;
|
|
|
|
nidx = CV_DTREE_CAT_DIR(u, subset) < 0 ? node.left : node.right;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( predictType == PREDICT_SUM )
|
|
|
|
sum += nodes[prev].value;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastClassIdx = nodes[prev].classIdx;
|
|
|
|
votes[lastClassIdx]++;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( predictType == PREDICT_MAX_VOTE )
|
|
|
|
{
|
|
|
|
int best_idx = lastClassIdx;
|
|
|
|
if( range.end - range.start > 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
best_idx = 0;
|
|
|
|
for( i = 1; i < nclasses; i++ )
|
|
|
|
if( votes[best_idx] < votes[i] )
|
|
|
|
best_idx = i;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
sum = (flags & RAW_OUTPUT) ? (float)best_idx : classLabels[best_idx];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return (float)sum;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
float DTreesImpl::predict( InputArray _samples, OutputArray _results, int flags ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
CV_Assert( !roots.empty() );
|
|
|
|
Mat samples = _samples.getMat(), results;
|
|
|
|
int i, nsamples = samples.rows;
|
|
|
|
int rtype = CV_32F;
|
|
|
|
bool needresults = _results.needed();
|
|
|
|
float retval = 0.f;
|
|
|
|
bool iscls = isClassifier();
|
|
|
|
float scale = !iscls ? 1.f/(int)roots.size() : 1.f;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( iscls && (flags & PREDICT_MASK) == PREDICT_MAX_VOTE )
|
|
|
|
rtype = CV_32S;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( needresults )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
_results.create(nsamples, 1, rtype);
|
|
|
|
results = _results.getMat();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nsamples = std::min(nsamples, 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < nsamples; i++ )
|
|
|
|
{
|
|
|
|
float val = predictTrees( Range(0, (int)roots.size()), samples.row(i), flags )*scale;
|
|
|
|
if( needresults )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
if( rtype == CV_32F )
|
|
|
|
results.at<float>(i) = val;
|
|
|
|
else
|
|
|
|
results.at<int>(i) = cvRound(val);
|
|
|
|
}
|
|
|
|
if( i == 0 )
|
|
|
|
retval = val;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::writeTrainingParams(FileStorage& fs) const
|
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
fs << "use_surrogates" << (params.useSurrogates ? 1 : 0);
|
|
|
|
fs << "max_categories" << params.getMaxCategories();
|
|
|
|
fs << "regression_accuracy" << params.getRegressionAccuracy();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
fs << "max_depth" << params.getMaxDepth();
|
|
|
|
fs << "min_sample_count" << params.getMinSampleCount();
|
|
|
|
fs << "cross_validation_folds" << params.getCVFolds();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
if( params.getCVFolds() > 1 )
|
|
|
|
fs << "use_1se_rule" << (params.use1SERule ? 1 : 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
if( !params.priors.empty() )
|
|
|
|
fs << "priors" << params.priors;
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::writeParams(FileStorage& fs) const
|
|
|
|
{
|
|
|
|
fs << "is_classifier" << isClassifier();
|
|
|
|
fs << "var_all" << (int)varType.size();
|
|
|
|
fs << "var_count" << getVarCount();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int ord_var_count = 0, cat_var_count = 0;
|
|
|
|
int i, n = (int)varType.size();
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
if( varType[i] == VAR_ORDERED )
|
|
|
|
ord_var_count++;
|
|
|
|
else
|
|
|
|
cat_var_count++;
|
|
|
|
fs << "ord_var_count" << ord_var_count;
|
|
|
|
fs << "cat_var_count" << cat_var_count;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "training_params" << "{";
|
|
|
|
writeTrainingParams(fs);
|
|
|
|
|
|
|
|
fs << "}";
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( !varIdx.empty() )
|
2014-12-16 23:15:50 +08:00
|
|
|
{
|
|
|
|
fs << "global_var_idx" << 1;
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "var_idx" << varIdx;
|
2014-12-16 23:15:50 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "var_type" << varType;
|
|
|
|
|
|
|
|
if( !catOfs.empty() )
|
|
|
|
fs << "cat_ofs" << catOfs;
|
|
|
|
if( !catMap.empty() )
|
|
|
|
fs << "cat_map" << catMap;
|
|
|
|
if( !classLabels.empty() )
|
|
|
|
fs << "class_labels" << classLabels;
|
|
|
|
if( !missingSubst.empty() )
|
|
|
|
fs << "missing_subst" << missingSubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DTreesImpl::writeSplit( FileStorage& fs, int splitidx ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const Split& split = splits[splitidx];
|
|
|
|
|
|
|
|
fs << "{:";
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int vi = split.varIdx;
|
|
|
|
fs << "var" << vi;
|
|
|
|
fs << "quality" << split.quality;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( varType[vi] == VAR_CATEGORICAL ) // split on a categorical var
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, n = getCatCount(vi), to_right = 0;
|
|
|
|
const int* subset = &subsets[split.subsetOfs];
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i < n; i++ )
|
2014-07-30 03:54:23 +08:00
|
|
|
to_right += CV_DTREE_CAT_DIR(i, subset) > 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// ad-hoc rule when to use inverse categorical split notation
|
|
|
|
// to achieve more compact and clear representation
|
2014-07-30 03:54:23 +08:00
|
|
|
int default_dir = to_right <= 1 || to_right <= std::min(3, n/2) || to_right <= n/3 ? -1 : 1;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << (default_dir*(split.inversed ? -1 : 1) > 0 ? "in" : "not_in") << "[:";
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int dir = CV_DTREE_CAT_DIR(i, subset);
|
2010-05-12 01:44:00 +08:00
|
|
|
if( dir*default_dir < 0 )
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << i;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
|
|
|
|
fs << "]";
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << (!split.inversed ? "le" : "gt") << split.c;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "}";
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::writeNode( FileStorage& fs, int nidx, int depth ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
const Node& node = nodes[nidx];
|
|
|
|
fs << "{";
|
|
|
|
fs << "depth" << depth;
|
|
|
|
fs << "value" << node.value;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _isClassifier )
|
|
|
|
fs << "norm_class_idx" << node.classIdx;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( node.split >= 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "splits" << "[";
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( int splitidx = node.split; splitidx >= 0; splitidx = splits[splitidx].next )
|
|
|
|
writeSplit( fs, splitidx );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "]";
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "}";
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::writeTree( FileStorage& fs, int root ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "nodes" << "[";
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int nidx = root, pidx = 0, depth = 0;
|
|
|
|
const Node *node = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// traverse the tree and save all the nodes in depth-first order
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
writeNode( fs, nidx, depth );
|
|
|
|
node = &nodes[nidx];
|
|
|
|
if( node->left < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2014-07-30 03:54:23 +08:00
|
|
|
nidx = node->left;
|
|
|
|
depth++;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( pidx = node->parent; pidx >= 0 && nodes[pidx].right == nidx;
|
|
|
|
nidx = pidx, pidx = nodes[pidx].parent )
|
|
|
|
depth--;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( pidx < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
nidx = nodes[pidx].right;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
fs << "]";
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::write( FileStorage& fs ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2016-03-23 06:19:42 +08:00
|
|
|
writeFormat(fs);
|
2014-07-30 03:54:23 +08:00
|
|
|
writeParams(fs);
|
|
|
|
writeTree(fs, roots[0]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::readParams( const FileNode& fn )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
_isClassifier = (int)fn["is_classifier"] != 0;
|
|
|
|
/*int var_all = (int)fn["var_all"];
|
|
|
|
int var_count = (int)fn["var_count"];
|
|
|
|
int cat_var_count = (int)fn["cat_var_count"];
|
|
|
|
int ord_var_count = (int)fn["ord_var_count"];*/
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
FileNode tparams_node = fn["training_params"];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
TreeParams params0 = TreeParams();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( !tparams_node.empty() ) // training parameters are not necessary
|
|
|
|
{
|
|
|
|
params0.useSurrogates = (int)tparams_node["use_surrogates"] != 0;
|
2015-02-11 18:24:14 +08:00
|
|
|
params0.setMaxCategories((int)(tparams_node["max_categories"].empty() ? 16 : tparams_node["max_categories"]));
|
|
|
|
params0.setRegressionAccuracy((float)tparams_node["regression_accuracy"]);
|
|
|
|
params0.setMaxDepth((int)tparams_node["max_depth"]);
|
|
|
|
params0.setMinSampleCount((int)tparams_node["min_sample_count"]);
|
|
|
|
params0.setCVFolds((int)tparams_node["cross_validation_folds"]);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
if( params0.getCVFolds() > 1 )
|
2014-07-30 03:54:23 +08:00
|
|
|
{
|
|
|
|
params.use1SERule = (int)tparams_node["use_1se_rule"] != 0;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
tparams_node["priors"] >> params0.priors;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-16 23:15:50 +08:00
|
|
|
readVectorOrMat(fn["var_idx"], varIdx);
|
2014-07-30 03:54:23 +08:00
|
|
|
fn["var_type"] >> varType;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-12-16 23:15:50 +08:00
|
|
|
int format = 0;
|
|
|
|
fn["format"] >> format;
|
|
|
|
bool isLegacy = format < 3;
|
|
|
|
|
|
|
|
int varAll = (int)fn["var_all"];
|
|
|
|
if (isLegacy && (int)varType.size() <= varAll)
|
|
|
|
{
|
|
|
|
std::vector<uchar> extendedTypes(varAll + 1, 0);
|
|
|
|
|
|
|
|
int i = 0, n;
|
|
|
|
if (!varIdx.empty())
|
|
|
|
{
|
|
|
|
n = (int)varIdx.size();
|
|
|
|
for (; i < n; ++i)
|
|
|
|
{
|
|
|
|
int var = varIdx[i];
|
|
|
|
extendedTypes[var] = varType[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n = (int)varType.size();
|
|
|
|
for (; i < n; ++i)
|
|
|
|
{
|
|
|
|
extendedTypes[i] = varType[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extendedTypes[varAll] = (uchar)(_isClassifier ? VAR_CATEGORICAL : VAR_ORDERED);
|
|
|
|
extendedTypes.swap(varType);
|
|
|
|
}
|
|
|
|
|
|
|
|
readVectorOrMat(fn["cat_map"], catMap);
|
|
|
|
|
|
|
|
if (isLegacy)
|
|
|
|
{
|
|
|
|
// generating "catOfs" from "cat_count"
|
|
|
|
catOfs.clear();
|
|
|
|
classLabels.clear();
|
|
|
|
std::vector<int> counts;
|
|
|
|
readVectorOrMat(fn["cat_count"], counts);
|
|
|
|
unsigned int i = 0, j = 0, curShift = 0, size = (int)varType.size() - 1;
|
|
|
|
for (; i < size; ++i)
|
|
|
|
{
|
|
|
|
Vec2i newOffsets(0, 0);
|
|
|
|
if (varType[i] == VAR_CATEGORICAL) // only categorical vars are represented in catMap
|
|
|
|
{
|
|
|
|
newOffsets[0] = curShift;
|
|
|
|
curShift += counts[j];
|
|
|
|
newOffsets[1] = curShift;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
catOfs.push_back(newOffsets);
|
|
|
|
}
|
|
|
|
// other elements in "catMap" are "classLabels"
|
|
|
|
if (curShift < catMap.size())
|
|
|
|
{
|
|
|
|
classLabels.insert(classLabels.end(), catMap.begin() + curShift, catMap.end());
|
|
|
|
catMap.erase(catMap.begin() + curShift, catMap.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fn["cat_ofs"] >> catOfs;
|
|
|
|
fn["missing_subst"] >> missingSubst;
|
|
|
|
fn["class_labels"] >> classLabels;
|
|
|
|
}
|
|
|
|
|
|
|
|
// init var mapping for node reading (var indexes or varIdx indexes)
|
|
|
|
bool globalVarIdx = false;
|
|
|
|
fn["global_var_idx"] >> globalVarIdx;
|
|
|
|
if (globalVarIdx || varIdx.empty())
|
|
|
|
setRangeVector(varMapping, (int)varType.size());
|
|
|
|
else
|
|
|
|
varMapping = varIdx;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
initCompVarIdx();
|
|
|
|
setDParams(params0);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::readSplit( const FileNode& fn )
|
|
|
|
{
|
|
|
|
Split split;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int vi = (int)fn["var"];
|
|
|
|
CV_Assert( 0 <= vi && vi <= (int)varType.size() );
|
2014-12-16 23:15:50 +08:00
|
|
|
vi = varMapping[vi]; // convert to varIdx if needed
|
2014-07-30 03:54:23 +08:00
|
|
|
split.varIdx = vi;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( varType[vi] == VAR_CATEGORICAL ) // split on categorical var
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, val, ssize = getSubsetSize(vi);
|
|
|
|
split.subsetOfs = (int)subsets.size();
|
|
|
|
for( i = 0; i < ssize; i++ )
|
|
|
|
subsets.push_back(0);
|
|
|
|
int* subset = &subsets[split.subsetOfs];
|
|
|
|
FileNode fns = fn["in"];
|
|
|
|
if( fns.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
fns = fn["not_in"];
|
|
|
|
split.inversed = true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( fns.isInt() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
val = (int)fns;
|
|
|
|
subset[val >> 5] |= 1 << (val & 31);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
FileNodeIterator it = fns.begin();
|
|
|
|
int n = (int)fns.size();
|
|
|
|
for( i = 0; i < n; i++, ++it )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
val = (int)*it;
|
|
|
|
subset[val >> 5] |= 1 << (val & 31);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for categorical splits we do not use inversed splits,
|
|
|
|
// instead we inverse the variable set in the split
|
2014-07-30 03:54:23 +08:00
|
|
|
if( split.inversed )
|
|
|
|
{
|
|
|
|
for( i = 0; i < ssize; i++ )
|
|
|
|
subset[i] ^= -1;
|
|
|
|
split.inversed = false;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
FileNode cmpNode = fn["le"];
|
|
|
|
if( cmpNode.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
cmpNode = fn["gt"];
|
|
|
|
split.inversed = true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
split.c = (float)cmpNode;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
split.quality = (float)fn["quality"];
|
|
|
|
splits.push_back(split);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
return (int)(splits.size() - 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::readNode( const FileNode& fn )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
Node node;
|
|
|
|
node.value = (double)fn["value"];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
if( _isClassifier )
|
|
|
|
node.classIdx = (int)fn["norm_class_idx"];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
FileNode sfn = fn["splits"];
|
|
|
|
if( !sfn.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, n = (int)sfn.size(), prevsplit = -1;
|
|
|
|
FileNodeIterator it = sfn.begin();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < n; i++, ++it )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int splitidx = readSplit(*it);
|
|
|
|
if( splitidx < 0 )
|
|
|
|
break;
|
|
|
|
if( prevsplit < 0 )
|
|
|
|
node.split = splitidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
2014-07-30 03:54:23 +08:00
|
|
|
splits[prevsplit].next = splitidx;
|
|
|
|
prevsplit = splitidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
nodes.push_back(node);
|
|
|
|
return (int)(nodes.size() - 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
int DTreesImpl::readTree( const FileNode& fn )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int i, n = (int)fn.size(), root = -1, pidx = -1;
|
|
|
|
FileNodeIterator it = fn.begin();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
for( i = 0; i < n; i++, ++it )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
int nidx = readNode(*it);
|
|
|
|
if( nidx < 0 )
|
|
|
|
break;
|
|
|
|
Node& node = nodes[nidx];
|
|
|
|
node.parent = pidx;
|
|
|
|
if( pidx < 0 )
|
|
|
|
root = nidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
2014-07-30 03:54:23 +08:00
|
|
|
{
|
|
|
|
Node& parent = nodes[pidx];
|
|
|
|
if( parent.left < 0 )
|
|
|
|
parent.left = nidx;
|
|
|
|
else
|
|
|
|
parent.right = nidx;
|
|
|
|
}
|
|
|
|
if( node.split >= 0 )
|
|
|
|
pidx = nidx;
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
{
|
2014-07-30 03:54:23 +08:00
|
|
|
while( pidx >= 0 && nodes[pidx].right >= 0 )
|
|
|
|
pidx = nodes[pidx].parent;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 03:54:23 +08:00
|
|
|
roots.push_back(root);
|
|
|
|
return root;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
void DTreesImpl::read( const FileNode& fn )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
clear();
|
2014-07-30 03:54:23 +08:00
|
|
|
readParams(fn);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
FileNode fnodes = fn["nodes"];
|
|
|
|
CV_Assert( !fnodes.empty() );
|
|
|
|
readTree(fnodes);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:24:14 +08:00
|
|
|
Ptr<DTrees> DTrees::create()
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2015-02-11 18:24:14 +08:00
|
|
|
return makePtr<DTreesImpl>();
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2014-08-03 07:08:25 +08:00
|
|
|
|
2017-01-29 18:13:01 +08:00
|
|
|
Ptr<DTrees> DTrees::load(const String& filepath, const String& nodeName)
|
|
|
|
{
|
|
|
|
return Algorithm::load<DTrees>(filepath, nodeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 03:54:23 +08:00
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* End of file. */
|