tree.cpp 53 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*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.
//
//
10 11
//                           License Agreement
//                For Open Source Computer Vision Library
12 13
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Copyright (C) 2014, Itseez Inc, all rights reserved.
15 16 17 18 19 20 21 22 23 24 25 26
// 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.
//
27
//   * The name of the copyright holders may not be used to endorse or promote products
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
//     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>

46 47
namespace cv {
namespace ml {
48

49
using std::vector;
50

51
void DTrees::setDParams(const DTrees::Params&)
52
{
53
    CV_Error(CV_StsNotImplemented, "");
54 55
}

56
DTrees::Params DTrees::getDParams() const
57
{
58 59
    CV_Error(CV_StsNotImplemented, "");
    return DTrees::Params();
60 61
}

62
DTrees::Params::Params()
63
{
64 65 66 67 68 69 70 71 72
    maxDepth = INT_MAX;
    minSampleCount = 10;
    regressionAccuracy = 0.01f;
    useSurrogates = false;
    maxCategories = 10;
    CVFolds = 10;
    use1SERule = true;
    truncatePrunedTree = true;
    priors = Mat();
73 74
}

75 76 77 78 79
DTrees::Params::Params( int _maxDepth, int _minSampleCount,
                        double _regressionAccuracy, bool _useSurrogates,
                        int _maxCategories, int _CVFolds,
                        bool _use1SERule, bool _truncatePrunedTree,
                        const Mat& _priors )
80
{
81 82 83 84 85 86 87 88 89 90
    maxDepth = _maxDepth;
    minSampleCount = _minSampleCount;
    regressionAccuracy = (float)_regressionAccuracy;
    useSurrogates = _useSurrogates;
    maxCategories = _maxCategories;
    CVFolds = _CVFolds;
    use1SERule = _use1SERule;
    truncatePrunedTree = _truncatePrunedTree;
    priors = _priors;
}
91

92
DTrees::Node::Node()
93
{
94 95 96 97
    classIdx = 0;
    value = 0;
    parent = left = right = split = defaultDir = -1;
}
98

99
DTrees::Split::Split()
100
{
101 102 103 104 105 106 107
    varIdx = 0;
    inversed = false;
    quality = 0.f;
    next = -1;
    c = 0.f;
    subsetOfs = 0;
}
108 109


110 111 112 113 114 115
DTreesImpl::WorkData::WorkData(const Ptr<TrainData>& _data)
{
    data = _data;
    vector<int> subsampleIdx;
    Mat sidx0 = _data->getTrainSampleIdx();
    if( !sidx0.empty() )
116
    {
117 118
        sidx0.copyTo(sidx);
        std::sort(sidx.begin(), sidx.end());
119 120 121
    }
    else
    {
122 123
        int n = _data->getNSamples();
        setRangeVector(sidx, n);
124 125
    }

126 127
    maxSubsetSize = 0;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142
DTreesImpl::DTreesImpl() {}
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();
Andrey Kamaev's avatar
Andrey Kamaev committed
143

144 145 146
    w.release();
    _isClassifier = false;
}
147

148 149 150 151
void DTreesImpl::startTraining( const Ptr<TrainData>& data, int )
{
    clear();
    w = makePtr<WorkData>(data);
Andrey Kamaev's avatar
Andrey Kamaev committed
152

153 154
    Mat vtype = data->getVarType();
    vtype.copyTo(varType);
155

156 157 158
    data->getCatOfs().copyTo(catOfs);
    data->getCatMap().copyTo(catMap);
    data->getDefaultSubstValues().copyTo(missingSubst);
159

160
    int nallvars = data->getNAllVars();
161

162 163 164 165 166
    Mat vidx0 = data->getVarIdx();
    if( !vidx0.empty() )
        vidx0.copyTo(varIdx);
    else
        setRangeVector(varIdx, nallvars);
167

168
    initCompVarIdx();
169

170
    w->maxSubsetSize = 0;
Andrey Kamaev's avatar
Andrey Kamaev committed
171

172 173 174
    int i, nvars = (int)varIdx.size();
    for( i = 0; i < nvars; i++ )
        w->maxSubsetSize = std::max(w->maxSubsetSize, getCatCount(varIdx[i]));
Andrey Kamaev's avatar
Andrey Kamaev committed
175

176
    w->maxSubsetSize = std::max((w->maxSubsetSize + 31)/32, 1);
177

178
    data->getSampleWeights().copyTo(w->sample_weights);
179

180
    _isClassifier = data->getResponseType() == VAR_CATEGORICAL;
181

182
    if( _isClassifier )
183
    {
184 185 186
        data->getNormCatResponses().copyTo(w->cat_responses);
        data->getClassLabels().copyTo(classLabels);
        int nclasses = (int)classLabels.size();
187

188 189 190 191 192 193 194 195 196 197
        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 );
198

199 200 201
            int nsamples = (int)w->cat_responses.size();
            const double* cw = class_weights.ptr<double>();
            CV_Assert( (int)w->sample_weights.size() == nsamples );
202

203 204 205 206 207 208 209
            for( i = 0; i < nsamples; i++ )
            {
                int ci = w->cat_responses[i];
                CV_Assert( 0 <= ci && ci < nclasses );
                w->sample_weights[i] *= cw[ci];
            }
        }
210 211
    }
    else
212 213 214 215 216 217 218 219 220 221
        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++ )
222
    {
223 224 225 226
        int vi = varIdx[i];
        CV_Assert( 0 <= vi && vi < nallvars && vi > prevIdx );
        prevIdx = vi;
        compVarIdx[vi] = i;
Andrey Kamaev's avatar
Andrey Kamaev committed
227
    }
228 229 230 231 232 233
}

void DTreesImpl::endTraining()
{
    w.release();
}
234

