source: orange/source/orange/tdidt_stop.cpp @ 6531:57bdc92cd8e9

Revision 6531:57bdc92cd8e9, 3.0 KB checked in by janezd <janez.demsar@…>, 4 years ago (diff)
  • changed licenses to GPL 3.0
Line 
1/*
2    This file is part of Orange.
3   
4    Copyright 1996-2010 Faculty of Computer and Information Science, University of Ljubljana
5    Contact: janez.demsar@fri.uni-lj.si
6
7    Orange is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Orange is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Orange.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22#include "examplegen.hpp"
23#include "contingency.hpp"
24
25#include "tdidt_stop.ppp"
26
27
28bool TTreeStopCriteria::operator()(PExampleGenerator gen, const int &, PDomainContingency ocont)
29{ int nor = gen->numberOfExamples();
30  if ((nor==0) || (nor==1))
31    return true;      // example set is too small
32
33  char vt = gen->domain->classVar->varType;
34  if (vt!=TValue::INTVAR)
35    return false;  // class is continuous, may continue
36
37  // is there more than one class left?
38  if (ocont) {
39    char ndcf = 0;
40    TDiscDistribution const &dva=CAST_TO_DISCDISTRIBUTION(ocont->classes);
41    const_ITERATE(TDiscDistribution, ci, dva)
42      if ((*ci>0) && (++ndcf==2))
43        return false; // at least two classes, may continue
44  }
45 
46  else { 
47    TExampleIterator ei = gen->begin();
48    TValue fv = (*ei).getClass();
49
50    while(fv.isSpecial() && ++ei)
51      fv = (*ei).getClass();
52    if (!ei)
53      return true;
54
55    const int fvi = fv.intV;
56
57    while(++ei) {
58      TValue &cval = (*ei).getClass();
59      if (!cval.isSpecial() && (cval.intV != fvi))
60        return false; // yes, may continue
61    }
62  }
63
64  return true; // no, there's just one class left!
65}
66
67
68TTreeStopCriteria_common::TTreeStopCriteria_common(float aMaxMajor, float aMinExamples)
69: maxMajority(aMaxMajor),
70  minExamples(aMinExamples)
71{}
72
73
74TTreeStopCriteria_common::TTreeStopCriteria_common(const TTreeStopCriteria_common &old)
75: TTreeStopCriteria(old),
76  maxMajority(old.maxMajority),
77  minExamples(old.minExamples)
78{}
79
80
81bool TTreeStopCriteria_common::operator()(PExampleGenerator gen, const int &weight, PDomainContingency ocont)
82{ if (TTreeStopCriteria::operator()(gen, weight, ocont)) 
83    return true; // inherited method says its enough
84
85  PDistribution classDist = ocont ? ocont->classes : getClassDistribution(gen, weight);
86  if (classDist->abs<minExamples)
87    return true; // not enough examples
88
89  float limit = maxMajority*classDist->abs;
90  TDiscDistribution *ddva = classDist.AS(TDiscDistribution);
91  if (ddva) {
92    const_PITERATE(TDiscDistribution, ci, ddva)
93      if (*ci>limit)
94        return true;
95  }
96  else {
97    TContDistribution *cdva = classDist.AS(TContDistribution);
98    const_PITERATE(TContDistribution, ci, cdva)
99      if ((*ci).second>limit)
100        return true;
101  }
102
103  return false;
104}
105
Note: See TracBrowser for help on using the repository browser.