1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
21 // * Redistribution's in binary form must reproduce the above copyright notice,
22 // this list of conditions and the following disclaimer in the documentation
23 // and/or other materials provided with the distribution.
25 // * The name of Intel Corporation may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
46 static const float ord_nan = FLT_MAX*0.5f;
47 static const int min_block_size = 1 << 16;
48 static const int block_size_delta = 1 << 10;
50 CvDTreeTrainData::CvDTreeTrainData()
52 var_idx = var_type = cat_count = cat_ofs = cat_map =
53 priors = priors_mult = counts = buf = direction = split_buf = responses_copy = 0;
54 tree_storage = temp_storage = 0;
60 CvDTreeTrainData::CvDTreeTrainData( const CvMat* _train_data, int _tflag,
61 const CvMat* _responses, const CvMat* _var_idx,
62 const CvMat* _sample_idx, const CvMat* _var_type,
63 const CvMat* _missing_mask, const CvDTreeParams& _params,
64 bool _shared, bool _add_labels )
66 var_idx = var_type = cat_count = cat_ofs = cat_map =
67 priors = priors_mult = counts = buf = direction = split_buf = responses_copy = 0;
69 tree_storage = temp_storage = 0;
71 set_data( _train_data, _tflag, _responses, _var_idx, _sample_idx,
72 _var_type, _missing_mask, _params, _shared, _add_labels );
76 CvDTreeTrainData::~CvDTreeTrainData()
82 bool CvDTreeTrainData::set_params( const CvDTreeParams& _params )
86 CV_FUNCNAME( "CvDTreeTrainData::set_params" );
93 if( params.max_categories < 2 )
94 CV_ERROR( CV_StsOutOfRange, "params.max_categories should be >= 2" );
95 params.max_categories = MIN( params.max_categories, 15 );
97 if( params.max_depth < 0 )
98 CV_ERROR( CV_StsOutOfRange, "params.max_depth should be >= 0" );
99 params.max_depth = MIN( params.max_depth, 25 );
101 params.min_sample_count = MAX(params.min_sample_count,1);
103 if( params.cv_folds < 0 )
104 CV_ERROR( CV_StsOutOfRange,
105 "params.cv_folds should be =0 (the tree is not pruned) "
106 "or n>0 (tree is pruned using n-fold cross-validation)" );
108 if( params.cv_folds == 1 )
111 if( params.regression_accuracy < 0 )
112 CV_ERROR( CV_StsOutOfRange, "params.regression_accuracy should be >= 0" );
121 #define CV_CMP_NUM_PTR(a,b) (*(a) < *(b))
122 static CV_IMPLEMENT_QSORT_EX( icvSortIntPtr, int*, CV_CMP_NUM_PTR, int )
123 static CV_IMPLEMENT_QSORT_EX( icvSortDblPtr, double*, CV_CMP_NUM_PTR, int )
125 #define CV_CMP_NUM_IDX(i,j) (aux[i] < aux[j])
126 static CV_IMPLEMENT_QSORT_EX( icvSortIntAux, int, CV_CMP_NUM_IDX, const float* )
127 static CV_IMPLEMENT_QSORT_EX( icvSortUShAux, unsigned short, CV_CMP_NUM_IDX, const float* )
129 #define CV_CMP_PAIRS(a,b) (*((a).i) < *((b).i))
130 static CV_IMPLEMENT_QSORT_EX( icvSortPairs, CvPair16u32s, CV_CMP_PAIRS, int )
132 void CvDTreeTrainData::set_data( const CvMat* _train_data, int _tflag,
133 const CvMat* _responses, const CvMat* _var_idx, const CvMat* _sample_idx,
134 const CvMat* _var_type, const CvMat* _missing_mask, const CvDTreeParams& _params,
135 bool _shared, bool _add_labels, bool _update_data )
137 CvMat* sample_indices = 0;
138 CvMat* var_type0 = 0;
141 CvPair16u32s* pair16u32s_ptr = 0;
142 CvDTreeTrainData* data = 0;
145 unsigned short* udst = 0;
148 CV_FUNCNAME( "CvDTreeTrainData::set_data" );
152 int sample_all = 0, r_type = 0, cv_n;
153 int total_c_count = 0;
154 int tree_block_size, temp_block_size, max_split_size, nv_size, cv_size = 0;
155 int ds_step, dv_step, ms_step = 0, mv_step = 0; // {data|mask}{sample|var}_step
158 const int *sidx = 0, *vidx = 0;
160 if( _update_data && data_root )
162 data = new CvDTreeTrainData( _train_data, _tflag, _responses, _var_idx,
163 _sample_idx, _var_type, _missing_mask, _params, _shared, _add_labels );
165 // compare new and old train data
166 if( !(data->var_count == var_count &&
167 cvNorm( data->var_type, var_type, CV_C ) < FLT_EPSILON &&
168 cvNorm( data->cat_count, cat_count, CV_C ) < FLT_EPSILON &&
169 cvNorm( data->cat_map, cat_map, CV_C ) < FLT_EPSILON) )
170 CV_ERROR( CV_StsBadArg,
171 "The new training data must have the same types and the input and output variables "
172 "and the same categories for categorical variables" );
174 cvReleaseMat( &priors );
175 cvReleaseMat( &priors_mult );
176 cvReleaseMat( &buf );
177 cvReleaseMat( &direction );
178 cvReleaseMat( &split_buf );
179 cvReleaseMemStorage( &temp_storage );
181 priors = data->priors; data->priors = 0;
182 priors_mult = data->priors_mult; data->priors_mult = 0;
183 buf = data->buf; data->buf = 0;
184 buf_count = data->buf_count; buf_size = data->buf_size;
185 sample_count = data->sample_count;
187 direction = data->direction; data->direction = 0;
188 split_buf = data->split_buf; data->split_buf = 0;
189 temp_storage = data->temp_storage; data->temp_storage = 0;
190 nv_heap = data->nv_heap; cv_heap = data->cv_heap;
192 data_root = new_node( 0, sample_count, 0, 0 );
201 CV_CALL( set_params( _params ));
203 // check parameter types and sizes
204 CV_CALL( cvCheckTrainData( _train_data, _tflag, _missing_mask, &var_all, &sample_all ));
206 train_data = _train_data;
207 responses = _responses;
209 if( _tflag == CV_ROW_SAMPLE )
211 ds_step = _train_data->step/CV_ELEM_SIZE(_train_data->type);
214 ms_step = _missing_mask->step, mv_step = 1;
218 dv_step = _train_data->step/CV_ELEM_SIZE(_train_data->type);
221 mv_step = _missing_mask->step, ms_step = 1;
225 sample_count = sample_all;
230 CV_CALL( sample_indices = cvPreprocessIndexArray( _sample_idx, sample_all ));
231 sidx = sample_indices->data.i;
232 sample_count = sample_indices->rows + sample_indices->cols - 1;
237 CV_CALL( var_idx = cvPreprocessIndexArray( _var_idx, var_all ));
238 vidx = var_idx->data.i;
239 var_count = var_idx->rows + var_idx->cols - 1;
243 if ( sample_count < 65536 )
246 if( !CV_IS_MAT(_responses) ||
247 (CV_MAT_TYPE(_responses->type) != CV_32SC1 &&
248 CV_MAT_TYPE(_responses->type) != CV_32FC1) ||
249 (_responses->rows != 1 && _responses->cols != 1) ||
250 _responses->rows + _responses->cols - 1 != sample_all )
251 CV_ERROR( CV_StsBadArg, "The array of _responses must be an integer or "
252 "floating-point vector containing as many elements as "
253 "the total number of samples in the training data matrix" );
256 CV_CALL( var_type0 = cvPreprocessVarType( _var_type, var_idx, var_count, &r_type ));
258 CV_CALL( var_type = cvCreateMat( 1, var_count+2, CV_32SC1 ));
264 is_classifier = r_type == CV_VAR_CATEGORICAL;
266 // step 0. calc the number of categorical vars
267 for( vi = 0; vi < var_count; vi++ )
269 var_type->data.i[vi] = var_type0->data.ptr[vi] == CV_VAR_CATEGORICAL ?
270 cat_var_count++ : ord_var_count--;
273 ord_var_count = ~ord_var_count;
274 cv_n = params.cv_folds;
275 // set the two last elements of var_type array to be able
276 // to locate responses and cross-validation labels using
277 // the corresponding get_* functions.
278 var_type->data.i[var_count] = cat_var_count;
279 var_type->data.i[var_count+1] = cat_var_count+1;
281 // in case of single ordered predictor we need dummy cv_labels
282 // for safe split_node_data() operation
283 have_labels = cv_n > 0 || (ord_var_count == 1 && cat_var_count == 0) || _add_labels;
285 work_var_count = var_count + (is_classifier ? 1 : 0) + (have_labels ? 1 : 0);
286 buf_size = (work_var_count + 1)*sample_count;
288 buf_count = shared ? 2 : 1;
292 CV_CALL( buf = cvCreateMat( buf_count, buf_size, CV_16UC1 ));
293 CV_CALL( pair16u32s_ptr = (CvPair16u32s*)cvAlloc( sample_count*sizeof(pair16u32s_ptr[0]) ));
297 CV_CALL( buf = cvCreateMat( buf_count, buf_size, CV_32SC1 ));
298 CV_CALL( int_ptr = (int**)cvAlloc( sample_count*sizeof(int_ptr[0]) ));
301 size = is_classifier ? (cat_var_count+1) : cat_var_count;
302 size = !size ? 1 : size;
303 CV_CALL( cat_count = cvCreateMat( 1, size, CV_32SC1 ));
304 CV_CALL( cat_ofs = cvCreateMat( 1, size, CV_32SC1 ));
306 size = is_classifier ? (cat_var_count + 1)*params.max_categories : cat_var_count*params.max_categories;
307 size = !size ? 1 : size;
308 CV_CALL( cat_map = cvCreateMat( 1, size, CV_32SC1 ));
310 // now calculate the maximum size of split,
311 // create memory storage that will keep nodes and splits of the decision tree
312 // allocate root node and the buffer for the whole training data
313 max_split_size = cvAlign(sizeof(CvDTreeSplit) +
314 (MAX(0,sample_count - 33)/32)*sizeof(int),sizeof(void*));
315 tree_block_size = MAX((int)sizeof(CvDTreeNode)*8, max_split_size);
316 tree_block_size = MAX(tree_block_size + block_size_delta, min_block_size);
317 CV_CALL( tree_storage = cvCreateMemStorage( tree_block_size ));
318 CV_CALL( node_heap = cvCreateSet( 0, sizeof(*node_heap), sizeof(CvDTreeNode), tree_storage ));
320 nv_size = var_count*sizeof(int);
321 nv_size = cvAlign(MAX( nv_size, (int)sizeof(CvSetElem) ), sizeof(void*));
323 temp_block_size = nv_size;
327 if( sample_count < cv_n*MAX(params.min_sample_count,10) )
328 CV_ERROR( CV_StsOutOfRange,
329 "The many folds in cross-validation for such a small dataset" );
331 cv_size = cvAlign( cv_n*(sizeof(int) + sizeof(double)*2), sizeof(double) );
332 temp_block_size = MAX(temp_block_size, cv_size);
335 temp_block_size = MAX( temp_block_size + block_size_delta, min_block_size );
336 CV_CALL( temp_storage = cvCreateMemStorage( temp_block_size ));
337 CV_CALL( nv_heap = cvCreateSet( 0, sizeof(*nv_heap), nv_size, temp_storage ));
339 CV_CALL( cv_heap = cvCreateSet( 0, sizeof(*cv_heap), cv_size, temp_storage ));
341 CV_CALL( data_root = new_node( 0, sample_count, 0, 0 ));
348 _fdst = (float*)cvAlloc(sample_count*sizeof(_fdst[0]));
349 if (is_buf_16u && (cat_var_count || is_classifier))
350 _idst = (int*)cvAlloc(sample_count*sizeof(_idst[0]));
352 // transform the training data to convenient representation
353 for( vi = 0; vi <= var_count; vi++ )
356 const uchar* mask = 0;
357 int m_step = 0, step;
358 const int* idata = 0;
359 const float* fdata = 0;
362 if( vi < var_count ) // analyze i-th input variable
364 int vi0 = vidx ? vidx[vi] : vi;
365 ci = get_var_type(vi);
366 step = ds_step; m_step = ms_step;
367 if( CV_MAT_TYPE(_train_data->type) == CV_32SC1 )
368 idata = _train_data->data.i + vi0*dv_step;
370 fdata = _train_data->data.fl + vi0*dv_step;
372 mask = _missing_mask->data.ptr + vi0*mv_step;
374 else // analyze _responses
377 step = CV_IS_MAT_CONT(_responses->type) ?
378 1 : _responses->step / CV_ELEM_SIZE(_responses->type);
379 if( CV_MAT_TYPE(_responses->type) == CV_32SC1 )
380 idata = _responses->data.i;
382 fdata = _responses->data.fl;
385 if( (vi < var_count && ci>=0) ||
386 (vi == var_count && is_classifier) ) // process categorical variable or response
388 int c_count, prev_label;
392 udst = (unsigned short*)(buf->data.s + vi*sample_count);
394 idst = buf->data.i + vi*sample_count;
397 for( i = 0; i < sample_count; i++ )
399 int val = INT_MAX, si = sidx ? sidx[i] : i;
400 if( !mask || !mask[si*m_step] )
403 val = idata[si*step];
406 float t = fdata[si*step];
408 if( fabs(t - val) > FLT_EPSILON )
410 sprintf( err, "%d-th value of %d-th (categorical) "
411 "variable is not an integer", i, vi );
412 CV_ERROR( CV_StsBadArg, err );
418 sprintf( err, "%d-th value of %d-th (categorical) "
419 "variable is too large", i, vi );
420 CV_ERROR( CV_StsBadArg, err );
427 pair16u32s_ptr[i].u = udst + i;
428 pair16u32s_ptr[i].i = _idst + i;
433 int_ptr[i] = idst + i;
437 c_count = num_valid > 0;
440 icvSortPairs( pair16u32s_ptr, sample_count, 0 );
441 // count the categories
442 for( i = 1; i < num_valid; i++ )
443 if (*pair16u32s_ptr[i].i != *pair16u32s_ptr[i-1].i)
448 icvSortIntPtr( int_ptr, sample_count, 0 );
449 // count the categories
450 for( i = 1; i < num_valid; i++ )
451 c_count += *int_ptr[i] != *int_ptr[i-1];
455 max_c_count = MAX( max_c_count, c_count );
456 cat_count->data.i[ci] = c_count;
457 cat_ofs->data.i[ci] = total_c_count;
459 // resize cat_map, if need
460 if( cat_map->cols < total_c_count + c_count )
463 CV_CALL( cat_map = cvCreateMat( 1,
464 MAX(cat_map->cols*3/2,total_c_count+c_count), CV_32SC1 ));
465 for( i = 0; i < total_c_count; i++ )
466 cat_map->data.i[i] = tmp_map->data.i[i];
467 cvReleaseMat( &tmp_map );
470 c_map = cat_map->data.i + total_c_count;
471 total_c_count += c_count;
476 // compact the class indices and build the map
477 prev_label = ~*pair16u32s_ptr[0].i;
478 for( i = 0; i < num_valid; i++ )
480 int cur_label = *pair16u32s_ptr[i].i;
481 if( cur_label != prev_label )
482 c_map[++c_count] = prev_label = cur_label;
483 *pair16u32s_ptr[i].u = (unsigned short)c_count;
485 // replace labels for missing values with -1
486 for( ; i < sample_count; i++ )
487 *pair16u32s_ptr[i].u = 65535;
491 // compact the class indices and build the map
492 prev_label = ~*int_ptr[0];
493 for( i = 0; i < num_valid; i++ )
495 int cur_label = *int_ptr[i];
496 if( cur_label != prev_label )
497 c_map[++c_count] = prev_label = cur_label;
498 *int_ptr[i] = c_count;
500 // replace labels for missing values with -1
501 for( ; i < sample_count; i++ )
505 else if( ci < 0 ) // process ordered variable
508 udst = (unsigned short*)(buf->data.s + vi*sample_count);
510 idst = buf->data.i + vi*sample_count;
512 for( i = 0; i < sample_count; i++ )
515 int si = sidx ? sidx[i] : i;
516 if( !mask || !mask[si*m_step] )
519 val = (float)idata[si*step];
521 val = fdata[si*step];
523 if( fabs(val) >= ord_nan )
525 sprintf( err, "%d-th value of %d-th (ordered) "
526 "variable (=%g) is too large", i, vi, val );
527 CV_ERROR( CV_StsBadArg, err );
532 udst[i] = (unsigned short)i;
534 idst[i] = i; // ÔÂÂÌÂÒÚË ‚˚¯Â ‚ if( idata )
539 icvSortUShAux( udst, num_valid, _fdst);
541 icvSortIntAux( idst, /*or num_valid?\*/ sample_count, _fdst );
545 data_root->set_num_valid(vi, num_valid);
550 udst = (unsigned short*)(buf->data.s + work_var_count*sample_count);
552 idst = buf->data.i + work_var_count*sample_count;
554 for (i = 0; i < sample_count; i++)
557 udst[i] = sidx ? (unsigned short)sidx[i] : (unsigned short)i;
559 idst[i] = sidx ? sidx[i] : i;
564 unsigned short* udst = 0;
570 udst = (unsigned short*)(buf->data.s + (get_work_var_count()-1)*sample_count);
571 for( i = vi = 0; i < sample_count; i++ )
573 udst[i] = (unsigned short)vi++;
574 vi &= vi < cv_n ? -1 : 0;
577 for( i = 0; i < sample_count; i++ )
579 int a = cvRandInt(r) % sample_count;
580 int b = cvRandInt(r) % sample_count;
581 unsigned short unsh = (unsigned short)vi;
582 CV_SWAP( udst[a], udst[b], unsh );
587 idst = buf->data.i + (get_work_var_count()-1)*sample_count;
588 for( i = vi = 0; i < sample_count; i++ )
591 vi &= vi < cv_n ? -1 : 0;
594 for( i = 0; i < sample_count; i++ )
596 int a = cvRandInt(r) % sample_count;
597 int b = cvRandInt(r) % sample_count;
598 CV_SWAP( idst[a], idst[b], vi );
604 cat_map->cols = MAX( total_c_count, 1 );
606 max_split_size = cvAlign(sizeof(CvDTreeSplit) +
607 (MAX(0,max_c_count - 33)/32)*sizeof(int),sizeof(void*));
608 CV_CALL( split_heap = cvCreateSet( 0, sizeof(*split_heap), max_split_size, tree_storage ));
610 have_priors = is_classifier && params.priors;
613 int m = get_num_classes();
615 CV_CALL( priors = cvCreateMat( 1, m, CV_64F ));
616 for( i = 0; i < m; i++ )
618 double val = have_priors ? params.priors[i] : 1.;
620 CV_ERROR( CV_StsOutOfRange, "Every class weight should be positive" );
621 priors->data.db[i] = val;
627 cvScale( priors, priors, 1./sum );
629 CV_CALL( priors_mult = cvCloneMat( priors ));
630 CV_CALL( counts = cvCreateMat( 1, m, CV_32SC1 ));
634 CV_CALL( direction = cvCreateMat( 1, sample_count, CV_8UC1 ));
635 CV_CALL( split_buf = cvCreateMat( 1, sample_count, CV_32SC1 ));
638 int maxNumThreads = 1;
640 maxNumThreads = cv::getNumThreads();
642 pred_float_buf.resize(maxNumThreads);
643 pred_int_buf.resize(maxNumThreads);
644 resp_float_buf.resize(maxNumThreads);
645 resp_int_buf.resize(maxNumThreads);
646 cv_lables_buf.resize(maxNumThreads);
647 sample_idx_buf.resize(maxNumThreads);
648 for( int ti = 0; ti < maxNumThreads; ti++ )
650 pred_float_buf[ti].resize(sample_count);
651 pred_int_buf[ti].resize(sample_count);
652 resp_float_buf[ti].resize(sample_count);
653 resp_int_buf[ti].resize(sample_count);
654 cv_lables_buf[ti].resize(sample_count);
655 sample_idx_buf[ti].resize(sample_count);
669 cvReleaseMat( &var_type0 );
670 cvReleaseMat( &sample_indices );
671 cvReleaseMat( &tmp_map );
674 void CvDTreeTrainData::do_responses_copy()
676 responses_copy = cvCreateMat( responses->rows, responses->cols, responses->type );
677 cvCopy( responses, responses_copy);
678 responses = responses_copy;
681 CvDTreeNode* CvDTreeTrainData::subsample_data( const CvMat* _subsample_idx )
683 CvDTreeNode* root = 0;
684 CvMat* isubsample_idx = 0;
685 CvMat* subsample_co = 0;
687 CV_FUNCNAME( "CvDTreeTrainData::subsample_data" );
692 CV_ERROR( CV_StsError, "No training data has been set" );
695 CV_CALL( isubsample_idx = cvPreprocessIndexArray( _subsample_idx, sample_count ));
697 if( !isubsample_idx )
699 // make a copy of the root node
702 root = new_node( 0, 1, 0, 0 );
705 root->num_valid = temp.num_valid;
706 if( root->num_valid )
708 for( i = 0; i < var_count; i++ )
709 root->num_valid[i] = data_root->num_valid[i];
711 root->cv_Tn = temp.cv_Tn;
712 root->cv_node_risk = temp.cv_node_risk;
713 root->cv_node_error = temp.cv_node_error;
717 int* sidx = isubsample_idx->data.i;
718 // co - array of count/offset pairs (to handle duplicated values in _subsample_idx)
719 int* co, cur_ofs = 0;
721 int work_var_count = get_work_var_count();
722 int count = isubsample_idx->rows + isubsample_idx->cols - 1;
724 root = new_node( 0, count, 1, 0 );
726 CV_CALL( subsample_co = cvCreateMat( 1, sample_count*2, CV_32SC1 ));
727 cvZero( subsample_co );
728 co = subsample_co->data.i;
729 for( i = 0; i < count; i++ )
731 for( i = 0; i < sample_count; i++ )
742 for( vi = 0; vi < work_var_count; vi++ )
744 int ci = get_var_type(vi);
746 if( ci >= 0 || vi >= var_count )
748 int* src_buf = get_pred_int_buf();
752 get_cat_var_data( data_root, vi, src_buf, &src );
756 unsigned short* udst = (unsigned short*)(buf->data.s + root->buf_idx*buf->cols +
757 vi*sample_count + root->offset);
758 for( i = 0; i < count; i++ )
760 int val = src[sidx[i]];
761 udst[i] = (unsigned short)val;
762 num_valid += val >= 0;
767 int* idst = buf->data.i + root->buf_idx*buf->cols +
768 vi*sample_count + root->offset;
769 for( i = 0; i < count; i++ )
771 int val = src[sidx[i]];
773 num_valid += val >= 0;
778 root->set_num_valid(vi, num_valid);
782 int *src_idx_buf = get_pred_int_buf();
783 const int* src_idx = 0;
784 float *src_val_buf = get_pred_float_buf();
785 const float* src_val = 0;
786 int j = 0, idx, count_i;
787 int num_valid = data_root->get_num_valid(vi);
789 get_ord_var_data( data_root, vi, src_val_buf, src_idx_buf, &src_val, &src_idx );
792 unsigned short* udst_idx = (unsigned short*)(buf->data.s + root->buf_idx*buf->cols +
793 vi*sample_count + data_root->offset);
794 for( i = 0; i < num_valid; i++ )
799 for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ )
800 udst_idx[j] = (unsigned short)cur_ofs;
803 root->set_num_valid(vi, j);
805 for( ; i < sample_count; i++ )
810 for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ )
811 udst_idx[j] = (unsigned short)cur_ofs;
816 int* idst_idx = buf->data.i + root->buf_idx*buf->cols +
817 vi*sample_count + root->offset;
818 for( i = 0; i < num_valid; i++ )
823 for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ )
824 idst_idx[j] = cur_ofs;
827 root->set_num_valid(vi, j);
829 for( ; i < sample_count; i++ )
834 for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ )
835 idst_idx[j] = cur_ofs;
840 // sample indices subsampling
841 int* sample_idx_src_buf = get_sample_idx_buf();
842 const int* sample_idx_src = 0;
843 get_sample_indices(data_root, sample_idx_src_buf, &sample_idx_src);
846 unsigned short* sample_idx_dst = (unsigned short*)(buf->data.s + root->buf_idx*buf->cols +
847 get_work_var_count()*sample_count + root->offset);
848 for (i = 0; i < count; i++)
849 sample_idx_dst[i] = (unsigned short)sample_idx_src[sidx[i]];
853 int* sample_idx_dst = buf->data.i + root->buf_idx*buf->cols +
854 get_work_var_count()*sample_count + root->offset;
855 for (i = 0; i < count; i++)
856 sample_idx_dst[i] = sample_idx_src[sidx[i]];
862 cvReleaseMat( &isubsample_idx );
863 cvReleaseMat( &subsample_co );
869 void CvDTreeTrainData::get_vectors( const CvMat* _subsample_idx,
870 float* values, uchar* missing,
871 float* responses, bool get_class_idx )
873 CvMat* subsample_idx = 0;
874 CvMat* subsample_co = 0;
876 CV_FUNCNAME( "CvDTreeTrainData::get_vectors" );
880 int i, vi, total = sample_count, count = total, cur_ofs = 0;
886 CV_CALL( subsample_idx = cvPreprocessIndexArray( _subsample_idx, sample_count ));
887 sidx = subsample_idx->data.i;
888 CV_CALL( subsample_co = cvCreateMat( 1, sample_count*2, CV_32SC1 ));
889 co = subsample_co->data.i;
890 cvZero( subsample_co );
891 count = subsample_idx->cols + subsample_idx->rows - 1;
892 for( i = 0; i < count; i++ )
894 for( i = 0; i < total; i++ )
896 int count_i = co[i*2];
899 co[i*2+1] = cur_ofs*var_count;
906 memset( missing, 1, count*var_count );
908 for( vi = 0; vi < var_count; vi++ )
910 int ci = get_var_type(vi);
911 if( ci >= 0 ) // categorical
913 float* dst = values + vi;
914 uchar* m = missing ? missing + vi : 0;
915 int* src_buf = get_pred_int_buf();
917 get_cat_var_data(data_root, vi, src_buf, &src);
919 for( i = 0; i < count; i++, dst += var_count )
921 int idx = sidx ? sidx[i] : i;
926 *m = (!is_buf_16u && val < 0) || (is_buf_16u && (val == 65535));
933 float* dst = values + vi;
934 uchar* m = missing ? missing + vi : 0;
935 int count1 = data_root->get_num_valid(vi);
936 float *src_val_buf = get_pred_float_buf();
937 const float *src_val = 0;
938 int* src_idx_buf = get_pred_int_buf();
939 const int* src_idx = 0;
940 get_ord_var_data(data_root, vi, src_val_buf, src_idx_buf, &src_val, &src_idx);
942 for( i = 0; i < count1; i++ )
944 int idx = src_idx[i];
949 cur_ofs = co[idx*2+1];
952 cur_ofs = idx*var_count;
955 float val = src_val[i];
956 for( ; count_i > 0; count_i--, cur_ofs += var_count )
972 int* src_buf = get_resp_int_buf();
974 get_class_labels(data_root, src_buf, &src);
975 for( i = 0; i < count; i++ )
977 int idx = sidx ? sidx[i] : i;
978 int val = get_class_idx ? src[idx] :
979 cat_map->data.i[cat_ofs->data.i[cat_var_count]+src[idx]];
980 responses[i] = (float)val;
985 float *_values_buf = get_resp_float_buf();
986 const float* _values = 0;
987 get_ord_responses(data_root, _values_buf, &_values);
988 for( i = 0; i < count; i++ )
990 int idx = sidx ? sidx[i] : i;
991 responses[i] = _values[idx];
998 cvReleaseMat( &subsample_idx );
999 cvReleaseMat( &subsample_co );
1003 CvDTreeNode* CvDTreeTrainData::new_node( CvDTreeNode* parent, int count,
1004 int storage_idx, int offset )
1006 CvDTreeNode* node = (CvDTreeNode*)cvSetNew( node_heap );
1008 node->sample_count = count;
1009 node->depth = parent ? parent->depth + 1 : 0;
1010 node->parent = parent;
1011 node->left = node->right = 0;
1014 node->class_idx = 0;
1017 node->buf_idx = storage_idx;
1018 node->offset = offset;
1020 node->num_valid = (int*)cvSetNew( nv_heap );
1022 node->num_valid = 0;
1023 node->alpha = node->node_risk = node->tree_risk = node->tree_error = 0.;
1024 node->complexity = 0;
1026 if( params.cv_folds > 0 && cv_heap )
1028 int cv_n = params.cv_folds;
1030 node->cv_Tn = (int*)cvSetNew( cv_heap );
1031 node->cv_node_risk = (double*)cvAlignPtr(node->cv_Tn + cv_n, sizeof(double));
1032 node->cv_node_error = node->cv_node_risk + cv_n;
1038 node->cv_node_risk = 0;
1039 node->cv_node_error = 0;
1046 CvDTreeSplit* CvDTreeTrainData::new_split_ord( int vi, float cmp_val,
1047 int split_point, int inversed, float quality )
1049 CvDTreeSplit* split = (CvDTreeSplit*)cvSetNew( split_heap );
1050 split->var_idx = vi;
1051 split->condensed_idx = INT_MIN;
1052 split->ord.c = cmp_val;
1053 split->ord.split_point = split_point;
1054 split->inversed = inversed;
1055 split->quality = quality;
1062 CvDTreeSplit* CvDTreeTrainData::new_split_cat( int vi, float quality )
1064 CvDTreeSplit* split = (CvDTreeSplit*)cvSetNew( split_heap );
1065 int i, n = (max_c_count + 31)/32;
1067 split->var_idx = vi;
1068 split->condensed_idx = INT_MIN;
1069 split->inversed = 0;
1070 split->quality = quality;
1071 for( i = 0; i < n; i++ )
1072 split->subset[i] = 0;
1079 void CvDTreeTrainData::free_node( CvDTreeNode* node )
1081 CvDTreeSplit* split = node->split;
1082 free_node_data( node );
1085 CvDTreeSplit* next = split->next;
1086 cvSetRemoveByPtr( split_heap, split );
1090 cvSetRemoveByPtr( node_heap, node );
1094 void CvDTreeTrainData::free_node_data( CvDTreeNode* node )
1096 if( node->num_valid )
1098 cvSetRemoveByPtr( nv_heap, node->num_valid );
1099 node->num_valid = 0;
1101 // do not free cv_* fields, as all the cross-validation related data is released at once.
1105 void CvDTreeTrainData::free_train_data()
1107 cvReleaseMat( &counts );
1108 cvReleaseMat( &buf );
1109 cvReleaseMat( &direction );
1110 cvReleaseMat( &split_buf );
1111 cvReleaseMemStorage( &temp_storage );
1112 cvReleaseMat( &responses_copy );
1113 pred_float_buf.clear();
1114 pred_int_buf.clear();
1115 resp_float_buf.clear();
1116 resp_int_buf.clear();
1117 cv_lables_buf.clear();
1118 sample_idx_buf.clear();
1120 cv_heap = nv_heap = 0;
1124 void CvDTreeTrainData::clear()
1128 cvReleaseMemStorage( &tree_storage );
1130 cvReleaseMat( &var_idx );
1131 cvReleaseMat( &var_type );
1132 cvReleaseMat( &cat_count );
1133 cvReleaseMat( &cat_ofs );
1134 cvReleaseMat( &cat_map );
1135 cvReleaseMat( &priors );
1136 cvReleaseMat( &priors_mult );
1138 node_heap = split_heap = 0;
1140 sample_count = var_all = var_count = max_c_count = ord_var_count = cat_var_count = 0;
1141 have_labels = have_priors = is_classifier = false;
1143 buf_count = buf_size = 0;
1152 int CvDTreeTrainData::get_num_classes() const
1154 return is_classifier ? cat_count->data.i[cat_var_count] : 0;
1158 int CvDTreeTrainData::get_var_type(int vi) const
1160 return var_type->data.i[vi];
1163 int CvDTreeTrainData::get_ord_var_data( CvDTreeNode* n, int vi, float* ord_values_buf, int* indices_buf, const float** ord_values, const int** indices )
1165 int vidx = var_idx ? var_idx->data.i[vi] : vi;
1166 int node_sample_count = n->sample_count;
1167 int* sample_indices_buf = get_sample_idx_buf();
1168 const int* sample_indices = 0;
1169 int td_step = train_data->step/CV_ELEM_SIZE(train_data->type);
1171 get_sample_indices(n, sample_indices_buf, &sample_indices);
1174 *indices = buf->data.i + n->buf_idx*buf->cols +
1175 vi*sample_count + n->offset;
1177 const unsigned short* short_indices = (const unsigned short*)(buf->data.s + n->buf_idx*buf->cols +
1178 vi*sample_count + n->offset );
1179 for( int i = 0; i < node_sample_count; i++ )
1180 indices_buf[i] = short_indices[i];
1181 *indices = indices_buf;
1184 if( tflag == CV_ROW_SAMPLE )
1186 for( int i = 0; i < node_sample_count &&
1187 ((((*indices)[i] >= 0) && !is_buf_16u) || (((*indices)[i] != 65535) && is_buf_16u)); i++ )
1189 int idx = (*indices)[i];
1190 idx = sample_indices[idx];
1191 ord_values_buf[i] = *(train_data->data.fl + idx * td_step + vidx);
1195 for( int i = 0; i < node_sample_count &&
1196 ((((*indices)[i] >= 0) && !is_buf_16u) || (((*indices)[i] != 65535) && is_buf_16u)); i++ )
1198 int idx = (*indices)[i];
1199 idx = sample_indices[idx];
1200 ord_values_buf[i] = *(train_data->data.fl + vidx* td_step + idx);
1203 *ord_values = ord_values_buf;
1204 return 0; //TODO: return the number of non-missing values
1208 void CvDTreeTrainData::get_class_labels( CvDTreeNode* n, int* labels_buf, const int** labels )
1211 get_cat_var_data( n, var_count, labels_buf, labels );
1214 void CvDTreeTrainData::get_sample_indices( CvDTreeNode* n, int* indices_buf, const int** indices )
1216 get_cat_var_data( n, get_work_var_count(), indices_buf, indices );
1219 void CvDTreeTrainData::get_ord_responses( CvDTreeNode* n, float* values_buf, const float** values)
1221 int sample_count = n->sample_count;
1222 int* indices_buf = get_sample_idx_buf();
1223 const int* indices = 0;
1225 int r_step = responses->step/CV_ELEM_SIZE(responses->type);
1227 get_sample_indices(n, indices_buf, &indices);
1230 for( int i = 0; i < sample_count &&
1231 (((indices[i] >= 0) && !is_buf_16u) || ((indices[i] != 65535) && is_buf_16u)); i++ )
1233 int idx = indices[i];
1234 values_buf[i] = *(responses->data.fl + idx * r_step);
1237 *values = values_buf;
1241 void CvDTreeTrainData::get_cv_labels( CvDTreeNode* n, int* labels_buf, const int** labels )
1244 get_cat_var_data( n, get_work_var_count()- 1, labels_buf, labels );
1248 int CvDTreeTrainData::get_cat_var_data( CvDTreeNode* n, int vi, int* cat_values_buf, const int** cat_values )
1251 *cat_values = buf->data.i + n->buf_idx*buf->cols +
1252 vi*sample_count + n->offset;
1254 const unsigned short* short_values = (const unsigned short*)(buf->data.s + n->buf_idx*buf->cols +
1255 vi*sample_count + n->offset);
1256 for( int i = 0; i < n->sample_count; i++ )
1257 cat_values_buf[i] = short_values[i];
1258 *cat_values = cat_values_buf;
1261 return 0; //TODO: return the number of non-missing values
1265 int CvDTreeTrainData::get_child_buf_idx( CvDTreeNode* n )
1267 int idx = n->buf_idx + 1;
1268 if( idx >= buf_count )
1269 idx = shared ? 1 : 0;
1274 void CvDTreeTrainData::write_params( CvFileStorage* fs ) const
1276 CV_FUNCNAME( "CvDTreeTrainData::write_params" );
1280 int vi, vcount = var_count;
1282 cvWriteInt( fs, "is_classifier", is_classifier ? 1 : 0 );
1283 cvWriteInt( fs, "var_all", var_all );
1284 cvWriteInt( fs, "var_count", var_count );
1285 cvWriteInt( fs, "ord_var_count", ord_var_count );
1286 cvWriteInt( fs, "cat_var_count", cat_var_count );
1288 cvStartWriteStruct( fs, "training_params", CV_NODE_MAP );
1289 cvWriteInt( fs, "use_surrogates", params.use_surrogates ? 1 : 0 );
1293 cvWriteInt( fs, "max_categories", params.max_categories );
1297 cvWriteReal( fs, "regression_accuracy", params.regression_accuracy );
1300 cvWriteInt( fs, "max_depth", params.max_depth );
1301 cvWriteInt( fs, "min_sample_count", params.min_sample_count );
1302 cvWriteInt( fs, "cross_validation_folds", params.cv_folds );
1304 if( params.cv_folds > 1 )
1306 cvWriteInt( fs, "use_1se_rule", params.use_1se_rule ? 1 : 0 );
1307 cvWriteInt( fs, "truncate_pruned_tree", params.truncate_pruned_tree ? 1 : 0 );
1311 cvWrite( fs, "priors", priors );
1313 cvEndWriteStruct( fs );
1316 cvWrite( fs, "var_idx", var_idx );
1318 cvStartWriteStruct( fs, "var_type", CV_NODE_SEQ+CV_NODE_FLOW );
1320 for( vi = 0; vi < vcount; vi++ )
1321 cvWriteInt( fs, 0, var_type->data.i[vi] >= 0 );
1323 cvEndWriteStruct( fs );
1325 if( cat_count && (cat_var_count > 0 || is_classifier) )
1327 CV_ASSERT( cat_count != 0 );
1328 cvWrite( fs, "cat_count", cat_count );
1329 cvWrite( fs, "cat_map", cat_map );
1336 void CvDTreeTrainData::read_params( CvFileStorage* fs, CvFileNode* node )
1338 CV_FUNCNAME( "CvDTreeTrainData::read_params" );
1342 CvFileNode *tparams_node, *vartype_node;
1344 int vi, max_split_size, tree_block_size;
1346 is_classifier = (cvReadIntByName( fs, node, "is_classifier" ) != 0);
1347 var_all = cvReadIntByName( fs, node, "var_all" );
1348 var_count = cvReadIntByName( fs, node, "var_count", var_all );
1349 cat_var_count = cvReadIntByName( fs, node, "cat_var_count" );
1350 ord_var_count = cvReadIntByName( fs, node, "ord_var_count" );
1352 tparams_node = cvGetFileNodeByName( fs, node, "training_params" );
1354 if( tparams_node ) // training parameters are not necessary
1356 params.use_surrogates = cvReadIntByName( fs, tparams_node, "use_surrogates", 1 ) != 0;
1360 params.max_categories = cvReadIntByName( fs, tparams_node, "max_categories" );
1364 params.regression_accuracy =
1365 (float)cvReadRealByName( fs, tparams_node, "regression_accuracy" );
1368 params.max_depth = cvReadIntByName( fs, tparams_node, "max_depth" );
1369 params.min_sample_count = cvReadIntByName( fs, tparams_node, "min_sample_count" );
1370 params.cv_folds = cvReadIntByName( fs, tparams_node, "cross_validation_folds" );
1372 if( params.cv_folds > 1 )
1374 params.use_1se_rule = cvReadIntByName( fs, tparams_node, "use_1se_rule" ) != 0;
1375 params.truncate_pruned_tree =
1376 cvReadIntByName( fs, tparams_node, "truncate_pruned_tree" ) != 0;
1379 priors = (CvMat*)cvReadByName( fs, tparams_node, "priors" );
1382 if( !CV_IS_MAT(priors) )
1383 CV_ERROR( CV_StsParseError, "priors must stored as a matrix" );
1384 priors_mult = cvCloneMat( priors );
1388 CV_CALL( var_idx = (CvMat*)cvReadByName( fs, node, "var_idx" ));
1391 if( !CV_IS_MAT(var_idx) ||
1392 (var_idx->cols != 1 && var_idx->rows != 1) ||
1393 var_idx->cols + var_idx->rows - 1 != var_count ||
1394 CV_MAT_TYPE(var_idx->type) != CV_32SC1 )
1395 CV_ERROR( CV_StsParseError,
1396 "var_idx (if exist) must be valid 1d integer vector containing <var_count> elements" );
1398 for( vi = 0; vi < var_count; vi++ )
1399 if( (unsigned)var_idx->data.i[vi] >= (unsigned)var_all )
1400 CV_ERROR( CV_StsOutOfRange, "some of var_idx elements are out of range" );
1403 ////// read var type
1404 CV_CALL( var_type = cvCreateMat( 1, var_count + 2, CV_32SC1 ));
1408 vartype_node = cvGetFileNodeByName( fs, node, "var_type" );
1410 if( vartype_node && CV_NODE_TYPE(vartype_node->tag) == CV_NODE_INT && var_count == 1 )
1411 var_type->data.i[0] = vartype_node->data.i ? cat_var_count++ : ord_var_count--;
1414 if( !vartype_node || CV_NODE_TYPE(vartype_node->tag) != CV_NODE_SEQ ||
1415 vartype_node->data.seq->total != var_count )
1416 CV_ERROR( CV_StsParseError, "var_type must exist and be a sequence of 0's and 1's" );
1418 cvStartReadSeq( vartype_node->data.seq, &reader );
1420 for( vi = 0; vi < var_count; vi++ )
1422 CvFileNode* n = (CvFileNode*)reader.ptr;
1423 if( CV_NODE_TYPE(n->tag) != CV_NODE_INT || (n->data.i & ~1) )
1424 CV_ERROR( CV_StsParseError, "var_type must exist and be a sequence of 0's and 1's" );
1425 var_type->data.i[vi] = n->data.i ? cat_var_count++ : ord_var_count--;
1426 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
1429 var_type->data.i[var_count] = cat_var_count;
1431 ord_var_count = ~ord_var_count;
1432 if( cat_var_count != cat_var_count || ord_var_count != ord_var_count )
1433 CV_ERROR( CV_StsParseError, "var_type is inconsistent with cat_var_count and ord_var_count" );
1436 if( cat_var_count > 0 || is_classifier )
1438 int ccount, total_c_count = 0;
1439 CV_CALL( cat_count = (CvMat*)cvReadByName( fs, node, "cat_count" ));
1440 CV_CALL( cat_map = (CvMat*)cvReadByName( fs, node, "cat_map" ));
1442 if( !CV_IS_MAT(cat_count) || !CV_IS_MAT(cat_map) ||
1443 (cat_count->cols != 1 && cat_count->rows != 1) ||
1444 CV_MAT_TYPE(cat_count->type) != CV_32SC1 ||
1445 cat_count->cols + cat_count->rows - 1 != cat_var_count + is_classifier ||
1446 (cat_map->cols != 1 && cat_map->rows != 1) ||
1447 CV_MAT_TYPE(cat_map->type) != CV_32SC1 )
1448 CV_ERROR( CV_StsParseError,
1449 "Both cat_count and cat_map must exist and be valid 1d integer vectors of an appropriate size" );
1451 ccount = cat_var_count + is_classifier;
1453 CV_CALL( cat_ofs = cvCreateMat( 1, ccount + 1, CV_32SC1 ));
1454 cat_ofs->data.i[0] = 0;
1457 for( vi = 0; vi < ccount; vi++ )
1459 int val = cat_count->data.i[vi];
1461 CV_ERROR( CV_StsOutOfRange, "some of cat_count elements are out of range" );
1462 max_c_count = MAX( max_c_count, val );
1463 cat_ofs->data.i[vi+1] = total_c_count += val;
1466 if( cat_map->cols + cat_map->rows - 1 != total_c_count )
1467 CV_ERROR( CV_StsBadSize,
1468 "cat_map vector length is not equal to the total number of categories in all categorical vars" );
1471 max_split_size = cvAlign(sizeof(CvDTreeSplit) +
1472 (MAX(0,max_c_count - 33)/32)*sizeof(int),sizeof(void*));
1474 tree_block_size = MAX((int)sizeof(CvDTreeNode)*8, max_split_size);
1475 tree_block_size = MAX(tree_block_size + block_size_delta, min_block_size);
1476 CV_CALL( tree_storage = cvCreateMemStorage( tree_block_size ));
1477 CV_CALL( node_heap = cvCreateSet( 0, sizeof(node_heap[0]),
1478 sizeof(CvDTreeNode), tree_storage ));
1479 CV_CALL( split_heap = cvCreateSet( 0, sizeof(split_heap[0]),
1480 max_split_size, tree_storage ));
1485 float* CvDTreeTrainData::get_pred_float_buf()
1487 return &pred_float_buf[cv::getThreadNum()][0];
1489 int* CvDTreeTrainData::get_pred_int_buf()
1491 return &pred_int_buf[cv::getThreadNum()][0];
1493 float* CvDTreeTrainData::get_resp_float_buf()
1495 return &resp_float_buf[cv::getThreadNum()][0];
1497 int* CvDTreeTrainData::get_resp_int_buf()
1499 return &resp_int_buf[cv::getThreadNum()][0];
1501 int* CvDTreeTrainData::get_cv_lables_buf()
1503 return &cv_lables_buf[cv::getThreadNum()][0];
1505 int* CvDTreeTrainData::get_sample_idx_buf()
1507 return &sample_idx_buf[cv::getThreadNum()][0];
1510 /////////////////////// Decision Tree /////////////////////////
1516 default_model_name = "my_tree";
1522 void CvDTree::clear()
1524 cvReleaseMat( &var_importance );
1534 pruned_tree_idx = -1;
1544 const CvDTreeNode* CvDTree::get_root() const
1550 int CvDTree::get_pruned_tree_idx() const
1552 return pruned_tree_idx;
1556 CvDTreeTrainData* CvDTree::get_data()
1562 bool CvDTree::train( const CvMat* _train_data, int _tflag,
1563 const CvMat* _responses, const CvMat* _var_idx,
1564 const CvMat* _sample_idx, const CvMat* _var_type,
1565 const CvMat* _missing_mask, CvDTreeParams _params )
1567 bool result = false;
1569 CV_FUNCNAME( "CvDTree::train" );
1574 data = new CvDTreeTrainData( _train_data, _tflag, _responses,
1575 _var_idx, _sample_idx, _var_type,
1576 _missing_mask, _params, false );
1577 CV_CALL( result = do_train(0) );
1584 bool CvDTree::train( const Mat& _train_data, int _tflag,
1585 const Mat& _responses, const Mat& _var_idx,
1586 const Mat& _sample_idx, const Mat& _var_type,
1587 const Mat& _missing_mask, CvDTreeParams _params )
1589 CvMat tdata = _train_data, responses = _responses, vidx=_var_idx,
1590 sidx=_sample_idx, vtype=_var_type, mmask=_missing_mask;
1591 return train(&tdata, _tflag, &responses, vidx.data.ptr ? &vidx : 0, sidx.data.ptr ? &sidx : 0,
1592 vtype.data.ptr ? &vtype : 0, mmask.data.ptr ? &mmask : 0, _params);
1596 bool CvDTree::train( CvMLData* _data, CvDTreeParams _params )
1598 bool result = false;
1600 CV_FUNCNAME( "CvDTree::train" );
1604 const CvMat* values = _data->get_values();
1605 const CvMat* response = _data->get_responses();
1606 const CvMat* missing = _data->get_missing();
1607 const CvMat* var_types = _data->get_var_types();
1608 const CvMat* train_sidx = _data->get_train_sample_idx();
1609 const CvMat* var_idx = _data->get_var_idx();
1611 CV_CALL( result = train( values, CV_ROW_SAMPLE, response, var_idx,
1612 train_sidx, var_types, missing, _params ) );
1619 bool CvDTree::train( CvDTreeTrainData* _data, const CvMat* _subsample_idx )
1621 bool result = false;
1623 CV_FUNCNAME( "CvDTree::train" );
1629 data->shared = true;
1630 CV_CALL( result = do_train(_subsample_idx));
1638 bool CvDTree::do_train( const CvMat* _subsample_idx )
1640 bool result = false;
1642 CV_FUNCNAME( "CvDTree::do_train" );
1646 root = data->subsample_data( _subsample_idx );
1648 CV_CALL( try_split_node(root));
1650 if( data->params.cv_folds > 0 )
1651 CV_CALL( prune_cv());
1654 data->free_train_data();
1664 void CvDTree::try_split_node( CvDTreeNode* node )
1666 CvDTreeSplit* best_split = 0;
1667 int i, n = node->sample_count, vi;
1668 bool can_split = true;
1669 double quality_scale;
1671 calc_node_value( node );
1673 if( node->sample_count <= data->params.min_sample_count ||
1674 node->depth >= data->params.max_depth )
1677 if( can_split && data->is_classifier )
1679 // check if we have a "pure" node,
1680 // we assume that cls_count is filled by calc_node_value()
1681 int* cls_count = data->counts->data.i;
1682 int nz = 0, m = data->get_num_classes();
1683 for( i = 0; i < m; i++ )
1684 nz += cls_count[i] != 0;
1685 if( nz == 1 ) // there is only one class
1688 else if( can_split )
1690 if( sqrt(node->node_risk)/n < data->params.regression_accuracy )
1696 best_split = find_best_split(node);
1697 // TODO: check the split quality ...
1698 node->split = best_split;
1700 if( !can_split || !best_split )
1702 data->free_node_data(node);
1706 quality_scale = calc_node_dir( node );
1707 if( data->params.use_surrogates )
1709 // find all the surrogate splits
1710 // and sort them by their similarity to the primary one
1711 for( vi = 0; vi < data->var_count; vi++ )
1713 CvDTreeSplit* split;
1714 int ci = data->get_var_type(vi);
1716 if( vi == best_split->var_idx )
1720 split = find_surrogate_split_cat( node, vi );
1722 split = find_surrogate_split_ord( node, vi );
1727 CvDTreeSplit* prev_split = node->split;
1728 split->quality = (float)(split->quality*quality_scale);
1730 while( prev_split->next &&
1731 prev_split->next->quality > split->quality )
1732 prev_split = prev_split->next;
1733 split->next = prev_split->next;
1734 prev_split->next = split;
1738 split_node_data( node );
1739 try_split_node( node->left );
1740 try_split_node( node->right );
1744 // calculate direction (left(-1),right(1),missing(0))
1745 // for each sample using the best split
1746 // the function returns scale coefficients for surrogate split quality factors.
1747 // the scale is applied to normalize surrogate split quality relatively to the
1748 // best (primary) split quality. That is, if a surrogate split is absolutely
1749 // identical to the primary split, its quality will be set to the maximum value =
1750 // quality of the primary split; otherwise, it will be lower.
1751 // besides, the function compute node->maxlr,
1752 // minimum possible quality (w/o considering the above mentioned scale)
1753 // for a surrogate split. Surrogate splits with quality less than node->maxlr
1754 // are not discarded.
1755 double CvDTree::calc_node_dir( CvDTreeNode* node )
1757 char* dir = (char*)data->direction->data.ptr;
1758 int i, n = node->sample_count, vi = node->split->var_idx;
1761 assert( !node->split->inversed );
1763 if( data->get_var_type(vi) >= 0 ) // split on categorical var
1765 int* labels_buf = data->get_pred_int_buf();
1766 const int* labels = 0;
1767 const int* subset = node->split->subset;
1768 data->get_cat_var_data( node, vi, labels_buf, &labels );
1769 if( !data->have_priors )
1771 int sum = 0, sum_abs = 0;
1773 for( i = 0; i < n; i++ )
1775 int idx = labels[i];
1776 int d = ( ((idx >= 0)&&(!data->is_buf_16u)) || ((idx != 65535)&&(data->is_buf_16u)) ) ?
1777 CV_DTREE_CAT_DIR(idx,subset) : 0;
1778 sum += d; sum_abs += d & 1;
1782 R = (sum_abs + sum) >> 1;
1783 L = (sum_abs - sum) >> 1;
1787 const double* priors = data->priors_mult->data.db;
1788 double sum = 0, sum_abs = 0;
1789 int *responses_buf = data->get_resp_int_buf();
1790 const int* responses;
1791 data->get_class_labels(node, responses_buf, &responses);
1793 for( i = 0; i < n; i++ )
1795 int idx = labels[i];
1796 double w = priors[responses[i]];
1797 int d = idx >= 0 ? CV_DTREE_CAT_DIR(idx,subset) : 0;
1798 sum += d*w; sum_abs += (d & 1)*w;
1802 R = (sum_abs + sum) * 0.5;
1803 L = (sum_abs - sum) * 0.5;
1806 else // split on ordered var
1808 int split_point = node->split->ord.split_point;
1809 int n1 = node->get_num_valid(vi);
1811 float* val_buf = data->get_pred_float_buf();
1812 const float* val = 0;
1813 int* sorted_buf = data->get_pred_int_buf();
1814 const int* sorted = 0;
1815 data->get_ord_var_data( node, vi, val_buf, sorted_buf, &val, &sorted);
1817 assert( 0 <= split_point && split_point < n1-1 );
1819 if( !data->have_priors )
1821 for( i = 0; i <= split_point; i++ )
1822 dir[sorted[i]] = (char)-1;
1823 for( ; i < n1; i++ )
1824 dir[sorted[i]] = (char)1;
1826 dir[sorted[i]] = (char)0;
1829 R = n1 - split_point + 1;
1833 const double* priors = data->priors_mult->data.db;
1834 int* responses_buf = data->get_resp_int_buf();
1835 const int* responses = 0;
1836 data->get_class_labels(node, responses_buf, &responses);
1839 for( i = 0; i <= split_point; i++ )
1841 int idx = sorted[i];
1842 double w = priors[responses[idx]];
1843 dir[idx] = (char)-1;
1847 for( ; i < n1; i++ )
1849 int idx = sorted[i];
1850 double w = priors[responses[idx]];
1856 dir[sorted[i]] = (char)0;
1859 node->maxlr = MAX( L, R );
1860 return node->split->quality/(L + R);
1863 CvDTreeSplit* CvDTree::find_best_split( CvDTreeNode* node )
1866 CvDTreeSplit *bestSplit = 0;
1867 int maxNumThreads = 1;
1869 maxNumThreads = cv::getNumThreads();
1871 vector<CvDTreeSplit*> splits(maxNumThreads);
1872 vector<CvDTreeSplit*> bestSplits(maxNumThreads);
1873 vector<int> canSplit(maxNumThreads);
1874 CvDTreeSplit **splitsPtr = &splits[0], ** bestSplitsPtr = &bestSplits[0];
1875 int* canSplitPtr = &canSplit[0];
1876 for (int i = 0; i < maxNumThreads; i++)
1878 splitsPtr[i] = data->new_split_cat( 0, -1.0f );
1879 bestSplitsPtr[i] = data->new_split_cat( 0, -1.0f );
1884 #pragma omp parallel for num_threads(maxNumThreads) schedule(dynamic)
1886 for( vi = 0; vi < data->var_count; vi++ )
1888 CvDTreeSplit *res, *t;
1889 int threadIdx = cv::getThreadNum();
1890 int ci = data->get_var_type(vi);
1891 if( node->get_num_valid(vi) <= 1 )
1894 if( data->is_classifier )
1897 res = find_split_cat_class( node, vi, bestSplitsPtr[threadIdx]->quality, splitsPtr[threadIdx] );
1899 res = find_split_ord_class( node, vi, bestSplitsPtr[threadIdx]->quality, splitsPtr[threadIdx] );
1904 res = find_split_cat_reg( node, vi, bestSplitsPtr[threadIdx]->quality, splitsPtr[threadIdx] );
1906 res = find_split_ord_reg( node, vi, bestSplitsPtr[threadIdx]->quality, splitsPtr[threadIdx] );
1911 canSplitPtr[threadIdx] = 1;
1912 if( bestSplitsPtr[threadIdx]->quality < splitsPtr[threadIdx]->quality )
1913 CV_SWAP( bestSplitsPtr[threadIdx], splitsPtr[threadIdx], t );
1917 for( ; ti < maxNumThreads; ti++ )
1919 if( canSplitPtr[ti] )
1921 bestSplit = bestSplitsPtr[ti];
1925 for( ; ti < maxNumThreads; ti++ )
1927 if( bestSplit->quality < bestSplitsPtr[ti]->quality )
1928 bestSplit = bestSplitsPtr[ti];
1930 for(int i = 0; i < maxNumThreads; i++)
1932 cvSetRemoveByPtr( data->split_heap, splitsPtr[i] );
1933 if( bestSplitsPtr[i] != bestSplit )
1934 cvSetRemoveByPtr( data->split_heap, bestSplitsPtr[i] );
1939 CvDTreeSplit* CvDTree::find_split_ord_class( CvDTreeNode* node, int vi,
1940 float init_quality, CvDTreeSplit* _split )
1942 const float epsilon = FLT_EPSILON*2;
1943 int n = node->sample_count;
1944 int n1 = node->get_num_valid(vi);
1945 int m = data->get_num_classes();
1947 float* values_buf = data->get_pred_float_buf();
1948 const float* values = 0;
1949 int* indices_buf = data->get_pred_int_buf();
1950 const int* indices = 0;
1951 data->get_ord_var_data( node, vi, values_buf, indices_buf, &values, &indices );
1952 int* responses_buf = data->get_resp_int_buf();
1953 const int* responses = 0;
1954 data->get_class_labels( node, responses_buf, &responses );
1956 const int* rc0 = data->counts->data.i;
1957 int* lc = (int*)cvStackAlloc(m*sizeof(lc[0]));
1958 int* rc = (int*)cvStackAlloc(m*sizeof(rc[0]));
1960 double lsum2 = 0, rsum2 = 0, best_val = init_quality;
1961 const double* priors = data->have_priors ? data->priors_mult->data.db : 0;
1963 // init arrays of class instance counters on both sides of the split
1964 for( i = 0; i < m; i++ )
1970 // compensate for missing values
1971 for( i = n1; i < n; i++ )
1973 rc[responses[indices[i]]]--;
1980 for( i = 0; i < m; i++ )
1981 rsum2 += (double)rc[i]*rc[i];
1983 for( i = 0; i < n1 - 1; i++ )
1985 int idx = responses[indices[i]];
1988 lv = lc[idx]; rv = rc[idx];
1991 lc[idx] = lv + 1; rc[idx] = rv - 1;
1993 if( values[i] + epsilon < values[i+1] )
1995 double val = (lsum2*R + rsum2*L)/((double)L*R);
1996 if( best_val < val )
2006 double L = 0, R = 0;
2007 for( i = 0; i < m; i++ )
2009 double wv = rc[i]*priors[i];
2014 for( i = 0; i < n1 - 1; i++ )
2016 int idx = responses[indices[i]];
2018 double p = priors[idx], p2 = p*p;
2020 lv = lc[idx]; rv = rc[idx];
2021 lsum2 += p2*(lv*2 + 1);
2022 rsum2 -= p2*(rv*2 - 1);
2023 lc[idx] = lv + 1; rc[idx] = rv - 1;
2025 if( values[i] + epsilon < values[i+1] )
2027 double val = (lsum2*R + rsum2*L)/((double)L*R);
2028 if( best_val < val )
2037 CvDTreeSplit* split = 0;
2040 split = _split ? _split : data->new_split_ord( 0, 0.0f, 0, 0, 0.0f );
2041 split->var_idx = vi;
2042 split->ord.c = (values[best_i] + values[best_i+1])*0.5f;
2043 split->ord.split_point = best_i;
2044 split->inversed = 0;
2045 split->quality = (float)best_val;
2051 void CvDTree::cluster_categories( const int* vectors, int n, int m,
2052 int* csums, int k, int* labels )
2054 // TODO: consider adding priors (class weights) and sample weights to the clustering algorithm
2055 int iters = 0, max_iters = 100;
2057 double* buf = (double*)cvStackAlloc( (n + k)*sizeof(buf[0]) );
2058 double *v_weights = buf, *c_weights = buf + k;
2059 bool modified = true;
2060 CvRNG* r = &data->rng;
2062 // assign labels randomly
2063 for( i = idx = 0; i < n; i++ )
2066 const int* v = vectors + i*m;
2068 idx &= idx < k ? -1 : 0;
2070 // compute weight of each vector
2071 for( j = 0; j < m; j++ )
2073 v_weights[i] = sum ? 1./sum : 0.;
2076 for( i = 0; i < n; i++ )
2078 int i1 = cvRandInt(r) % n;
2079 int i2 = cvRandInt(r) % n;
2080 CV_SWAP( labels[i1], labels[i2], j );
2083 for( iters = 0; iters <= max_iters; iters++ )
2086 for( i = 0; i < k; i++ )
2088 for( j = 0; j < m; j++ )
2092 for( i = 0; i < n; i++ )
2094 const int* v = vectors + i*m;
2095 int* s = csums + labels[i]*m;
2096 for( j = 0; j < m; j++ )
2100 // exit the loop here, when we have up-to-date csums
2101 if( iters == max_iters || !modified )
2106 // calculate weight of each cluster
2107 for( i = 0; i < k; i++ )
2109 const int* s = csums + i*m;
2111 for( j = 0; j < m; j++ )
2113 c_weights[i] = sum ? 1./sum : 0;
2116 // now for each vector determine the closest cluster
2117 for( i = 0; i < n; i++ )
2119 const int* v = vectors + i*m;
2120 double alpha = v_weights[i];
2121 double min_dist2 = DBL_MAX;
2124 for( idx = 0; idx < k; idx++ )
2126 const int* s = csums + idx*m;
2127 double dist2 = 0., beta = c_weights[idx];
2128 for( j = 0; j < m; j++ )
2130 double t = v[j]*alpha - s[j]*beta;
2133 if( min_dist2 > dist2 )
2140 if( min_idx != labels[i] )
2142 labels[i] = min_idx;
2148 CvDTreeSplit* CvDTree::find_split_cat_class( CvDTreeNode* node, int vi, float init_quality, CvDTreeSplit* _split )
2150 int ci = data->get_var_type(vi);
2151 int n = node->sample_count;
2152 int m = data->get_num_classes();
2153 int _mi = data->cat_count->data.i[ci], mi = _mi;
2155 int* labels_buf = data->get_pred_int_buf();
2156 const int* labels = 0;
2157 data->get_cat_var_data(node, vi, labels_buf, &labels);
2158 int *responses_buf = data->get_resp_int_buf();
2159 const int* responses = 0;
2160 data->get_class_labels(node, responses_buf, &responses);
2162 int* lc = (int*)cvStackAlloc(m*sizeof(lc[0]));
2163 int* rc = (int*)cvStackAlloc(m*sizeof(rc[0]));
2164 int* _cjk = (int*)cvStackAlloc(m*(mi+1)*sizeof(_cjk[0]))+m, *cjk = _cjk;
2165 double* c_weights = (double*)cvStackAlloc( mi*sizeof(c_weights[0]) );
2166 int* cluster_labels = 0;
2169 double L = 0, R = 0;
2170 double best_val = init_quality;
2171 int prevcode = 0, best_subset = -1, subset_i, subset_n, subtract = 0;
2172 const double* priors = data->priors_mult->data.db;
2174 // init array of counters:
2175 // c_{jk} - number of samples that have vi-th input variable = j and response = k.
2176 for( j = -1; j < mi; j++ )
2177 for( k = 0; k < m; k++ )
2180 for( i = 0; i < n; i++ )
2182 j = ( labels[i] == 65535 && data->is_buf_16u) ? -1 : labels[i];
2189 if( mi > data->params.max_categories )
2191 mi = MIN(data->params.max_categories, n);
2193 cluster_labels = (int*)cvStackAlloc(mi*sizeof(cluster_labels[0]));
2194 cluster_categories( _cjk, _mi, m, cjk, mi, cluster_labels );
2202 int_ptr = (int**)cvStackAlloc( mi*sizeof(int_ptr[0]) );
2203 for( j = 0; j < mi; j++ )
2204 int_ptr[j] = cjk + j*2 + 1;
2205 icvSortIntPtr( int_ptr, mi, 0 );
2210 for( k = 0; k < m; k++ )
2213 for( j = 0; j < mi; j++ )
2214 sum += cjk[j*m + k];
2219 for( j = 0; j < mi; j++ )
2222 for( k = 0; k < m; k++ )
2223 sum += cjk[j*m + k]*priors[k];
2228 for( ; subset_i < subset_n; subset_i++ )
2232 double lsum2 = 0, rsum2 = 0;
2235 idx = (int)(int_ptr[subset_i] - cjk)/2;
2238 int graycode = (subset_i>>1)^subset_i;
2239 int diff = graycode ^ prevcode;
2241 // determine index of the changed bit.
2243 idx = diff >= (1 << 16) ? 16 : 0;
2244 u.f = (float)(((diff >> 16) | diff) & 65535);
2245 idx += (u.i >> 23) - 127;
2246 subtract = graycode < prevcode;
2247 prevcode = graycode;
2251 weight = c_weights[idx];
2252 if( weight < FLT_EPSILON )
2257 for( k = 0; k < m; k++ )
2260 int lval = lc[k] + t;
2261 int rval = rc[k] - t;
2262 double p = priors[k], p2 = p*p;
2263 lsum2 += p2*lval*lval;
2264 rsum2 += p2*rval*rval;
2265 lc[k] = lval; rc[k] = rval;
2272 for( k = 0; k < m; k++ )
2275 int lval = lc[k] - t;
2276 int rval = rc[k] + t;
2277 double p = priors[k], p2 = p*p;
2278 lsum2 += p2*lval*lval;
2279 rsum2 += p2*rval*rval;
2280 lc[k] = lval; rc[k] = rval;
2286 if( L > FLT_EPSILON && R > FLT_EPSILON )
2288 double val = (lsum2*R + rsum2*L)/((double)L*R);
2289 if( best_val < val )
2292 best_subset = subset_i;
2297 CvDTreeSplit* split = 0;
2298 if( best_subset >= 0 )
2300 split = _split ? _split : data->new_split_cat( 0, -1.0f );
2301 split->var_idx = vi;
2302 split->quality = (float)best_val;
2303 memset( split->subset, 0, (data->max_c_count + 31)/32 * sizeof(int));
2306 for( i = 0; i <= best_subset; i++ )
2308 idx = (int)(int_ptr[i] - cjk) >> 1;
2309 split->subset[idx >> 5] |= 1 << (idx & 31);
2314 for( i = 0; i < _mi; i++ )
2316 idx = cluster_labels ? cluster_labels[i] : i;
2317 if( best_subset & (1 << idx) )
2318 split->subset[i >> 5] |= 1 << (i & 31);
2326 CvDTreeSplit* CvDTree::find_split_ord_reg( CvDTreeNode* node, int vi, float init_quality, CvDTreeSplit* _split )
2328 const float epsilon = FLT_EPSILON*2;
2329 int n = node->sample_count;
2330 int n1 = node->get_num_valid(vi);
2332 float* values_buf = data->get_pred_float_buf();
2333 const float* values = 0;
2334 int* indices_buf = data->get_pred_int_buf();
2335 const int* indices = 0;
2336 data->get_ord_var_data( node, vi, values_buf, indices_buf, &values, &indices );
2337 float* responses_buf = data->get_resp_float_buf();
2338 const float* responses = 0;
2339 data->get_ord_responses( node, responses_buf, &responses );
2342 double best_val = init_quality, lsum = 0, rsum = node->value*n;
2345 // compensate for missing values
2346 for( i = n1; i < n; i++ )
2347 rsum -= responses[indices[i]];
2349 // find the optimal split
2350 for( i = 0; i < n1 - 1; i++ )
2352 float t = responses[indices[i]];
2357 if( values[i] + epsilon < values[i+1] )
2359 double val = (lsum*lsum*R + rsum*rsum*L)/((double)L*R);
2360 if( best_val < val )
2368 CvDTreeSplit* split = 0;
2371 split = _split ? _split : data->new_split_ord( 0, 0.0f, 0, 0, 0.0f );
2372 split->var_idx = vi;
2373 split->ord.c = (values[best_i] + values[best_i+1])*0.5f;
2374 split->ord.split_point = best_i;
2375 split->inversed = 0;
2376 split->quality = (float)best_val;
2381 CvDTreeSplit* CvDTree::find_split_cat_reg( CvDTreeNode* node, int vi, float init_quality, CvDTreeSplit* _split )
2383 int ci = data->get_var_type(vi);
2384 int n = node->sample_count;
2385 int mi = data->cat_count->data.i[ci];
2386 int* labels_buf = data->get_pred_int_buf();
2387 const int* labels = 0;
2388 float* responses_buf = data->get_resp_float_buf();
2389 const float* responses = 0;
2390 data->get_cat_var_data(node, vi, labels_buf, &labels);
2391 data->get_ord_responses(node, responses_buf, &responses);
2393 double* sum = (double*)cvStackAlloc( (mi+1)*sizeof(sum[0]) ) + 1;
2394 int* counts = (int*)cvStackAlloc( (mi+1)*sizeof(counts[0]) ) + 1;
2395 double** sum_ptr = (double**)cvStackAlloc( (mi+1)*sizeof(sum_ptr[0]) );
2396 int i, L = 0, R = 0;
2397 double best_val = init_quality, lsum = 0, rsum = 0;
2398 int best_subset = -1, subset_i;
2400 for( i = -1; i < mi; i++ )
2401 sum[i] = counts[i] = 0;
2403 // calculate sum response and weight of each category of the input var
2404 for( i = 0; i < n; i++ )
2406 int idx = ( (labels[i] == 65535) && data->is_buf_16u ) ? -1 : labels[i];
2407 double s = sum[idx] + responses[i];
2408 int nc = counts[idx] + 1;
2413 // calculate average response in each category
2414 for( i = 0; i < mi; i++ )
2418 sum[i] /= MAX(counts[i],1);
2419 sum_ptr[i] = sum + i;
2422 icvSortDblPtr( sum_ptr, mi, 0 );
2424 // revert back to unnormalized sums
2425 // (there should be a very little loss of accuracy)
2426 for( i = 0; i < mi; i++ )
2427 sum[i] *= counts[i];
2429 for( subset_i = 0; subset_i < mi-1; subset_i++ )
2431 int idx = (int)(sum_ptr[subset_i] - sum);
2432 int ni = counts[idx];
2436 double s = sum[idx];
2442 double val = (lsum*lsum*R + rsum*rsum*L)/((double)L*R);
2443 if( best_val < val )
2446 best_subset = subset_i;
2452 CvDTreeSplit* split = 0;
2453 if( best_subset >= 0 )
2455 split = _split ? _split : data->new_split_cat( 0, -1.0f);
2456 split->var_idx = vi;
2457 split->quality = (float)best_val;
2458 memset( split->subset, 0, (data->max_c_count + 31)/32 * sizeof(int));
2459 for( i = 0; i <= best_subset; i++ )
2461 int idx = (int)(sum_ptr[i] - sum);
2462 split->subset[idx >> 5] |= 1 << (idx & 31);
2468 CvDTreeSplit* CvDTree::find_surrogate_split_ord( CvDTreeNode* node, int vi )
2470 const float epsilon = FLT_EPSILON*2;
2471 const char* dir = (char*)data->direction->data.ptr;
2472 int n1 = node->get_num_valid(vi);
2473 float* values_buf = data->get_pred_float_buf();
2474 const float* values = 0;
2475 int* indices_buf = data->get_pred_int_buf();
2476 const int* indices = 0;
2477 data->get_ord_var_data( node, vi, values_buf, indices_buf, &values, &indices );
2478 // LL - number of samples that both the primary and the surrogate splits send to the left
2479 // LR - ... primary split sends to the left and the surrogate split sends to the right
2480 // RL - ... primary split sends to the right and the surrogate split sends to the left
2481 // RR - ... both send to the right
2482 int i, best_i = -1, best_inversed = 0;
2485 if( !data->have_priors )
2487 int LL = 0, RL = 0, LR, RR;
2488 int worst_val = cvFloor(node->maxlr), _best_val = worst_val;
2489 int sum = 0, sum_abs = 0;
2491 for( i = 0; i < n1; i++ )
2493 int d = dir[indices[i]];
2494 sum += d; sum_abs += d & 1;
2497 // sum_abs = R + L; sum = R - L
2498 RR = (sum_abs + sum) >> 1;
2499 LR = (sum_abs - sum) >> 1;
2501 // initially all the samples are sent to the right by the surrogate split,
2502 // LR of them are sent to the left by primary split, and RR - to the right.
2503 // now iteratively compute LL, LR, RL and RR for every possible surrogate split value.
2504 for( i = 0; i < n1 - 1; i++ )
2506 int d = dir[indices[i]];
2511 if( LL + RR > _best_val && values[i] + epsilon < values[i+1] )
2514 best_i = i; best_inversed = 0;
2520 if( RL + LR > _best_val && values[i] + epsilon < values[i+1] )
2523 best_i = i; best_inversed = 1;
2527 best_val = _best_val;
2531 double LL = 0, RL = 0, LR, RR;
2532 double worst_val = node->maxlr;
2533 double sum = 0, sum_abs = 0;
2534 const double* priors = data->priors_mult->data.db;
2535 int* responses_buf = data->get_resp_int_buf();
2536 const int* responses = 0;
2537 data->get_class_labels(node, responses_buf, &responses);
2538 best_val = worst_val;
2540 for( i = 0; i < n1; i++ )
2542 int idx = indices[i];
2543 double w = priors[responses[idx]];
2545 sum += d*w; sum_abs += (d & 1)*w;
2548 // sum_abs = R + L; sum = R - L
2549 RR = (sum_abs + sum)*0.5;
2550 LR = (sum_abs - sum)*0.5;
2552 // initially all the samples are sent to the right by the surrogate split,
2553 // LR of them are sent to the left by primary split, and RR - to the right.
2554 // now iteratively compute LL, LR, RL and RR for every possible surrogate split value.
2555 for( i = 0; i < n1 - 1; i++ )
2557 int idx = indices[i];
2558 double w = priors[responses[idx]];
2564 if( LL + RR > best_val && values[i] + epsilon < values[i+1] )
2567 best_i = i; best_inversed = 0;
2573 if( RL + LR > best_val && values[i] + epsilon < values[i+1] )
2576 best_i = i; best_inversed = 1;
2581 return best_i >= 0 && best_val > node->maxlr ? data->new_split_ord( vi,
2582 (values[best_i] + values[best_i+1])*0.5f, best_i, best_inversed, (float)best_val ) : 0;
2586 CvDTreeSplit* CvDTree::find_surrogate_split_cat( CvDTreeNode* node, int vi )
2588 const char* dir = (char*)data->direction->data.ptr;
2589 int n = node->sample_count;
2590 int* labels_buf = data->get_pred_int_buf();
2591 const int* labels = 0;
2592 data->get_cat_var_data(node, vi, labels_buf, &labels);
2593 // LL - number of samples that both the primary and the surrogate splits send to the left
2594 // LR - ... primary split sends to the left and the surrogate split sends to the right
2595 // RL - ... primary split sends to the right and the surrogate split sends to the left
2596 // RR - ... both send to the right
2597 CvDTreeSplit* split = data->new_split_cat( vi, 0 );
2598 int i, mi = data->cat_count->data.i[data->get_var_type(vi)], l_win = 0;
2599 double best_val = 0;
2600 double* lc = (double*)cvStackAlloc( (mi+1)*2*sizeof(lc[0]) ) + 1;
2601 double* rc = lc + mi + 1;
2603 for( i = -1; i < mi; i++ )
2606 // for each category calculate the weight of samples
2607 // sent to the left (lc) and to the right (rc) by the primary split
2608 if( !data->have_priors )
2610 int* _lc = (int*)cvStackAlloc((mi+2)*2*sizeof(_lc[0])) + 1;
2611 int* _rc = _lc + mi + 1;
2613 for( i = -1; i < mi; i++ )
2614 _lc[i] = _rc[i] = 0;
2616 for( i = 0; i < n; i++ )
2618 int idx = ( (labels[i] == 65535) && (data->is_buf_16u) ) ? -1 : labels[i];
2620 int sum = _lc[idx] + d;
2621 int sum_abs = _rc[idx] + (d & 1);
2622 _lc[idx] = sum; _rc[idx] = sum_abs;
2625 for( i = 0; i < mi; i++ )
2628 int sum_abs = _rc[i];
2629 lc[i] = (sum_abs - sum) >> 1;
2630 rc[i] = (sum_abs + sum) >> 1;
2635 const double* priors = data->priors_mult->data.db;
2636 int* responses_buf = data->get_resp_int_buf();
2637 const int* responses = 0;
2638 data->get_class_labels(node, responses_buf, &responses);
2640 for( i = 0; i < n; i++ )
2642 int idx = ( (labels[i] == 65535) && (data->is_buf_16u) ) ? -1 : labels[i];
2643 double w = priors[responses[i]];
2645 double sum = lc[idx] + d*w;
2646 double sum_abs = rc[idx] + (d & 1)*w;
2647 lc[idx] = sum; rc[idx] = sum_abs;
2650 for( i = 0; i < mi; i++ )
2653 double sum_abs = rc[i];
2654 lc[i] = (sum_abs - sum) * 0.5;
2655 rc[i] = (sum_abs + sum) * 0.5;
2659 // 2. now form the split.
2660 // in each category send all the samples to the same direction as majority
2661 for( i = 0; i < mi; i++ )
2663 double lval = lc[i], rval = rc[i];
2666 split->subset[i >> 5] |= 1 << (i & 31);
2674 split->quality = (float)best_val;
2675 if( split->quality <= node->maxlr || l_win == 0 || l_win == mi )
2676 cvSetRemoveByPtr( data->split_heap, split ), split = 0;
2682 void CvDTree::calc_node_value( CvDTreeNode* node )
2684 int i, j, k, n = node->sample_count, cv_n = data->params.cv_folds;
2685 int* cv_labels_buf = data->get_cv_lables_buf();
2686 const int* cv_labels = 0;
2687 data->get_cv_labels(node, cv_labels_buf, &cv_labels);
2689 if( data->is_classifier )
2691 // in case of classification tree:
2692 // * node value is the label of the class that has the largest weight in the node.
2693 // * node risk is the weighted number of misclassified samples,
2694 // * j-th cross-validation fold value and risk are calculated as above,
2695 // but using the samples with cv_labels(*)!=j.
2696 // * j-th cross-validation fold error is calculated as the weighted number of
2697 // misclassified samples with cv_labels(*)==j.
2699 // compute the number of instances of each class
2700 int* cls_count = data->counts->data.i;
2701 int* responses_buf = data->get_resp_int_buf();
2702 const int* responses = 0;
2703 data->get_class_labels(node, responses_buf, &responses);
2704 int m = data->get_num_classes();
2705 int* cv_cls_count = (int*)cvStackAlloc(m*cv_n*sizeof(cv_cls_count[0]));
2706 double max_val = -1, total_weight = 0;
2708 double* priors = data->priors_mult->data.db;
2710 for( k = 0; k < m; k++ )
2715 for( i = 0; i < n; i++ )
2716 cls_count[responses[i]]++;
2720 for( j = 0; j < cv_n; j++ )
2721 for( k = 0; k < m; k++ )
2722 cv_cls_count[j*m + k] = 0;
2724 for( i = 0; i < n; i++ )
2726 j = cv_labels[i]; k = responses[i];
2727 cv_cls_count[j*m + k]++;
2730 for( j = 0; j < cv_n; j++ )
2731 for( k = 0; k < m; k++ )
2732 cls_count[k] += cv_cls_count[j*m + k];
2735 if( data->have_priors && node->parent == 0 )
2737 // compute priors_mult from priors, take the sample ratio into account.
2739 for( k = 0; k < m; k++ )
2741 int n_k = cls_count[k];
2742 priors[k] = data->priors->data.db[k]*(n_k ? 1./n_k : 0.);
2746 for( k = 0; k < m; k++ )
2750 for( k = 0; k < m; k++ )
2752 double val = cls_count[k]*priors[k];
2753 total_weight += val;
2761 node->class_idx = max_k;
2762 node->value = data->cat_map->data.i[
2763 data->cat_ofs->data.i[data->cat_var_count] + max_k];
2764 node->node_risk = total_weight - max_val;
2766 for( j = 0; j < cv_n; j++ )
2768 double sum_k = 0, sum = 0, max_val_k = 0;
2769 max_val = -1; max_k = -1;
2771 for( k = 0; k < m; k++ )
2773 double w = priors[k];
2774 double val_k = cv_cls_count[j*m + k]*w;
2775 double val = cls_count[k]*w - val_k;
2786 node->cv_Tn[j] = INT_MAX;
2787 node->cv_node_risk[j] = sum - max_val;
2788 node->cv_node_error[j] = sum_k - max_val_k;
2793 // in case of regression tree:
2794 // * node value is 1/n*sum_i(Y_i), where Y_i is i-th response,
2795 // n is the number of samples in the node.
2796 // * node risk is the sum of squared errors: sum_i((Y_i - <node_value>)^2)
2797 // * j-th cross-validation fold value and risk are calculated as above,
2798 // but using the samples with cv_labels(*)!=j.
2799 // * j-th cross-validation fold error is calculated
2800 // using samples with cv_labels(*)==j as the test subset:
2801 // error_j = sum_(i,cv_labels(i)==j)((Y_i - <node_value_j>)^2),
2802 // where node_value_j is the node value calculated
2803 // as described in the previous bullet, and summation is done
2804 // over the samples with cv_labels(*)==j.
2806 double sum = 0, sum2 = 0;
2807 float* values_buf = data->get_resp_float_buf();
2808 const float* values = 0;
2809 data->get_ord_responses(node, values_buf, &values);
2810 double *cv_sum = 0, *cv_sum2 = 0;
2815 for( i = 0; i < n; i++ )
2817 double t = values[i];
2824 cv_sum = (double*)cvStackAlloc( cv_n*sizeof(cv_sum[0]) );
2825 cv_sum2 = (double*)cvStackAlloc( cv_n*sizeof(cv_sum2[0]) );
2826 cv_count = (int*)cvStackAlloc( cv_n*sizeof(cv_count[0]) );
2828 for( j = 0; j < cv_n; j++ )
2830 cv_sum[j] = cv_sum2[j] = 0.;
2834 for( i = 0; i < n; i++ )
2837 double t = values[i];
2838 double s = cv_sum[j] + t;
2839 double s2 = cv_sum2[j] + t*t;
2840 int nc = cv_count[j] + 1;
2846 for( j = 0; j < cv_n; j++ )
2853 node->node_risk = sum2 - (sum/n)*sum;
2854 node->value = sum/n;
2856 for( j = 0; j < cv_n; j++ )
2858 double s = cv_sum[j], si = sum - s;
2859 double s2 = cv_sum2[j], s2i = sum2 - s2;
2860 int c = cv_count[j], ci = n - c;
2861 double r = si/MAX(ci,1);
2862 node->cv_node_risk[j] = s2i - r*r*ci;
2863 node->cv_node_error[j] = s2 - 2*r*s + c*r*r;
2864 node->cv_Tn[j] = INT_MAX;
2870 void CvDTree::complete_node_dir( CvDTreeNode* node )
2872 int vi, i, n = node->sample_count, nl, nr, d0 = 0, d1 = -1;
2873 int nz = n - node->get_num_valid(node->split->var_idx);
2874 char* dir = (char*)data->direction->data.ptr;
2876 // try to complete direction using surrogate splits
2877 if( nz && data->params.use_surrogates )
2879 CvDTreeSplit* split = node->split->next;
2880 for( ; split != 0 && nz; split = split->next )
2882 int inversed_mask = split->inversed ? -1 : 0;
2883 vi = split->var_idx;
2885 if( data->get_var_type(vi) >= 0 ) // split on categorical var
2887 int* labels_buf = data->get_pred_int_buf();
2888 const int* labels = 0;
2889 data->get_cat_var_data(node, vi, labels_buf, &labels);
2890 const int* subset = split->subset;
2892 for( i = 0; i < n; i++ )
2894 int idx = labels[i];
2895 if( !dir[i] && ( ((idx >= 0)&&(!data->is_buf_16u)) || ((idx != 65535)&&(data->is_buf_16u)) ))
2898 int d = CV_DTREE_CAT_DIR(idx,subset);
2899 dir[i] = (char)((d ^ inversed_mask) - inversed_mask);
2905 else // split on ordered var
2907 float* values_buf = data->get_pred_float_buf();
2908 const float* values = 0;
2909 int* indices_buf = data->get_pred_int_buf();
2910 const int* indices = 0;
2911 data->get_ord_var_data( node, vi, values_buf, indices_buf, &values, &indices );
2912 int split_point = split->ord.split_point;
2913 int n1 = node->get_num_valid(vi);
2915 assert( 0 <= split_point && split_point < n-1 );
2917 for( i = 0; i < n1; i++ )
2919 int idx = indices[i];
2922 int d = i <= split_point ? -1 : 1;
2923 dir[idx] = (char)((d ^ inversed_mask) - inversed_mask);
2932 // find the default direction for the rest
2935 for( i = nr = 0; i < n; i++ )
2938 d0 = nl > nr ? -1 : nr > nl;
2941 // make sure that every sample is directed either to the left or to the right
2942 for( i = 0; i < n; i++ )
2952 dir[i] = (char)d; // remap (-1,1) to (0,1)
2957 void CvDTree::split_node_data( CvDTreeNode* node )
2959 int vi, i, n = node->sample_count, nl, nr, scount = data->sample_count;
2960 char* dir = (char*)data->direction->data.ptr;
2961 CvDTreeNode *left = 0, *right = 0;
2962 int* new_idx = data->split_buf->data.i;
2963 int new_buf_idx = data->get_child_buf_idx( node );
2964 int work_var_count = data->get_work_var_count();
2965 CvMat* buf = data->buf;
2966 cv::AutoBuffer<int, 1<<14> _temp_buf(n);
2967 int* temp_buf = _temp_buf;
2969 complete_node_dir(node);
2971 for( i = nl = nr = 0; i < n; i++ )
2974 // initialize new indices for splitting ordered variables
2975 new_idx[i] = (nl & (d-1)) | (nr & -d); // d ? ri : li
2981 bool split_input_data;
2982 node->left = left = data->new_node( node, nl, new_buf_idx, node->offset );
2983 node->right = right = data->new_node( node, nr, new_buf_idx, node->offset + nl );
2985 split_input_data = node->depth + 1 < data->params.max_depth &&
2986 (node->left->sample_count > data->params.min_sample_count ||
2987 node->right->sample_count > data->params.min_sample_count);
2989 // split ordered variables, keep both halves sorted.
2990 for( vi = 0; vi < data->var_count; vi++ )
2992 int ci = data->get_var_type(vi);
2993 int n1 = node->get_num_valid(vi);
2994 int *src_idx_buf = data->get_pred_int_buf();
2995 const int* src_idx = 0;
2996 float *src_val_buf = data->get_pred_float_buf();
2997 const float* src_val = 0;
2999 if( ci >= 0 || !split_input_data )
3002 data->get_ord_var_data(node, vi, src_val_buf, src_idx_buf, &src_val, &src_idx);
3004 for(i = 0; i < n; i++)
3005 temp_buf[i] = src_idx[i];
3007 if (data->is_buf_16u)
3009 unsigned short *ldst, *rdst, *ldst0, *rdst0;
3010 //unsigned short tl, tr;
3011 ldst0 = ldst = (unsigned short*)(buf->data.s + left->buf_idx*buf->cols +
3012 vi*scount + left->offset);
3013 rdst0 = rdst = (unsigned short*)(ldst + nl);
3016 for( i = 0; i < n1; i++ )
3018 int idx = temp_buf[i];
3023 *rdst = (unsigned short)idx;
3028 *ldst = (unsigned short)idx;
3033 left->set_num_valid(vi, (int)(ldst - ldst0));
3034 right->set_num_valid(vi, (int)(rdst - rdst0));
3039 int idx = temp_buf[i];
3044 *rdst = (unsigned short)idx;
3049 *ldst = (unsigned short)idx;
3056 int *ldst0, *ldst, *rdst0, *rdst;
3057 ldst0 = ldst = buf->data.i + left->buf_idx*buf->cols +
3058 vi*scount + left->offset;
3059 rdst0 = rdst = buf->data.i + right->buf_idx*buf->cols +
3060 vi*scount + right->offset;
3063 for( i = 0; i < n1; i++ )
3065 int idx = temp_buf[i];
3080 left->set_num_valid(vi, (int)(ldst - ldst0));
3081 right->set_num_valid(vi, (int)(rdst - rdst0));
3086 int idx = temp_buf[i];
3103 // split categorical vars, responses and cv_labels using new_idx relocation table
3104 for( vi = 0; vi < work_var_count; vi++ )
3106 int ci = data->get_var_type(vi);
3107 int n1 = node->get_num_valid(vi), nr1 = 0;
3109 if( ci < 0 || (vi < data->var_count && !split_input_data) )
3112 int *src_lbls_buf = data->get_pred_int_buf();
3113 const int* src_lbls = 0;
3114 data->get_cat_var_data(node, vi, src_lbls_buf, &src_lbls);
3116 for(i = 0; i < n; i++)
3117 temp_buf[i] = src_lbls[i];
3119 if (data->is_buf_16u)
3121 unsigned short *ldst = (unsigned short *)(buf->data.s + left->buf_idx*buf->cols +
3122 vi*scount + left->offset);
3123 unsigned short *rdst = (unsigned short *)(buf->data.s + right->buf_idx*buf->cols +
3124 vi*scount + right->offset);
3126 for( i = 0; i < n; i++ )
3129 int idx = temp_buf[i];
3132 *rdst = (unsigned short)idx;
3134 nr1 += (idx != 65535 )&d;
3138 *ldst = (unsigned short)idx;
3143 if( vi < data->var_count )
3145 left->set_num_valid(vi, n1 - nr1);
3146 right->set_num_valid(vi, nr1);
3151 int *ldst = buf->data.i + left->buf_idx*buf->cols +
3152 vi*scount + left->offset;
3153 int *rdst = buf->data.i + right->buf_idx*buf->cols +
3154 vi*scount + right->offset;
3156 for( i = 0; i < n; i++ )
3159 int idx = temp_buf[i];
3164 nr1 += (idx >= 0)&d;
3174 if( vi < data->var_count )
3176 left->set_num_valid(vi, n1 - nr1);
3177 right->set_num_valid(vi, nr1);
3183 // split sample indices
3184 int *sample_idx_src_buf = data->get_sample_idx_buf();
3185 const int* sample_idx_src = 0;
3186 data->get_sample_indices(node, sample_idx_src_buf, &sample_idx_src);
3188 for(i = 0; i < n; i++)
3189 temp_buf[i] = sample_idx_src[i];
3191 int pos = data->get_work_var_count();
3192 if (data->is_buf_16u)
3194 unsigned short* ldst = (unsigned short*)(buf->data.s + left->buf_idx*buf->cols +
3195 pos*scount + left->offset);
3196 unsigned short* rdst = (unsigned short*)(buf->data.s + right->buf_idx*buf->cols +
3197 pos*scount + right->offset);
3198 for (i = 0; i < n; i++)
3201 unsigned short idx = (unsigned short)temp_buf[i];
3216 int* ldst = buf->data.i + left->buf_idx*buf->cols +
3217 pos*scount + left->offset;
3218 int* rdst = buf->data.i + right->buf_idx*buf->cols +
3219 pos*scount + right->offset;
3220 for (i = 0; i < n; i++)
3223 int idx = temp_buf[i];
3237 // deallocate the parent node data that is not needed anymore
3238 data->free_node_data(node);
3241 float CvDTree::calc_error( CvMLData* _data, int type, vector<float> *resp )
3244 const CvMat* values = _data->get_values();
3245 const CvMat* response = _data->get_responses();
3246 const CvMat* missing = _data->get_missing();
3247 const CvMat* sample_idx = (type == CV_TEST_ERROR) ? _data->get_test_sample_idx() : _data->get_train_sample_idx();
3248 const CvMat* var_types = _data->get_var_types();
3249 int* sidx = sample_idx ? sample_idx->data.i : 0;
3250 int r_step = CV_IS_MAT_CONT(response->type) ?
3251 1 : response->step / CV_ELEM_SIZE(response->type);
3252 bool is_classifier = var_types->data.ptr[var_types->cols-1] == CV_VAR_CATEGORICAL;
3253 int sample_count = sample_idx ? sample_idx->cols : 0;
3254 sample_count = (type == CV_TRAIN_ERROR && sample_count == 0) ? values->rows : sample_count;
3255 float* pred_resp = 0;
3256 if( resp && (sample_count > 0) )
3258 resp->resize( sample_count );
3259 pred_resp = &((*resp)[0]);
3262 if ( is_classifier )
3264 for( int i = 0; i < sample_count; i++ )
3267 int si = sidx ? sidx[i] : i;
3268 cvGetRow( values, &sample, si );
3270 cvGetRow( missing, &miss, si );
3271 float r = (float)predict( &sample, missing ? &miss : 0 )->value;
3274 int d = fabs((double)r - response->data.fl[si*r_step]) <= FLT_EPSILON ? 0 : 1;
3277 err = sample_count ? err / (float)sample_count * 100 : -FLT_MAX;
3281 for( int i = 0; i < sample_count; i++ )
3284 int si = sidx ? sidx[i] : i;
3285 cvGetRow( values, &sample, si );
3287 cvGetRow( missing, &miss, si );
3288 float r = (float)predict( &sample, missing ? &miss : 0 )->value;
3291 float d = r - response->data.fl[si*r_step];
3294 err = sample_count ? err / (float)sample_count : -FLT_MAX;
3299 void CvDTree::prune_cv()
3305 // 1. build tree sequence for each cv fold, calculate error_{Tj,beta_k}.
3306 // 2. choose the best tree index (if need, apply 1SE rule).
3307 // 3. store the best index and cut the branches.
3309 CV_FUNCNAME( "CvDTree::prune_cv" );
3313 int ti, j, tree_count = 0, cv_n = data->params.cv_folds, n = root->sample_count;
3314 // currently, 1SE for regression is not implemented
3315 bool use_1se = data->params.use_1se_rule != 0 && data->is_classifier;
3317 double min_err = 0, min_err_se = 0;
3320 CV_CALL( ab = cvCreateMat( 1, 256, CV_64F ));
3322 // build the main tree sequence, calculate alpha's
3325 double min_alpha = update_tree_rnc(tree_count, -1);
3326 if( cut_tree(tree_count, -1, min_alpha) )
3329 if( ab->cols <= tree_count )
3331 CV_CALL( temp = cvCreateMat( 1, ab->cols*3/2, CV_64F ));
3332 for( ti = 0; ti < ab->cols; ti++ )
3333 temp->data.db[ti] = ab->data.db[ti];
3334 cvReleaseMat( &ab );
3339 ab->data.db[tree_count] = min_alpha;
3342 ab->data.db[0] = 0.;
3344 if( tree_count > 0 )
3346 for( ti = 1; ti < tree_count-1; ti++ )
3347 ab->data.db[ti] = sqrt(ab->data.db[ti]*ab->data.db[ti+1]);
3348 ab->data.db[tree_count-1] = DBL_MAX*0.5;
3350 CV_CALL( err_jk = cvCreateMat( cv_n, tree_count, CV_64F ));
3351 err = err_jk->data.db;
3353 for( j = 0; j < cv_n; j++ )
3356 for( ; tk < tree_count; tj++ )
3358 double min_alpha = update_tree_rnc(tj, j);
3359 if( cut_tree(tj, j, min_alpha) )
3360 min_alpha = DBL_MAX;
3362 for( ; tk < tree_count; tk++ )
3364 if( ab->data.db[tk] > min_alpha )
3366 err[j*tree_count + tk] = root->tree_error;
3371 for( ti = 0; ti < tree_count; ti++ )
3374 for( j = 0; j < cv_n; j++ )
3375 sum_err += err[j*tree_count + ti];
3376 if( ti == 0 || sum_err < min_err )
3381 min_err_se = sqrt( sum_err*(n - sum_err) );
3383 else if( sum_err < min_err + min_err_se )
3388 pruned_tree_idx = min_idx;
3389 free_prune_data(data->params.truncate_pruned_tree != 0);
3393 cvReleaseMat( &err_jk );
3394 cvReleaseMat( &ab );
3395 cvReleaseMat( &temp );
3399 double CvDTree::update_tree_rnc( int T, int fold )
3401 CvDTreeNode* node = root;
3402 double min_alpha = DBL_MAX;
3406 CvDTreeNode* parent;
3409 int t = fold >= 0 ? node->cv_Tn[fold] : node->Tn;
3410 if( t <= T || !node->left )
3412 node->complexity = 1;
3413 node->tree_risk = node->node_risk;
3414 node->tree_error = 0.;
3417 node->tree_risk = node->cv_node_risk[fold];
3418 node->tree_error = node->cv_node_error[fold];
3425 for( parent = node->parent; parent && parent->right == node;
3426 node = parent, parent = parent->parent )
3428 parent->complexity += node->complexity;
3429 parent->tree_risk += node->tree_risk;
3430 parent->tree_error += node->tree_error;
3432 parent->alpha = ((fold >= 0 ? parent->cv_node_risk[fold] : parent->node_risk)
3433 - parent->tree_risk)/(parent->complexity - 1);
3434 min_alpha = MIN( min_alpha, parent->alpha );
3440 parent->complexity = node->complexity;
3441 parent->tree_risk = node->tree_risk;
3442 parent->tree_error = node->tree_error;
3443 node = parent->right;
3450 int CvDTree::cut_tree( int T, int fold, double min_alpha )
3452 CvDTreeNode* node = root;
3458 CvDTreeNode* parent;
3461 int t = fold >= 0 ? node->cv_Tn[fold] : node->Tn;
3462 if( t <= T || !node->left )
3464 if( node->alpha <= min_alpha + FLT_EPSILON )
3467 node->cv_Tn[fold] = T;
3477 for( parent = node->parent; parent && parent->right == node;
3478 node = parent, parent = parent->parent )
3484 node = parent->right;
3491 void CvDTree::free_prune_data(bool cut_tree)
3493 CvDTreeNode* node = root;
3497 CvDTreeNode* parent;
3500 // do not call cvSetRemoveByPtr( cv_heap, node->cv_Tn )
3501 // as we will clear the whole cross-validation heap at the end
3503 node->cv_node_error = node->cv_node_risk = 0;
3509 for( parent = node->parent; parent && parent->right == node;
3510 node = parent, parent = parent->parent )
3512 if( cut_tree && parent->Tn <= pruned_tree_idx )
3514 data->free_node( parent->left );
3515 data->free_node( parent->right );
3516 parent->left = parent->right = 0;
3523 node = parent->right;
3527 cvClearSet( data->cv_heap );
3531 void CvDTree::free_tree()
3533 if( root && data && data->shared )
3535 pruned_tree_idx = INT_MIN;
3536 free_prune_data(true);
3537 data->free_node(root);
3542 CvDTreeNode* CvDTree::predict( const CvMat* _sample,
3543 const CvMat* _missing, bool preprocessed_input ) const
3545 CvDTreeNode* result = 0;
3548 CV_FUNCNAME( "CvDTree::predict" );
3552 int i, step, mstep = 0;
3553 const float* sample;
3555 CvDTreeNode* node = root;
3562 CV_ERROR( CV_StsError, "The tree has not been trained yet" );
3564 if( !CV_IS_MAT(_sample) || CV_MAT_TYPE(_sample->type) != CV_32FC1 ||
3565 (_sample->cols != 1 && _sample->rows != 1) ||
3566 (_sample->cols + _sample->rows - 1 != data->var_all && !preprocessed_input) ||
3567 (_sample->cols + _sample->rows - 1 != data->var_count && preprocessed_input) )
3568 CV_ERROR( CV_StsBadArg,
3569 "the input sample must be 1d floating-point vector with the same "
3570 "number of elements as the total number of variables used for training" );
3572 sample = _sample->data.fl;
3573 step = CV_IS_MAT_CONT(_sample->type) ? 1 : _sample->step/sizeof(sample[0]);
3575 if( data->cat_count && !preprocessed_input ) // cache for categorical variables
3577 int n = data->cat_count->cols;
3578 catbuf = (int*)cvStackAlloc(n*sizeof(catbuf[0]));
3579 for( i = 0; i < n; i++ )
3585 if( !CV_IS_MAT(_missing) || !CV_IS_MASK_ARR(_missing) ||
3586 !CV_ARE_SIZES_EQ(_missing, _sample) )
3587 CV_ERROR( CV_StsBadArg,
3588 "the missing data mask must be 8-bit vector of the same size as input sample" );
3589 m = _missing->data.ptr;
3590 mstep = CV_IS_MAT_CONT(_missing->type) ? 1 : _missing->step/sizeof(m[0]);
3593 vtype = data->var_type->data.i;
3594 vidx = data->var_idx && !preprocessed_input ? data->var_idx->data.i : 0;
3595 cmap = data->cat_map ? data->cat_map->data.i : 0;
3596 cofs = data->cat_ofs ? data->cat_ofs->data.i : 0;
3598 while( node->Tn > pruned_tree_idx && node->left )
3600 CvDTreeSplit* split = node->split;
3602 for( ; !dir && split != 0; split = split->next )
3604 int vi = split->var_idx;
3606 i = vidx ? vidx[vi] : vi;
3607 float val = sample[i*step];
3608 if( m && m[i*mstep] )
3610 if( ci < 0 ) // ordered
3611 dir = val <= split->ord.c ? -1 : 1;
3615 if( preprocessed_input )
3622 int a = c = cofs[ci];
3623 int b = (ci+1 >= data->cat_ofs->cols) ? data->cat_map->cols : cofs[ci+1];
3625 int ival = cvRound(val);
3627 CV_ERROR( CV_StsBadArg,
3628 "one of input categorical variable is not an integer" );
3635 if( ival < cmap[c] )
3637 else if( ival > cmap[c] )
3643 if( c < 0 || ival != cmap[c] )
3646 catbuf[ci] = c -= cofs[ci];
3649 c = ( (c == 65535) && data->is_buf_16u ) ? -1 : c;
3650 dir = CV_DTREE_CAT_DIR(c, split->subset);
3653 if( split->inversed )
3659 double diff = node->right->sample_count - node->left->sample_count;
3660 dir = diff < 0 ? -1 : 1;
3662 node = dir < 0 ? node->left : node->right;
3673 CvDTreeNode* CvDTree::predict( const Mat& _sample, const Mat& _missing, bool preprocessed_input ) const
3675 CvMat sample = _sample, mmask = _missing;
3676 return predict(&sample, mmask.data.ptr ? &mmask : 0, preprocessed_input);
3680 const CvMat* CvDTree::get_var_importance()
3682 if( !var_importance )
3684 CvDTreeNode* node = root;
3688 var_importance = cvCreateMat( 1, data->var_count, CV_64F );
3689 cvZero( var_importance );
3690 importance = var_importance->data.db;
3694 CvDTreeNode* parent;
3695 for( ;; node = node->left )
3697 CvDTreeSplit* split = node->split;
3699 if( !node->left || node->Tn <= pruned_tree_idx )
3702 for( ; split != 0; split = split->next )
3703 importance[split->var_idx] += split->quality;
3706 for( parent = node->parent; parent && parent->right == node;
3707 node = parent, parent = parent->parent )
3713 node = parent->right;
3716 cvNormalize( var_importance, var_importance, 1., 0, CV_L1 );
3719 return var_importance;
3723 void CvDTree::write_split( CvFileStorage* fs, CvDTreeSplit* split ) const
3727 cvStartWriteStruct( fs, 0, CV_NODE_MAP + CV_NODE_FLOW );
3728 cvWriteInt( fs, "var", split->var_idx );
3729 cvWriteReal( fs, "quality", split->quality );
3731 ci = data->get_var_type(split->var_idx);
3732 if( ci >= 0 ) // split on a categorical var
3734 int i, n = data->cat_count->data.i[ci], to_right = 0, default_dir;
3735 for( i = 0; i < n; i++ )
3736 to_right += CV_DTREE_CAT_DIR(i,split->subset) > 0;
3738 // ad-hoc rule when to use inverse categorical split notation
3739 // to achieve more compact and clear representation
3740 default_dir = to_right <= 1 || to_right <= MIN(3, n/2) || to_right <= n/3 ? -1 : 1;
3742 cvStartWriteStruct( fs, default_dir*(split->inversed ? -1 : 1) > 0 ?
3743 "in" : "not_in", CV_NODE_SEQ+CV_NODE_FLOW );
3745 for( i = 0; i < n; i++ )
3747 int dir = CV_DTREE_CAT_DIR(i,split->subset);
3748 if( dir*default_dir < 0 )
3749 cvWriteInt( fs, 0, i );
3751 cvEndWriteStruct( fs );
3754 cvWriteReal( fs, !split->inversed ? "le" : "gt", split->ord.c );
3756 cvEndWriteStruct( fs );
3760 void CvDTree::write_node( CvFileStorage* fs, CvDTreeNode* node ) const
3762 CvDTreeSplit* split;
3764 cvStartWriteStruct( fs, 0, CV_NODE_MAP );
3766 cvWriteInt( fs, "depth", node->depth );
3767 cvWriteInt( fs, "sample_count", node->sample_count );
3768 cvWriteReal( fs, "value", node->value );
3770 if( data->is_classifier )
3771 cvWriteInt( fs, "norm_class_idx", node->class_idx );
3773 cvWriteInt( fs, "Tn", node->Tn );
3774 cvWriteInt( fs, "complexity", node->complexity );
3775 cvWriteReal( fs, "alpha", node->alpha );
3776 cvWriteReal( fs, "node_risk", node->node_risk );
3777 cvWriteReal( fs, "tree_risk", node->tree_risk );
3778 cvWriteReal( fs, "tree_error", node->tree_error );
3782 cvStartWriteStruct( fs, "splits", CV_NODE_SEQ );
3784 for( split = node->split; split != 0; split = split->next )
3785 write_split( fs, split );
3787 cvEndWriteStruct( fs );
3790 cvEndWriteStruct( fs );
3794 void CvDTree::write_tree_nodes( CvFileStorage* fs ) const
3796 //CV_FUNCNAME( "CvDTree::write_tree_nodes" );
3800 CvDTreeNode* node = root;
3802 // traverse the tree and save all the nodes in depth-first order
3805 CvDTreeNode* parent;
3808 write_node( fs, node );
3814 for( parent = node->parent; parent && parent->right == node;
3815 node = parent, parent = parent->parent )
3821 node = parent->right;
3828 void CvDTree::write( CvFileStorage* fs, const char* name ) const
3830 //CV_FUNCNAME( "CvDTree::write" );
3834 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_ML_TREE );
3836 //get_var_importance();
3837 data->write_params( fs );
3838 //if( var_importance )
3839 //cvWrite( fs, "var_importance", var_importance );
3842 cvEndWriteStruct( fs );
3848 void CvDTree::write( CvFileStorage* fs ) const
3850 //CV_FUNCNAME( "CvDTree::write" );
3854 cvWriteInt( fs, "best_tree_idx", pruned_tree_idx );
3856 cvStartWriteStruct( fs, "nodes", CV_NODE_SEQ );
3857 write_tree_nodes( fs );
3858 cvEndWriteStruct( fs );
3864 CvDTreeSplit* CvDTree::read_split( CvFileStorage* fs, CvFileNode* fnode )
3866 CvDTreeSplit* split = 0;
3868 CV_FUNCNAME( "CvDTree::read_split" );
3874 if( !fnode || CV_NODE_TYPE(fnode->tag) != CV_NODE_MAP )
3875 CV_ERROR( CV_StsParseError, "some of the splits are not stored properly" );
3877 vi = cvReadIntByName( fs, fnode, "var", -1 );
3878 if( (unsigned)vi >= (unsigned)data->var_count )
3879 CV_ERROR( CV_StsOutOfRange, "Split variable index is out of range" );
3881 ci = data->get_var_type(vi);
3882 if( ci >= 0 ) // split on categorical var
3884 int i, n = data->cat_count->data.i[ci], inversed = 0, val;
3887 split = data->new_split_cat( vi, 0 );
3888 inseq = cvGetFileNodeByName( fs, fnode, "in" );
3891 inseq = cvGetFileNodeByName( fs, fnode, "not_in" );
3895 (CV_NODE_TYPE(inseq->tag) != CV_NODE_SEQ && CV_NODE_TYPE(inseq->tag) != CV_NODE_INT))
3896 CV_ERROR( CV_StsParseError,
3897 "Either 'in' or 'not_in' tags should be inside a categorical split data" );
3899 if( CV_NODE_TYPE(inseq->tag) == CV_NODE_INT )
3901 val = inseq->data.i;
3902 if( (unsigned)val >= (unsigned)n )
3903 CV_ERROR( CV_StsOutOfRange, "some of in/not_in elements are out of range" );
3905 split->subset[val >> 5] |= 1 << (val & 31);
3909 cvStartReadSeq( inseq->data.seq, &reader );
3911 for( i = 0; i < reader.seq->total; i++ )
3913 CvFileNode* inode = (CvFileNode*)reader.ptr;
3914 val = inode->data.i;
3915 if( CV_NODE_TYPE(inode->tag) != CV_NODE_INT || (unsigned)val >= (unsigned)n )
3916 CV_ERROR( CV_StsOutOfRange, "some of in/not_in elements are out of range" );
3918 split->subset[val >> 5] |= 1 << (val & 31);
3919 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
3923 // for categorical splits we do not use inversed splits,
3924 // instead we inverse the variable set in the split
3926 for( i = 0; i < (n + 31) >> 5; i++ )
3927 split->subset[i] ^= -1;
3931 CvFileNode* cmp_node;
3932 split = data->new_split_ord( vi, 0, 0, 0, 0 );
3934 cmp_node = cvGetFileNodeByName( fs, fnode, "le" );
3937 cmp_node = cvGetFileNodeByName( fs, fnode, "gt" );
3938 split->inversed = 1;
3941 split->ord.c = (float)cvReadReal( cmp_node );
3944 split->quality = (float)cvReadRealByName( fs, fnode, "quality" );
3952 CvDTreeNode* CvDTree::read_node( CvFileStorage* fs, CvFileNode* fnode, CvDTreeNode* parent )
3954 CvDTreeNode* node = 0;
3956 CV_FUNCNAME( "CvDTree::read_node" );
3963 if( !fnode || CV_NODE_TYPE(fnode->tag) != CV_NODE_MAP )
3964 CV_ERROR( CV_StsParseError, "some of the tree elements are not stored properly" );
3966 CV_CALL( node = data->new_node( parent, 0, 0, 0 ));
3967 depth = cvReadIntByName( fs, fnode, "depth", -1 );
3968 if( depth != node->depth )
3969 CV_ERROR( CV_StsParseError, "incorrect node depth" );
3971 node->sample_count = cvReadIntByName( fs, fnode, "sample_count" );
3972 node->value = cvReadRealByName( fs, fnode, "value" );
3973 if( data->is_classifier )
3974 node->class_idx = cvReadIntByName( fs, fnode, "norm_class_idx" );
3976 node->Tn = cvReadIntByName( fs, fnode, "Tn" );
3977 node->complexity = cvReadIntByName( fs, fnode, "complexity" );
3978 node->alpha = cvReadRealByName( fs, fnode, "alpha" );
3979 node->node_risk = cvReadRealByName( fs, fnode, "node_risk" );
3980 node->tree_risk = cvReadRealByName( fs, fnode, "tree_risk" );
3981 node->tree_error = cvReadRealByName( fs, fnode, "tree_error" );
3983 splits = cvGetFileNodeByName( fs, fnode, "splits" );
3987 CvDTreeSplit* last_split = 0;
3989 if( CV_NODE_TYPE(splits->tag) != CV_NODE_SEQ )
3990 CV_ERROR( CV_StsParseError, "splits tag must stored as a sequence" );
3992 cvStartReadSeq( splits->data.seq, &reader );
3993 for( i = 0; i < reader.seq->total; i++ )
3995 CvDTreeSplit* split;
3996 CV_CALL( split = read_split( fs, (CvFileNode*)reader.ptr ));
3998 node->split = last_split = split;
4000 last_split = last_split->next = split;
4002 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
4012 void CvDTree::read_tree_nodes( CvFileStorage* fs, CvFileNode* fnode )
4014 CV_FUNCNAME( "CvDTree::read_tree_nodes" );
4020 CvDTreeNode* parent = &_root;
4022 parent->left = parent->right = parent->parent = 0;
4024 cvStartReadSeq( fnode->data.seq, &reader );
4026 for( i = 0; i < reader.seq->total; i++ )
4030 CV_CALL( node = read_node( fs, (CvFileNode*)reader.ptr, parent != &_root ? parent : 0 ));
4032 parent->left = node;
4034 parent->right = node;
4039 while( parent && parent->right )
4040 parent = parent->parent;
4043 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
4052 void CvDTree::read( CvFileStorage* fs, CvFileNode* fnode )
4054 CvDTreeTrainData* _data = new CvDTreeTrainData();
4055 _data->read_params( fs, fnode );
4057 read( fs, fnode, _data );
4058 get_var_importance();
4062 // a special entry point for reading weak decision trees from the tree ensembles
4063 void CvDTree::read( CvFileStorage* fs, CvFileNode* node, CvDTreeTrainData* _data )
4065 CV_FUNCNAME( "CvDTree::read" );
4069 CvFileNode* tree_nodes;
4074 tree_nodes = cvGetFileNodeByName( fs, node, "nodes" );
4075 if( !tree_nodes || CV_NODE_TYPE(tree_nodes->tag) != CV_NODE_SEQ )
4076 CV_ERROR( CV_StsParseError, "nodes tag is missing" );
4078 pruned_tree_idx = cvReadIntByName( fs, node, "best_tree_idx", -1 );
4079 read_tree_nodes( fs, tree_nodes );