235 236 237 238 239 240 241 242
bool DTreesImpl::train( const Ptr<TrainData>& trainData, int flags )
{
    startTraining(trainData, flags);
    bool ok = addTree( w->sidx ) >= 0;
    w.release();
    endTraining();
    return ok;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
243

244 245 246 247
const vector<int>& DTreesImpl::getActiveVars()
{
    return varIdx;
}
248

249 250 251
int DTreesImpl::addTree(const vector<int>& sidx )
{
    size_t n = (params.maxDepth > 0 ? (1 << params.maxDepth) : 1024) + w->wnodes.size();
252

253 254 255 256 257 258
    w->wnodes.reserve(n);
    w->wsplits.reserve(n);
    w->wsubsets.reserve(n*w->maxSubsetSize);
    w->wnodes.clear();
    w->wsplits.clear();
    w->wsubsets.clear();
259

260
    int cv_n = params.CVFolds;
261

262
    if( cv_n > 0 )
263
    {
264 265 266
        w->cv_Tn.resize(n*cv_n);
        w->cv_node_error.resize(n*cv_n);
        w->cv_node_risk.resize(n*cv_n);
267 268
    }

269 270 271
    // build the tree recursively
    int w_root = addNodeAndTrySplit(-1, sidx);
    int maxdepth = INT_MAX;//pruneCV(root);
272

273 274
    int w_nidx = w_root, pidx = -1, depth = 0;
    int root = (int)nodes.size();
275

276
    for(;;)
277
    {
278 279 280 281 282 283
        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;
284

285 286
        int wsplit_idx = wnode.split;
        if( wsplit_idx >= 0 )
287
        {
288 289 290 291 292 293 294 295
            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 )
296
            {
297 298 299 300
                int ssize = getSubsetSize(split.varIdx);
                split.subsetOfs = (int)subsets.size();
                subsets.resize(split.subsetOfs + ssize);
                memcpy(&subsets[split.subsetOfs], &w->wsubsets[wsplit.subsetOfs], ssize*sizeof(int));
Andrey Kamaev's avatar
Andrey Kamaev committed
301
            }
302 303
            node.split = (int)splits.size();
            splits.push_back(split);
304
        }
305 306 307
        int nidx = (int)nodes.size();
        nodes.push_back(node);
        if( pidx >= 0 )
308
        {
309 310
            int w_pidx = w->wnodes[w_nidx].parent;
            if( w->wnodes[w_pidx].left == w_nidx )
311
            {
312
                nodes[pidx].left = nidx;
313 314 315
            }
            else
            {
316 317
                CV_Assert(w->wnodes[w_pidx].right == w_nidx);
                nodes[pidx].right = nidx;
318
            }
319
        }
320

321 322 323 324 325
        if( wnode.left >= 0 && depth+1 < maxdepth )
        {
            w_nidx = wnode.left;
            pidx = nidx;
            depth++;
326 327 328
        }
        else
        {
329 330
            int w_pidx = wnode.parent;
            while( w_pidx >= 0 && w->wnodes[w_pidx].right == w_nidx )
331
            {
332 333 334 335 336
                w_nidx = w_pidx;
                w_pidx = w->wnodes[w_pidx].parent;
                nidx = pidx;
                pidx = nodes[pidx].parent;
                depth--;
337 338
            }

339 340
            if( w_pidx < 0 )
                break;
341

342
            w_nidx = w->wnodes[w_pidx].right;
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
343
            CV_Assert( w_nidx >= 0 );
344 345
        }
    }
346 347
    roots.push_back(root);
    return root;
348 349
}

350
DTrees::Params DTreesImpl::getDParams() const
351
{
352
    return params0;
353 354
}

355
void DTreesImpl::setDParams(const Params& _params)
356
{
357 358 359 360
    params0 = params = _params;
    if( params.maxCategories < 2 )
        CV_Error( CV_StsOutOfRange, "params.max_categories should be >= 2" );
    params.maxCategories = std::min( params.maxCategories, 15 );
361

362 363 364
    if( params.maxDepth < 0 )
        CV_Error( CV_StsOutOfRange, "params.max_depth should be >= 0" );
    params.maxDepth = std::min( params.maxDepth, 25 );
365

366
    params.minSampleCount = std::max(params.minSampleCount, 1);
367

368 369 370 371
    if( params.CVFolds < 0 )
        CV_Error( CV_StsOutOfRange,
                 "params.CVFolds should be =0 (the tree is not pruned) "
                 "or n>0 (tree is pruned using n-fold cross-validation)" );
372

373 374
    if( params.CVFolds == 1 )
        params.CVFolds = 0;
375

376 377 378
    if( params.regressionAccuracy < 0 )
        CV_Error( CV_StsOutOfRange, "params.regression_accuracy should be >= 0" );
}
379

380 381 382 383 384
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();
385

386 387 388
    node.parent = parent;
    node.depth = parent >= 0 ? w->wnodes[parent].depth + 1 : 0;
    int nfolds = params.CVFolds;
389

390
    if( nfolds > 0 )
391
    {
392 393 394
        w->cv_Tn.resize((nidx+1)*nfolds);
        w->cv_node_error.resize((nidx+1)*nfolds);
        w->cv_node_risk.resize((nidx+1)*nfolds);
395 396
    }

397 398 399
    int i, n = node.sample_count = (int)sidx.size();
    bool can_split = true;
    vector<int> sleft, sright;
400

401
    calcValue( nidx, sidx );
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    if( n <= params.minSampleCount || node.depth >= params.maxDepth )
        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
    {
        if( sqrt(node.node_risk) < params.regressionAccuracy )
            can_split = false;
    }
421

422 423
    if( can_split )
        node.split = findBestSplit( sidx );
424

425
    //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);
426

427 428 429 430 431
    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");
432

433 434 435 436
        int left = addNodeAndTrySplit( nidx, sleft );
        int right = addNodeAndTrySplit( nidx, sright );
        w->wnodes[nidx].left = left;
        w->wnodes[nidx].right = right;
437
        CV_Assert( w->wnodes[nidx].left > 0 && w->wnodes[nidx].right > 0 );
