Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1. public function buildTree(array $criteria = [])
  2. {
  3.     $treeNode = new TreeNode();
  4.     $checkClass = $this->isBelongToOneClass($criteria);
  5.  
  6.     if ($checkClass['return']) {
  7.         $treeNode->setAttribute($this->targetAttribute);
  8.         $treeNode->addChild('result', $checkClass['class']);
  9.         $treeNode->setIsLeaf(true);
  10.  
  11.         return $treeNode;
  12.     }
  13.  
  14.     $splitCriterion = $this->calculateSplitCriterion($criteria);
  15.  
  16.     $bestAttrName = $this->getBiggestArrayAttribute($splitCriterion);
  17.     $bestAttrValues = $this->getAttributeValues($bestAttrName);
  18.  
  19.     $treeNode->setAttribute($bestAttrName);
  20.  
  21.     unset($splitCriterion[$bestAttrName]);
  22.    
  23.     foreach ($bestAttrValues as $value) {
  24.         $criteria[$bestAttrName] = $value;
  25.         $targetCount = $this->countTargetByCriteria($criteria);
  26.         $treeNode->addClassesCount($value, $targetCount);
  27.  
  28.         if (array_sum($targetCount) == 0) {
  29.             $targetCount2 = $this->countTargetByCriteria([$bestAttrName => $value]);
  30.             $biggestClass = $this->getBiggestArrayAttribute($targetCount2);
  31.  
  32.             $child = new TreeNode();
  33.             $child->setParent($treeNode);
  34.             $child->setAttribute($this->targetAttribute);
  35.             $child->addChild('result', $biggestClass);
  36.             $child->setIsLeaf(true);
  37.  
  38.             $treeNode->addChild($value, $child);
  39.         } elseif (!empty($splitCriterion)) {
  40.             $child = $this->buildTree($criteria);
  41.             $child->setParent($treeNode);
  42.  
  43.             $treeNode->addChild($value, $child);
  44.         } else {
  45.             $classProb = $this->calculateClassProbability($criteria);
  46.             $biggestClass = $this->getBiggestArrayAttribute($classProb);
  47.  
  48.             $child = new TreeNode();
  49.             $child->setParent($treeNode);
  50.             $child->setAttribute($this->targetAttribute);
  51.             $child->addChild('result', $biggestClass);
  52.             $child->setIsLeaf(true);
  53.  
  54.             $treeNode->addChild($value, $child);
  55.         }
  56.     }
  57.  
  58.     return $treeNode;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement