Пример #1
0
 public function calcSplitInfo($attribute)
 {
     //$this->load->library('Customer');
     $customerArray = MCustomers::getCustomerOrderBy($attribute);
     $customersNumber = sizeof($customerArray);
     if ($customersNumber > 0) {
         $attValue = -1;
         $numberOfAValue = 0;
         $splitInfo = 0;
         foreach ($customerArray as $customer) {
             $attributeArray = $customer->toArray();
             //var_dump($attributeArray[$attribute]);
             if ($attValue == $attributeArray[$attribute]) {
                 $numberOfAValue++;
             } else {
                 if ($attValue == -1) {
                 } else {
                     //var_dump("_value:".$attValue."_");
                     $p = (double) $numberOfAValue / $customersNumber;
                     //var_dump("_p:".$p."_");
                     $tam = -$p * (log($p) / log(2));
                     //var_dump("_tam:".$tam."_");
                     $splitInfo += $tam;
                     //var_dump("_splitI:".$splitInfo."_");
                 }
                 $attValue = (double) $attributeArray[$attribute];
                 //var_dump($attValue);
                 $numberOfAValue = 1;
             }
         }
         return $splitInfo;
     } else {
         return 0;
     }
 }
Пример #2
0
 public function buildTree($node, $customerArray)
 {
     $this->i++;
     if ($this->checkOnlyClass($customerArray)) {
         $node->type = 1;
         $node->label = 'class';
         $node->data = $customerArray[0]->class;
         //$node->save();
         echo "saved_leaf" . $this->i;
         return;
     } else {
         //$node->save;
         echo "saved_normal" . $this->i;
         $max = 0;
         $maxRatioAtt;
         $forSearch = new Customer();
         foreach ($forSearch->attribute_array as $attribute) {
             $ratio = $this->calcGainRatio($customerArray, $attribute);
             if ($max < $ratio) {
                 $max = $ratio;
                 $maxRatioAtt = $attribute;
             }
         }
         $node->type = 0;
         $newNode = new TreeNode();
         $newNode->parent_id = $node->id;
         $newNode->type = 0;
         $newNode->label = $maxRatioAtt;
         if ($this->isContinueous($maxRatioAtt)) {
             $tmpMCus = new MCustomers();
             $valAttr = $tmpMCus->getDistinctValAttr($maxRatioAtt);
             $total = count($valAttr);
             for ($i = 0; $i < $total - 1; $i++) {
                 for ($j = i + 1; $j < $total; $j++) {
                     if ($valAttr[$i] > $valAttr[$j]) {
                         $temp = $valAttr[$i];
                         $valAttr[$i] = $valAttr[$j];
                         $valAttr[$j] = $temp;
                     }
                 }
             }
             $maxAtt = 0;
             $maxTemp = 0;
             for ($i = 0; $i < $total - 1; $i++) {
                 $temp = (double) ($valAttr[$i] + $valAttr[$i + 1]) / 2;
                 if ($maxAtt < $this->gainRatioM($customerArray, $maxRatioAtt, $temp)) {
                     $maxAtt = $this->gainRatioM($customerArray, $maxRatioAtt, $temp);
                     $maxTemp = $temp;
                 }
             }
             $moc = $maxTemp;
             $aboveMoc = array();
             $lowerMoc = array();
             foreach ($customerArray as $eachCus) {
                 $eachCusArr = $eachCus->toArray();
                 if ($eachCusArr[$maxRatioAtt] > $moc) {
                     array_push($aboveMoc, $eachCus);
                 } else {
                     array_push($lowerMoc, $eachCus);
                 }
             }
             $newNode->data = ">" . $moc;
             $this->buildTree($newNode, $aboveMoc);
             $newNode->data = "<" . $moc;
             $this->buildTree($newNode, $lowerMoc);
         } else {
             $tmpMCus = new MCustomers();
             $valAttr = $tmpMCus->getDistinctValAttr($maxRatioAtt);
             $total = count($valAttr);
             for ($i = 0; $i < $total; $i++) {
                 $tmp = array();
                 foreach ($customerArray as $eachCus) {
                     $eachCusArr = $eachCus->toArray();
                     if ($eachCusArr[$maxRatioAtt] == $valAttr[$i]) {
                         array_push($tmp, $eachCus);
                     }
                 }
                 $newNode->data = $i;
                 $this->buildTree($newNode, $tmp);
             }
         }
     }
 }