438 439
    }

440
    return nidx;
441 442
}

443
int DTreesImpl::findBestSplit( const vector<int>& _sidx )
444
{
445 446 447 448 449 450 451
    const vector<int>& activeVars = getActiveVars();
    int splitidx = -1;
    int vi_, nv = (int)activeVars.size();
    AutoBuffer<int> buf(w->maxSubsetSize*2);
    int *subset = buf, *best_subset = subset + w->maxSubsetSize;
    WSplit split, best_split;
    best_split.quality = 0.;
452

453
    for( vi_ = 0; vi_ < nv; vi_++ )
454
    {
455 456
        int vi = activeVars[vi_];
        if( varType[vi] == VAR_CATEGORICAL )
457
        {
458 459 460 461
            if( _isClassifier )
                split = findSplitCatClass(vi, _sidx, 0, subset);
            else
                split = findSplitCatReg(vi, _sidx, 0, subset);
462
        }
463
        else
464
        {
465 466 467 468
            if( _isClassifier )
                split = findSplitOrdClass(vi, _sidx, 0);
            else
                split = findSplitOrdReg(vi, _sidx, 0);
469
        }
470
        if( split.quality > best_split.quality )
471
        {
472 473
            best_split = split;
            std::swap(subset, best_subset);
474 475 476
        }
    }

477 478 479 480 481 482 483 484 485 486 487 488
    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);
    }
489

490
    return splitidx;
491 492
}

493
void DTreesImpl::calcValue( int nidx, const vector<int>& _sidx )
494
{
495 496 497
    WNode* node = &w->wnodes[nidx];
    int i, j, k, n = (int)_sidx.size(), cv_n = params.CVFolds;
    int m = (int)classLabels.size();
498

499
    cv::AutoBuffer<double> buf(std::max(m, 3)*(cv_n+1));
500

501 502 503 504 505 506 507
    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);
    }
508

509
    if( _isClassifier )
510 511 512 513 514 515 516 517 518 519
    {
        // 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
520 521 522
        double* cls_count = buf;
        double* cv_cls_count = cls_count + m;

523 524 525 526 527 528 529 530 531
        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++ )
532 533 534 535
            {
                int si = _sidx[i];
                cls_count[w->cat_responses[si]] += w->sample_weights[si];
            }
536 537 538 539 540 541 542 543 544
        }
        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++ )
            {
545 546 547
                int si = _sidx[i];
                j = w->cv_labels[si]; k = w->cat_responses[si];
                cv_cls_count[j*m + k] += w->sample_weights[si];
548 549 550 551 552 553 554 555 556
            }

            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++ )
        {
557
            double val = cls_count[k];
558 559 560 561 562 563 564 565 566
            total_weight += val;
            if( max_val < val )
            {
                max_val = val;
                max_k = k;
            }
        }

        node->class_idx = max_k;
567
        node->value = classLabels[max_k];
568 569 570 571 572 573 574 575 576
        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++ )
            {
577 578
                double val_k = cv_cls_count[j*m + k];
                double val = cls_count[k] - val_k;
579 580 581 582 583 584 585 586 587 588
                sum_k += val_k;
                sum += val;
                if( max_val < val )
                {
                    max_val = val;
                    max_val_k = val_k;
                    max_k = k;
                }
            }

589 590 591
            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;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
        }
    }
    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.
608
        double sum = 0, sum2 = 0, sumw = 0;
609 610 611 612 613

        if( cv_n == 0 )
        {
            for( i = 0; i < n; i++ )
            {
614 615 616 617 618 619
                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;
620 621 622 623
            }
        }
        else
        {
624 625
            double *cv_sum = buf, *cv_sum2 = cv_sum + cv_n;
            double* cv_count = (double*)(cv_sum2 + cv_n);
626 627 628 629 630 631 632 633 634

            for( j = 0; j < cv_n; j++ )
            {
                cv_sum[j] = cv_sum2[j] = 0.;
                cv_count[j] = 0;
            }

            for( i = 0; i < n; i++ )
            {
635 636 637 638 639 640 641
                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;
642
            }
643

644 645 646 647
            for( j = 0; j < cv_n; j++ )
            {
                sum += cv_sum[j];
                sum2 += cv_sum2[j];
648 649 650 651 652 653 654 655 656 657 658 659
                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;
660 661
            }
        }
662

663 664 665 666
        node->node_risk = sum2 - (sum/sumw)*sum;
        node->value = sum/sumw;
    }
}
667

668 669 670 671 672
DTreesImpl::WSplit DTreesImpl::findSplitOrdClass( int vi, const vector<int>& _sidx, double initQuality )
{
    const double epsilon = FLT_EPSILON*2;
    int n = (int)_sidx.size();
    int m = (int)classLabels.size();
673

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    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];
    double* lcw = (double*)(uchar*)buf;
    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;

        if( values[curr] + epsilon < values[next] )
721
        {
722 723 724 725 726 727
            double val = (lsum2*R + rsum2*L)/(L*R);
            if( best_val < val )
            {
                best_val = val;
                best_i = i;
            }
728 729 730
        }
    }

731 732 733 734 735
    WSplit split;
    if( best_i >= 0 )
    {
        split.varIdx = vi;
        split.c = (values[sorted_idx[best_i]] + values[sorted_idx[best_i+1]])*0.5f;
736
        split.inversed = false;
737 738 739 740
        split.quality = (float)best_val;
    }
    return split;
}
741

742 743
// 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 )
744
{
745 746 747 748 749
    int iters = 0, max_iters = 100;
    int i, j, idx;
    cv::AutoBuffer<double> buf(n + k);
    double *v_weights = buf, *c_weights = buf + n;
    bool modified = true;
750
    RNG r((uint64)-1);
751 752 753 754

    // assign labels randomly
    for( i = 0; i < n; i++ )
    {
755
        double sum = 0;
756 757
        const double* v = vectors + i*m;
        labels[i] = i < k ? i : r.uniform(0, k);
758

759 760 761 762 763 764 765 766 767 768 769 770 771 772
        // 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++ )
773
    {
774 775 776 777 778 779 780 781
        // 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++ )
782
        {
783 784 785 786 787
            const double* v = vectors + i*m;
            double* s = csums + labels[i]*m;
            for( j = 0; j < m; j++ )
                s[j] += v[j];
        }
788

789 790 791
        // exit the loop here, when we have up-to-date csums
        if( iters == max_iters || !modified )
            break;
792

793
        modified = false;
Andrey Kamaev's avatar
Andrey Kamaev committed
794

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
        // 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++ )
814
            {
815 816 817
                const double* s = csums + idx*m;
                double dist2 = 0., beta = c_weights[idx];
                for( j = 0; j < m; j++ )
818
                {
819 820 821 822 823 824 825
                    double t = v[j]*alpha - s[j]*beta;
                    dist2 += t*t;
                }
                if( min_dist2 > dist2 )
                {
                    min_dist2 = dist2;
                    min_idx = idx;
826 827
                }
            }
828

829 830 831
            if( min_idx != labels[i] )
                modified = true;
            labels[i] = min_idx;
832 833
        }
    }
834
}
835

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
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;
    if( m > 2 && mi > params.maxCategories )
        base_size += m*std::min(params.maxCategories, n) + mi;
    else
        base_size += mi;
    AutoBuffer<double> buf(base_size + n);

    double* lc = (double*)buf;
    double* rc = lc + m;
    double* _cjk = rc + m*2, *cjk = _cjk;
    double* c_weights = cjk + m*mi;

    int* labels = (int*)(buf + base_size);
    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++ )
874
    {
875 876 877 878
        si = _sidx[i];
        j = labels[i];
        k = responses[si];
        cjk[j*m + k] += weights[si];
879 880
    }

881
    if( m > 2 )
882
    {
883
        if( mi > params.maxCategories )
884
        {
885 886 887 888
            mi = std::min(params.maxCategories, n);
            cjk = c_weights + _mi;
            cluster_labels = (int*)(cjk + m*mi);
            clusterCategories( _cjk, _mi, m, cjk, mi, cluster_labels );
889
        }
890 891
        subset_i = 1;
        subset_n = 1 << mi;
892
    }
893
    else
894
    {
895 896 897 898 899 900 901
        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;
902 903
    }

904 905 906 907 908 909 910 911 912
    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;
    }
913

914 915 916 917 918 919 920 921
    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];
    }
922

923
    for( ; subset_i < subset_n; subset_i++ )
924
    {
925
        double lsum2 = 0, rsum2 = 0;
926

927 928 929 930 931 932
        if( m == 2 )
            idx = (int)(dbl_ptr[subset_i] - cjk)/2;
        else
        {
            int graycode = (subset_i>>1)^subset_i;
            int diff = graycode ^ prevcode;
933

934 935 936 937 938 939 940 941
            // 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;
        }
942

943 944 945 946
        double* crow = cjk + idx*m;
        double weight = c_weights[idx];
        if( weight < FLT_EPSILON )
            continue;
947

948
        if( !subtract )
949
        {
950
            for( k = 0; k < m; k++ )
951
            {
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
                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;
972
            }
973 974 975
            L -= weight;
            R += weight;
        }
976

977 978 979 980 981 982 983 984 985 986
        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;
            }
        }
    }
987

988 989 990 991 992 993 994 995 996
    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++ )
997
            {
998 999
                idx = (int)(dbl_ptr[i] - cjk) >> 1;
                subset[idx >> 5] |= 1 << (idx & 31);
1000 1001 1002 1003
            }
        }
        else
        {
1004
            for( i = 0; i < _mi; i++ )
1005
            {
1006 1007 1008
                idx = cluster_labels ? cluster_labels[i] : i;
                if( best_subset & (1 << idx) )
                    subset[i >> 5] |= 1 << (i & 31);
1009
            }
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
        }
    }
    return split;
}

DTreesImpl::WSplit DTreesImpl::findSplitOrdReg( int vi, const vector<int>& _sidx, double initQuality )
{
    const float epsilon = FLT_EPSILON*2;
    const double* weights = &w->sample_weights[0];
    int n = (int)_sidx.size();

    AutoBuffer<uchar> buf(n*(sizeof(int) + sizeof(float)));

    float* values = (float*)(uchar*)buf;
    int* sorted_idx = (int*)(values + n);
    w->data->getValues(vi, _sidx, values);
    const double* responses = &w->ord_responses[0];
1027

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    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;
1052

1053 1054 1055 1056
        if( values[curr] + epsilon < values[next] )
        {
            double val = (lsum*lsum*R + rsum*rsum*L)/(L*R);
            if( best_val < val )
1057
            {
1058 1059
                best_val = val;
                best_i = i;
1060 1061 1062 1063
            }
        }
    }

1064 1065
    WSplit split;
    if( best_i >= 0 )
1066
    {
1067 1068
        split.varIdx = vi;
        split.c = (values[sorted_idx[best_i]] + values[sorted_idx[best_i+1]])*0.5f;
1069
        split.inversed = false;
1070 1071 1072 1073
        split.quality = (float)best_val;
    }
    return split;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1074

1075 1076 1077 1078 1079 1080 1081
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);
1082

1083 1084 1085 1086 1087
    AutoBuffer<double> buf(3*mi + 3 + n);
    double* sum = (double*)buf + 1;
    double* counts = sum + mi + 1;
    double** sum_ptr = (double**)(counts + mi);
    int* cat_labels = (int*)(sum_ptr + mi);
1088

1089
    w->data->getNormCatValues(vi, _sidx, cat_labels);
1090

1091 1092
    double L = 0, R = 0, best_val = initQuality, lsum = 0, rsum = 0;
    int i, si, best_subset = -1, subset_i;
Andrey Kamaev's avatar
Andrey Kamaev committed
1093

1094 1095
    for( i = -1; i < mi; i++ )
        sum[i] = counts[i] = 0;
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105
    // 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;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
1106

1107 1108 1109 1110 1111 1112 1113 1114
    // 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;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
1115

1116
    std::sort(sum_ptr, sum_ptr + mi, cmp_lt_ptr<double>());
1117

1118 1119 1120 1121
    // revert back to unnormalized sums
    // (there should be a very little loss in accuracy)
    for( i = 0; i < mi; i++ )
        sum[i] *= counts[i];
1122

1123 1124 1125 1126
    for( subset_i = 0; subset_i < mi-1; subset_i++ )
    {
        int idx = (int)(sum_ptr[subset_i] - sum);
        double ni = counts[idx];
1127

1128 1129 1130 1131 1132
        if( ni > FLT_EPSILON )
        {
            double s = sum[idx];
            lsum += s; L += ni;
            rsum -= s; R -= ni;
1133

1134
            if( L > FLT_EPSILON && R > FLT_EPSILON )
1135
            {
1136 1137 1138 1139 1140 1141
                double val = (lsum*lsum*R + rsum*rsum*L)/(L*R);
                if( best_val < val )
                {
                    best_val = val;
                    best_subset = subset_i;
                }
1142
            }
Andrey Kamaev's avatar
Andrey Kamaev committed
1143
        }
1144
    }
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    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;
}
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169
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();
1170

1171 1172 1173 1174
    AutoBuffer<float> buf(n);
    int mi = getCatCount(vi);
    double wleft = 0, wright = 0;
    const double* weights = &w->sample_weights[0];
1175

1176
    if( mi <= 0 ) // split on an ordered variable
1177
    {
1178 1179 1180 1181 1182
        float c = split.c;
        float* values = buf;
        w->data->getValues(vi, _sidx, values);

        for( i = 0; i < n; i++ )
1183
        {
1184 1185
            si = _sidx[i];
            if( values[i] <= c )
1186
            {
1187 1188
                _sleft.push_back(si);
                wleft += weights[si];
1189 1190 1191
            }
            else
            {
1192 1193
                _sright.push_back(si);
                wright += weights[si];
1194 1195 1196 1197 1198
            }
        }
    }
    else
    {
1199 1200 1201 1202 1203
        const int* subset = &w->wsubsets[split.subsetOfs];
        int* cat_labels = (int*)(float*)buf;
        w->data->getNormCatValues(vi, _sidx, cat_labels);

        for( i = 0; i < n; i++ )
1204
        {
1205 1206 1207
            si = _sidx[i];
            unsigned u = cat_labels[i];
            if( CV_DTREE_CAT_DIR(u, subset) < 0 )
1208
            {
1209 1210
                _sleft.push_back(si);
                wleft += weights[si];
1211 1212 1213
            }
            else
            {
1214 1215
                _sright.push_back(si);
                wright += weights[si];
1216 1217 1218
            }
        }
    }
1219 1220
    CV_Assert( (int)_sleft.size() < n && (int)_sright.size() < n );
    return wleft > wright ? -1 : 1;
1221 1222
}

1223
int DTreesImpl::pruneCV( int root )
1224
{
1225
    vector<double> ab;
1226 1227 1228 1229 1230

    // 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.

1231
    int ti, tree_count = 0, j, cv_n = params.CVFolds, n = w->wnodes[root].sample_count;
1232
    // currently, 1SE for regression is not implemented
1233
    bool use_1se = params.use1SERule != 0 && _isClassifier;
1234 1235 1236 1237 1238 1239
    double min_err = 0, min_err_se = 0;
    int min_idx = -1;

    // build the main tree sequence, calculate alpha's
    for(;;tree_count++)
    {
1240 1241
        double min_alpha = updateTreeRNC(root, tree_count, -1);
        if( cutTree(root, tree_count, -1, min_alpha) )
1242 1243
            break;

1244
        ab.push_back(min_alpha);
1245 1246 1247 1248
    }

    if( tree_count > 0 )
    {
1249 1250
        ab[0] = 0.;

1251
        for( ti = 1; ti < tree_count-1; ti++ )
1252 1253
            ab[ti] = std::sqrt(ab[ti]*ab[ti+1]);
        ab[tree_count-1] = DBL_MAX*0.5;
1254

1255
        Mat err_jk(cv_n, tree_count, CV_64F);
1256 1257 1258 1259

        for( j = 0; j < cv_n; j++ )
        {
            int tj = 0, tk = 0;
1260
            for( ; tj < tree_count; tj++ )
1261
            {
1262 1263
                double min_alpha = updateTreeRNC(root, tj, j);
                if( cutTree(root, tj, j, min_alpha) )
1264 1265 1266 1267
                    min_alpha = DBL_MAX;

                for( ; tk < tree_count; tk++ )
                {
1268
                    if( ab[tk] > min_alpha )
1269
                        break;
1270
                    err_jk.at<double>(j, tk) = w->wnodes[root].tree_error;
1271 1272 1273 1274 1275 1276 1277 1278
                }
            }
        }

        for( ti = 0; ti < tree_count; ti++ )
        {
            double sum_err = 0;
            for( j = 0; j < cv_n; j++ )
1279
                sum_err += err_jk.at<double>(j, ti);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
            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;
        }
    }

1292
    return min_idx;
1293 1294
}

1295
double DTreesImpl::updateTreeRNC( int root, double T, int fold )
1296
{
1297
    int nidx = root, pidx = -1, cv_n = params.CVFolds;
1298 1299 1300 1301
    double min_alpha = DBL_MAX;

    for(;;)
    {
1302 1303
        WNode *node = 0, *parent = 0;

1304 1305
        for(;;)
        {
1306 1307 1308
            node = &w->wnodes[nidx];
            double t = fold >= 0 ? w->cv_Tn[nidx*cv_n + fold] : node->Tn;
            if( t <= T || node->left < 0 )
1309 1310 1311 1312 1313 1314
            {
                node->complexity = 1;
                node->tree_risk = node->node_risk;
                node->tree_error = 0.;
                if( fold >= 0 )
                {
1315 1316
                    node->tree_risk = w->cv_node_risk[nidx*cv_n + fold];
                    node->tree_error = w->cv_node_error[nidx*cv_n + fold];
1317 1318 1319
                }
                break;
            }
1320
            nidx = node->left;
1321 1322
        }

1323 1324
        for( pidx = node->parent; pidx >= 0 && w->wnodes[pidx].right == nidx;
             nidx = pidx, pidx = w->wnodes[pidx].parent )
1325
        {
1326 1327
            node = &w->wnodes[nidx];
            parent = &w->wnodes[pidx];
1328 1329 1330 1331
            parent->complexity += node->complexity;
            parent->tree_risk += node->tree_risk;
            parent->tree_error += node->tree_error;

1332 1333 1334
            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 );
1335 1336
        }

1337
        if( pidx < 0 )
1338 1339
            break;

1340 1341
        node = &w->wnodes[nidx];
        parent = &w->wnodes[pidx];
1342 1343 1344
        parent->complexity = node->complexity;
        parent->tree_risk = node->tree_risk;
        parent->tree_error = node->tree_error;
1345
        nidx = parent->right;
1346 1347 1348 1349 1350
    }

    return min_alpha;
}

1351
bool DTreesImpl::cutTree( int root, double T, int fold, double min_alpha )
1352
{
1353 1354 1355 1356
    int cv_n = params.CVFolds, nidx = root, pidx = -1;
    WNode* node = &w->wnodes[root];
    if( node->left < 0 )
        return true;
1357 1358 1359 1360 1361

    for(;;)
    {
        for(;;)
        {
1362 1363 1364
            node = &w->wnodes[nidx];
            double t = fold >= 0 ? w->cv_Tn[nidx*cv_n + fold] : node->Tn;
            if( t <= T || node->left < 0 )
1365 1366 1367 1368
                break;
            if( node->alpha <= min_alpha + FLT_EPSILON )
            {
                if( fold >= 0 )
1369
                    w->cv_Tn[nidx*cv_n + fold] = T;
1370 1371
                else
                    node->Tn = T;
1372 1373
                if( nidx == root )
                    return true;
1374 1375
                break;
            }
1376
            nidx = node->left;
1377
        }
1378

1379 1380
        for( pidx = node->parent; pidx >= 0 && w->wnodes[pidx].right == nidx;
             nidx = pidx, pidx = w->wnodes[pidx].parent )
1381
            ;
1382

1383
        if( pidx < 0 )
1384
            break;
1385

1386
        nidx = w->wnodes[pidx].right;
1387
    }
1388

1389
    return false;
1390 1391
}

1392
float DTreesImpl::predictTrees( const Range& range, const Mat& sample, int flags ) const
1393
{
1394
    CV_Assert( sample.type() == CV_32F );
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    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);
    int* votes = buf;
    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();
1415

1416 1417
    for( i = 0; i < catbufsize; i++ )
        catbuf[i] = -1;
1418

1419
    if( predictType == PREDICT_AUTO )
1420
    {
1421 1422
        predictType = !_isClassifier || (classLabels.size() == 2 && (flags & RAW_OUTPUT) != 0) ?
            PREDICT_SUM : PREDICT_MAX_VOTE;
1423 1424
    }

1425
    if( predictType == PREDICT_MAX_VOTE )
1426
    {
1427 1428
        for( i = 0; i < nclasses; i++ )
            votes[i] = 0;
1429 1430
    }

1431
    for( int ridx = range.start; ridx < range.end; ridx++ )
1432
    {
1433
        int nidx = roots[ridx], prev = nidx, c = 0;
1434

1435
        for(;;)
1436
        {
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
            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
1458
            {
1459
                if( flags & PREPROCESSED_INPUT )
1460 1461 1462 1463 1464 1465
                    c = cvRound(val);
                else
                {
                    c = catbuf[ci];
                    if( c < 0 )
                    {
1466 1467
                        int a = c = cofs[vi][0];
                        int b = cofs[vi][1];
Andrey Kamaev's avatar
Andrey Kamaev committed
1468

1469 1470
                        int ival = cvRound(val);
                        if( ival != val )
1471
                            CV_Error( CV_StsBadArg,
1472
                                     "one of input categorical variable is not an integer" );
Andrey Kamaev's avatar
Andrey Kamaev committed
1473

1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
                        while( a < b )
                        {
                            c = (a + b) >> 1;
                            if( ival < cmap[c] )
                                b = c;
                            else if( ival > cmap[c] )
                                a = c+1;
                            else
                                break;
                        }

1485
                        CV_Assert( c >= 0 && ival == cmap[c] );
1486

1487 1488
                        c -= cofs[vi][0];
                        catbuf[ci] = c;
1489
                    }
1490 1491 1492
                    const int* subset = &subsets[split.subsetOfs];
                    unsigned u = c;
                    nidx = CV_DTREE_CAT_DIR(u, subset) < 0 ? node.left : node.right;
1493 1494
                }
            }
1495
        }
1496

1497 1498 1499 1500 1501 1502
        if( predictType == PREDICT_SUM )
            sum += nodes[prev].value;
        else
        {
            lastClassIdx = nodes[prev].classIdx;
            votes[lastClassIdx]++;
1503
        }
1504
    }
1505

1506 1507 1508 1509
    if( predictType == PREDICT_MAX_VOTE )
    {
        int best_idx = lastClassIdx;
        if( range.end - range.start > 1 )
1510
        {
1511 1512 1513 1514
            best_idx = 0;
            for( i = 1; i < nclasses; i++ )
                if( votes[best_idx] < votes[i] )
                    best_idx = i;
1515
        }
1516
        sum = (flags & RAW_OUTPUT) ? (float)best_idx : classLabels[best_idx];
1517 1518
    }

1519
    return (float)sum;
1520 1521 1522
}


1523
float DTreesImpl::predict( InputArray _samples, OutputArray _results, int flags ) const
1524
{
1525 1526 1527 1528 1529 1530 1531 1532
    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;
1533

1534 1535
    if( iscls && (flags & PREDICT_MASK) == PREDICT_MAX_VOTE )
        rtype = CV_32S;
1536

1537
    if( needresults )
1538
    {
1539 1540 1541 1542 1543
        _results.create(nsamples, 1, rtype);
        results = _results.getMat();
    }
    else
        nsamples = std::min(nsamples, 1);
1544

1545 1546 1547 1548
    for( i = 0; i < nsamples; i++ )
    {
        float val = predictTrees( Range(0, (int)roots.size()), samples.row(i), flags )*scale;
        if( needresults )
1549
        {
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
            if( rtype == CV_32F )
                results.at<float>(i) = val;
            else
                results.at<int>(i) = cvRound(val);
        }
        if( i == 0 )
            retval = val;
    }
    return retval;
}
1560

1561 1562 1563 1564 1565
void DTreesImpl::writeTrainingParams(FileStorage& fs) const
{
    fs << "use_surrogates" << (params0.useSurrogates ? 1 : 0);
    fs << "max_categories" << params0.maxCategories;
    fs << "regression_accuracy" << params0.regressionAccuracy;
1566

1567 1568 1569
    fs << "max_depth" << params0.maxDepth;
    fs << "min_sample_count" << params0.minSampleCount;
    fs << "cross_validation_folds" << params0.CVFolds;
1570

1571 1572
    if( params0.CVFolds > 1 )
        fs << "use_1se_rule" << (params0.use1SERule ? 1 : 0);
1573

1574 1575 1576
    if( !params0.priors.empty() )
        fs << "priors" << params0.priors;
}
1577

1578 1579 1580 1581 1582
void DTreesImpl::writeParams(FileStorage& fs) const
{
    fs << "is_classifier" << isClassifier();
    fs << "var_all" << (int)varType.size();
    fs << "var_count" << getVarCount();
1583

1584 1585 1586 1587 1588 1589 1590 1591 1592
    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;
1593

1594 1595 1596 1597
    fs << "training_params" << "{";
    writeTrainingParams(fs);

    fs << "}";
1598

1599 1600
    if( !varIdx.empty() )
        fs << "var_idx" << varIdx;
1601

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    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
1615
{
1616 1617 1618
    const Split& split = splits[splitidx];

    fs << "{:";
1619

1620 1621 1622
    int vi = split.varIdx;
    fs << "var" << vi;
    fs << "quality" << split.quality;
1623

1624
    if( varType[vi] == VAR_CATEGORICAL ) // split on a categorical var
1625
    {
1626 1627
        int i, n = getCatCount(vi), to_right = 0;
        const int* subset = &subsets[split.subsetOfs];
1628
        for( i = 0; i < n; i++ )
1629
            to_right += CV_DTREE_CAT_DIR(i, subset) > 0;
1630 1631 1632

        // ad-hoc rule when to use inverse categorical split notation
        // to achieve more compact and clear representation
1633
        int default_dir = to_right <= 1 || to_right <= std::min(3, n/2) || to_right <= n/3 ? -1 : 1;
1634

1635
        fs << (default_dir*(split.inversed ? -1 : 1) > 0 ? "in" : "not_in") << "[:";
1636 1637 1638

        for( i = 0; i < n; i++ )
        {
1639
            int dir = CV_DTREE_CAT_DIR(i, subset);
1640
            if( dir*default_dir < 0 )
1641
                fs << i;
1642
        }
1643 1644

        fs << "]";
1645 1646
    }
    else
1647
        fs << (!split.inversed ? "le" : "gt") << split.c;
1648

1649
    fs << "}";
1650 1651
}

1652
void DTreesImpl::writeNode( FileStorage& fs, int nidx, int depth ) const
1653
{
1654 1655 1656 1657
    const Node& node = nodes[nidx];
    fs << "{";
    fs << "depth" << depth;
    fs << "value" << node.value;
1658

1659 1660
    if( _isClassifier )
        fs << "norm_class_idx" << node.classIdx;
1661

1662
    if( node.split >= 0 )
1663
    {
1664
        fs << "splits" << "[";
1665

1666 1667
        for( int splitidx = node.split; splitidx >= 0; splitidx = splits[splitidx].next )
            writeSplit( fs, splitidx );
1668

1669
        fs << "]";
1670 1671
    }

1672
    fs << "}";
1673 1674
}

1675
void DTreesImpl::writeTree( FileStorage& fs, int root ) const
1676
{
1677
    fs << "nodes" << "[";
1678

1679 1680
    int nidx = root, pidx = 0, depth = 0;
    const Node *node = 0;
1681 1682 1683 1684 1685 1686

    // traverse the tree and save all the nodes in depth-first order
    for(;;)
    {
        for(;;)
        {
1687 1688 1689
            writeNode( fs, nidx, depth );
            node = &nodes[nidx];
            if( node->left < 0 )
1690
                break;
1691 1692
            nidx = node->left;
            depth++;
1693 1694
        }

1695 1696 1697
        for( pidx = node->parent; pidx >= 0 && nodes[pidx].right == nidx;
             nidx = pidx, pidx = nodes[pidx].parent )
            depth--;
1698

1699
        if( pidx < 0 )
1700 1701
            break;

1702
        nidx = nodes[pidx].right;
1703 1704
    }

1705
    fs << "]";
1706 1707
}

1708
void DTreesImpl::write( FileStorage& fs ) const
1709
{
1710 1711
    writeParams(fs);
    writeTree(fs, roots[0]);
1712 1713
}

1714
void DTreesImpl::readParams( const FileNode& fn )
1715
{
1716 1717 1718 1719 1720
    _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"];*/
1721

1722
    FileNode tparams_node = fn["training_params"];
1723

1724
    params0 = Params();
1725

1726 1727 1728 1729 1730
    if( !tparams_node.empty() ) // training parameters are not necessary
    {
        params0.useSurrogates = (int)tparams_node["use_surrogates"] != 0;
        params0.maxCategories = (int)tparams_node["max_categories"];
        params0.regressionAccuracy = (float)tparams_node["regression_accuracy"];
1731

1732 1733 1734
        params0.maxDepth = (int)tparams_node["max_depth"];
        params0.minSampleCount = (int)tparams_node["min_sample_count"];
        params0.CVFolds = (int)tparams_node["cross_validation_folds"];
1735

1736 1737 1738 1739
        if( params0.CVFolds > 1 )
        {
            params.use1SERule = (int)tparams_node["use_1se_rule"] != 0;
        }
1740

1741 1742
        tparams_node["priors"] >> params0.priors;
    }
1743

1744 1745
    fn["var_idx"] >> varIdx;
    fn["var_type"] >> varType;
1746

1747 1748 1749 1750
    fn["cat_ofs"] >> catOfs;
    fn["cat_map"] >> catMap;
    fn["missing_subst"] >> missingSubst;
    fn["class_labels"] >> classLabels;
1751

1752 1753 1754
    initCompVarIdx();
    setDParams(params0);
}
1755

1756 1757 1758
int DTreesImpl::readSplit( const FileNode& fn )
{
    Split split;
1759

1760 1761 1762
    int vi = (int)fn["var"];
    CV_Assert( 0 <= vi && vi <= (int)varType.size() );
    split.varIdx = vi;
1763

1764
    if( varType[vi] == VAR_CATEGORICAL ) // split on categorical var
1765
    {
1766 1767 1768 1769 1770 1771 1772
        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() )
1773
        {
1774 1775
            fns = fn["not_in"];
            split.inversed = true;
1776 1777
        }

1778
        if( fns.isInt() )
1779
        {
1780 1781
            val = (int)fns;
            subset[val >> 5] |= 1 << (val & 31);
1782 1783 1784
        }
        else
        {
1785 1786 1787
            FileNodeIterator it = fns.begin();
            int n = (int)fns.size();
            for( i = 0; i < n; i++, ++it )
1788
            {
1789 1790
                val = (int)*it;
                subset[val >> 5] |= 1 << (val & 31);
1791 1792 1793 1794 1795
            }
        }

        // for categorical splits we do not use inversed splits,
        // instead we inverse the variable set in the split
1796 1797 1798 1799 1800 1801
        if( split.inversed )
        {
            for( i = 0; i < ssize; i++ )
                subset[i] ^= -1;
            split.inversed = false;
        }
1802 1803 1804
    }
    else
    {
1805 1806
        FileNode cmpNode = fn["le"];
        if( cmpNode.empty() )
1807
        {
1808 1809
            cmpNode = fn["gt"];
            split.inversed = true;
1810
        }
1811
        split.c = (float)cmpNode;
1812
    }
1813

1814 1815
    split.quality = (float)fn["quality"];
    splits.push_back(split);
1816

1817
    return (int)(splits.size() - 1);
1818 1819
}

1820
int DTreesImpl::readNode( const FileNode& fn )
1821
{
1822 1823
    Node node;
    node.value = (double)fn["value"];
1824

1825 1826
    if( _isClassifier )
        node.classIdx = (int)fn["norm_class_idx"];
1827

1828 1829
    FileNode sfn = fn["splits"];
    if( !sfn.empty() )
1830
    {
1831 1832
        int i, n = (int)sfn.size(), prevsplit = -1;
        FileNodeIterator it = sfn.begin();
1833

1834
        for( i = 0; i < n; i++, ++it )
1835
        {
1836 1837 1838 1839 1840
            int splitidx = readSplit(*it);
            if( splitidx < 0 )
                break;
            if( prevsplit < 0 )
                node.split = splitidx;
1841
            else
1842 1843
                splits[prevsplit].next = splitidx;
            prevsplit = splitidx;
1844 1845
        }
    }
1846 1847
    nodes.push_back(node);
    return (int)(nodes.size() - 1);
1848 1849
}

1850
int DTreesImpl::readTree( const FileNode& fn )
1851
{
1852 1853
    int i, n = (int)fn.size(), root = -1, pidx = -1;
    FileNodeIterator it = fn.begin();
1854

1855
    for( i = 0; i < n; i++, ++it )
1856
    {
1857 1858 1859 1860 1861 1862 1863
        int nidx = readNode(*it);
        if( nidx < 0 )
            break;
        Node& node = nodes[nidx];
        node.parent = pidx;
        if( pidx < 0 )
            root = nidx;
1864
        else
1865 1866 1867 1868 1869 1870 1871 1872 1873
        {
            Node& parent = nodes[pidx];
            if( parent.left < 0 )
                parent.left = nidx;
            else
                parent.right = nidx;
        }
        if( node.split >= 0 )
            pidx = nidx;
1874 1875
        else
        {
1876 1877
            while( pidx >= 0 && nodes[pidx].right >= 0 )
                pidx = nodes[pidx].parent;
1878 1879
        }
    }
1880 1881
    roots.push_back(root);
    return root;
1882 1883
}

1884
void DTreesImpl::read( const FileNode& fn )
1885 1886
{
    clear();
1887
    readParams(fn);
1888

1889 1890 1891
    FileNode fnodes = fn["nodes"];
    CV_Assert( !fnodes.empty() );
    readTree(fnodes);
1892 1893
}

1894
Ptr<DTrees> DTrees::create(const DTrees::Params& params)
1895
{
1896 1897 1898 1899
    Ptr<DTreesImpl> p = makePtr<DTreesImpl>();
    p->setDParams(params);
    return p;
}
1900

1901
}
1902 1903
}

1904
/* End of file. */