php codeigniter中的Fp-growth算法

时间:2015-04-17 10:28:49

标签: php codeigniter

我正在尝试在PHP Codeigneter中编写fp增长算法。它可以很好地处理小数据集(数组)但是它给了我一个大数据集的错误它显示了一些频繁项集(输出)但是也显示了这个。"为foreach()"提供的参数无效。"致命错误:第119行"

中的models \ Algorithm.php超过300秒的最大执行时间 模型中的

class Algorithm extends CI_Model{

public function find_frequent_itemsets($transactions, $minimum_support){

    $processedTransactions = array();
    $items = array();
    //var_dump($transactions);
    foreach($transactions as $transaction){
        if(!is_array($transaction)) 
            continue;
        $processed = array();

        foreach($transaction as $item){

            if(array_key_exists($item, $items)){
                $items[$item] += 1;
            }else{

                $items[$item] = 1;
            }
            array_push($processed, $item);

        }// every transaction.

        array_push($processedTransactions, $processed);
    }

      foreach(array_keys($items) as $index){
        if($items[$index] < $minimum_support){
            unset($items[$index]);
        }
    }



    $sortedTransactions = array();
    //var_dump($processedTransactions);
    foreach($processedTransactions as $currentTransactions){
        $tmp = array();

        foreach($currentTransactions as $item){
            if(array_key_exists($item, $items)){
                // key exits = frequent item.
                //array_push($tmp, array($item, $items[$item]));
                $tmp[$item] = $items[$item];
            }
        }

        // sort based on most frequent item.
        arsort($tmp);
        //var_dump($tmp);
        array_push($sortedTransactions, array_keys($tmp));
    }

    //var_dump($sortedTransactions);
    // Create a Fp-Tree
    $tree = new FPTree();

    // Add all Transactions.
    foreach($sortedTransactions as $currentTransactions){
        $tree->add($currentTransactions);
    }




        for itemset in find_with_suffix(master, []):
            yield itemset


    foreach(find_with_suffix($tree, array(), $minimum_support) as $itemset){
        yield $itemset;
    }

}// end function`}`


function find_with_suffix($tree, $suffix, $minimum_support){
/* $element = ($item, $nodes)
   $item is the item it self like (a, b, ..., e)
   $nodes is the linked list for that item (e1 -> e2 -> e3 .... ) */
  foreach($tree->getItems() as $element){
    $item = $element[0];
    $nodes = $element[1];
    $support = 0;
    /* calculate support of each linked list. */
    foreach($nodes as $n){
        $support += $n->getCount();
    }

    if($support >= $minimum_support && !array_key_exists($item, $suffix)){
        $found_set = array_merge(array($item), $suffix);
        /* yields current found set */
        yield $found_set;




$condTree =  conditional_tree_from_paths($tree->prefixPaths($item),$minimum_support);


            foreach(find_with_suffix($condTree, $found_set, $minimum_support) as $s){
            yield $s;
        }
    }
}}function conditional_tree_from_paths($paths, $minimum_support){

$tree = new FPTree();
$conditionItem = null;
$items = array();

foreach($paths as $path){
    if($conditionItem == null){
        $conditionItem = end($path)->getItem();
        //reset($path);
    }

    $point = $tree->root;

    foreach($path as $node){
        //next_point = point.search(node.item)
        $nextPoint = $point->search($node->getItem());

        if($nextPoint == null){
            // Add a new node to the tree.
            array_push($items, $node->getItem());
            $count = ($node->getItem() == $conditionItem) ? $node->getCount() : 0;
            $nextPoint = new FPNode($tree, $node->getItem(), $count);
            $point->add($nextPoint);
            $tree->updateRoute($nextPoint);
        }
        $point = $nextPoint;
    }
}

// assert condition_item is not None
foreach($tree->prefixPaths($conditionItem) as $path){
    $count = end($path)->getCount();
    //reset($path);
    foreach(array_reverse(array_slice($path, 0, count($path) - 1)) as $node){
        $tmpCount = $node->getCount();
        $node->setCount($tmpCount + $count);
    }
}

// as set.
$items = array_unique($items);


foreach($items as $item){

    $currentSupport = 0;
    foreach($tree->getNodes($item) as $n){
        $currentSupport += $n->getCount();
    }

    if($currentSupport < $minimum_support){
        foreach($tree->getNodes($item) as $n){
            $parent = $n->getParent();
            if($parent != null){
                $parent->remove($n);
            }
        }
    }
}

// Remove leaves .
foreach($tree->getNodes($conditionItem) as $node){
    $parent = $node->getParent();
    if($parent != null){
        $parent->remove($node);
    }
}

return $tree;}class FPNode {
var $tree;
var $item;
var $count;
var $parent;
var $children;
var $neighbor;

/**
 * @param $tree
 * @param $item
 * @param int $count
 */
public function __construct($tree, $item, $count=1){
    $this->tree = $tree;
    $this->item = $item;
    $this->count = $count;
    $this->parent = null;
    $this->children = array();
    $this->neighbor = null;
}

/**
 * @param $child
 */
public function add($child){
    $item = $child->getItem();
    if(!array_key_exists($item, $this->children)){
        $child->setParent($this);
        $this->children[$item] = $child;
    }
}


/**
 * @param $item
 * @return null
 */
public function search($item){
    //return array_key_exists($item, $this->children);
    if(array_key_exists($item, $this->children)){
        return $this->children[$item];
    }else{
        return null;
    }
}

/**
 * @param $child
 */
public function remove($child){
    $item = $child->getItem();
    if($this->search($item)){
        unset($this->children[$item]);
        $child->setParent(null);
        //self._tree._removed(child)
        $this->tree->removed($child);
        foreach($child->getChildren() as $sub_child){
            $sub_item = $sub_child->getItem();
            if($this->search($sub_item)){
                // self._children[sub_child.item]._count += sub_child.count
                $currentCount = $this->children[$sub_child->getItem()]->getCount();
                $subCount = $sub_child->getCount();
                $this->children[$sub_child->getItem()]->setCount($currentCount + $subCount);
                $sub_child->setParent(null);
            }else{
                $this->add($sub_child);
            }
        }
        $child->setChildren(null);
    }
}

/**
 *
 */
public function increment(){
    $this->count++;
}

/**
 * @return bool
 */
public function isRoot(){
    return $this->item == null && $this->count == null;
}
/**
 * @return mixed
 */
public function getTree()
{
    return $this->tree;
}

/**
 * @param mixed $tree
 */
public function setTree($tree)
{
    $this->tree = $tree;
}

/**
 * @return mixed
 */
public function getItem()
{
    return $this->item;
}

/**
 * @param mixed $item
 */
public function setItem($item)
{
    $this->item = $item;
}

/**
 * @return int
 */
public function getCount()
{
    return $this->count;
}

/**
 * @param int $count
 */
public function setCount($count)
{
    $this->count = $count;
}

/**
 * @return null
 */
public function getParent()
{
    return $this->parent;
}

/**
 * @param null $parent
 */
public function setParent($parent)
{
    $this->parent = $parent;
}

/**
 * @return null
 */
public function getNeighbor()
{
    return $this->neighbor;
}

/**
 * @param null $neighbor
 */
public function setNeighbor($neighbor)
{
    $this->neighbor = $neighbor;
}

/**
 * @return array
 */
public function getChildren()
{
    return $this->children;
}

/**
 * @param array $children
 */
public function setChildren($children)
{
    $this->children = $children;
}}class FPTree {

var $root;
var $routes;

public function __construct(){
    $this->root = new FPNode($this, null, null);
    // Item -> (head, tail)
    $this->routes = array();
}

public function add($transaction){
    $point = $this->root;

    foreach($transaction as $item){
        $nextPoint = $point->search($item);

        if($nextPoint != null){
            $nextPoint->increment();
        }else{
            // Null
            $nextPoint = new FPNode($this, $item, 1);
            $point->add($nextPoint);
            $this->updateRoute($nextPoint);
        }

        $point = $nextPoint;
    }
}

public function updateRoute($point){
    $item = $point->getItem();

    if(array_key_exists($item, $this->routes)){
        $route = $this->routes[$item];
        $route[1]->setNeighbor($point);
        $this->routes[$item] = array($route[0], $point);
    }else{
        // $item not found.
        // add new pair with the same item.
        // used as linked list but (head, tail) only.
        $this->routes[$item] = array($point, $point);
    }
}

public function getItems(){
    $items = array();
    foreach(array_keys($this->routes) as $item){
        array_push($items, array($item, $this->getNodes($item)));
    }

    return $items;
}

public function getNodes($item){

    if(array_key_exists($item, $this->routes)){

        $node = $this->routes[$item][0];
        $nodes = array();


        while($node != null){
            array_push($nodes, $node);
            $node = $node->getNeighbor();
        }
        return $nodes;
    }else{
        return null;
    }
}

public function getPath($node){
    $path = array();
    while($node != null && !$node->isRoot()){
        array_push($path, $node);
        $node = $node->getParent();
    }
    return array_reverse($path);
}

public function prefixPaths($item){
    $paths = array();

    foreach($this->getNodes($item) as $node){
        array_push($paths, $this->getPath($node));
    }

    return $paths;
}

public function removed($node){
    $item = $node->getItem();
    if(array_key_exists($item, $this->routes)){
        $route = $this->routes[$item];

        $head = $route[0];
        $tail = $route[1];

        if($node == $head){
            if($node == $tail || $node != $node->getNeighbor()){
                unset($this->routes[$item]);
            }else{

                $this->routes[$item] = array($node->getNeighbor(), $tail);
            }
        }else{

            $nodes = $this->getNodes($item);
            foreach($nodes as $n){
                // if n.neighbor is node:
                if($n->getNeighbor() == $node){
                    $n->setNeighbor($node->getNeighbor());
                    if($node == $tail){
                        $this->routes[$item] = array($head, $n);
                    }
                }

            }
        }
    }
}

public function printTree(){
    echo "Tree\n";
    echo "Root";
    $this->inspect($this->root, 0);
}

public function inspect($node, $level){
    for($i = 0; $i < $level; ++$i){
        echo '..';
    }
    if($node != null){


        echo "(". $node->getItem() . ":" . $node->getCount() . ")" . " ==> ";
        foreach($node->getChildren() as $child){
            echo "(" . $child->getItem() . ":" . $child->getCount() . "), ";
        }
        echo "\n";
        foreach($node->getChildren() as $child){
            $this->inspect($child, $level + 1);
        }
    }
}

public function printNeighbors($item){
    $nodes = $this->getNodes($item);

    if($nodes != null){
        foreach($nodes as $node){
            echo "(" . $node->getItem() . ":" . $node->getCount() . ") -> ";
        }
    }
    echo "\n";
}

public function printAllPrefix($item){
    $paths = $this->prefixPaths($item);

    foreach($paths as $path){
        foreach($path as $node){
            echo "(" . $node->getItem() . ":" . $node->getCount() . ") -> ";
        }

        echo "\n";
    }
}

public function getRoot()
{
    return $this->root;
}


public function setRoot($root)
{
    $this->root = $root;
}


public function getRoutes()
{
    return $this->routes;
}


public function setRoutes ($routes)
{
    $this->routes = $routes;
}

 }
控制器中的

 $transactions=$this->Getdata->getdataset();
$minimum_support = 2;
foreach($this->algorithm->find_frequent_itemsets($transactions,$minimum_support) as $itemset){                     
        echo '{' . implode(", ", $itemset) . '}' . "\n";
             }

2 个答案:

答案 0 :(得分:0)

我认为你有2个错误。首先是脚本在30秒时超时,您可以在脚本顶部使用此行轻松修复

set_time_limit(0); //0 means unlimited

其次是警告您尝试循环的东西不是数组或对象。您的数据集可能在某处有一个空子数组。您可以通过将foreach循环包装在

中来解决此问题
if(is_array($array)){ //or is_object() if you expect an object
    //do foreach in here
}

答案 1 :(得分:0)

<?php
class Algorithm {

public function find_frequent_itemsets($transactions, $minimum_support){

    $processedTransactions = array();
    $items = array();
    //var_dump($transactions);
    foreach($transactions as $transaction){
        if(!is_array($transaction)) 
            continue;
        $processed = array();

        foreach($transaction as $item){

            if(array_key_exists($item, $items)){
                $items[$item] += 1;
            }else{

                $items[$item] = 1;
            }
            array_push($processed, $item);

        }// every transaction.

        array_push($processedTransactions, $processed);
    }

      foreach(array_keys($items) as $index){
        if($items[$index] < $minimum_support){
            unset($items[$index]);
        }
    }



    $sortedTransactions = array();
    //var_dump($processedTransactions);
    foreach($processedTransactions as $currentTransactions){
        $tmp = array();

        foreach($currentTransactions as $item){
            if(array_key_exists($item, $items)){
                // key exits = frequent item.
                //array_push($tmp, array($item, $items[$item]));
                $tmp[$item] = $items[$item];
            }
        }

        // sort based on most frequent item.
        arsort($tmp);
        //var_dump($tmp);
        array_push($sortedTransactions, array_keys($tmp));
    }

    //var_dump($sortedTransactions);
    // Create a Fp-Tree
    $tree = new FPTree();

    // Add all Transactions.
    foreach($sortedTransactions as $currentTransactions){
        $tree->add($currentTransactions);
    }




        for itemset in find_with_suffix(master, []):
            yield itemset


    foreach(find_with_suffix($tree, array(), $minimum_support) as $itemset){
        yield $itemset;
    }

}// end function`}`


function find_with_suffix($tree, $suffix, $minimum_support){
/* $element = ($item, $nodes)
   $item is the item it self like (a, b, ..., e)
   $nodes is the linked list for that item (e1 -> e2 -> e3 .... ) */
  foreach($tree->getItems() as $element){
    $item = $element[0];
    $nodes = $element[1];
    $support = 0;
    /* calculate support of each linked list. */
    foreach($nodes as $n){
        $support += $n->getCount();
    }

    if($support >= $minimum_support && !array_key_exists($item, $suffix)){
        $found_set = array_merge(array($item), $suffix);
        /* yields current found set */
        yield $found_set;




$condTree =  conditional_tree_from_paths($tree->prefixPaths($item),$minimum_support);


            foreach(find_with_suffix($condTree, $found_set, $minimum_support) as $s){
            yield $s;
        }
    }
}}function conditional_tree_from_paths($paths, $minimum_support){

$tree = new FPTree();
$conditionItem = null;
$items = array();

foreach($paths as $path){
    if($conditionItem == null){
        $conditionItem = end($path)->getItem();
        //reset($path);
    }

    $point = $tree->root;

    foreach($path as $node){
        //next_point = point.search(node.item)
        $nextPoint = $point->search($node->getItem());

        if($nextPoint == null){
            // Add a new node to the tree.
            array_push($items, $node->getItem());
            $count = ($node->getItem() == $conditionItem) ? $node->getCount() : 0;
            $nextPoint = new FPNode($tree, $node->getItem(), $count);
            $point->add($nextPoint);
            $tree->updateRoute($nextPoint);
        }
        $point = $nextPoint;
    }
}

// assert condition_item is not None
foreach($tree->prefixPaths($conditionItem) as $path){
    $count = end($path)->getCount();
    //reset($path);
    foreach(array_reverse(array_slice($path, 0, count($path) - 1)) as $node){
        $tmpCount = $node->getCount();
        $node->setCount($tmpCount + $count);
    }
}

// as set.
$items = array_unique($items);
if (is_array($items) || is_object($items)){

foreach($items as $item){

    $currentSupport = 0;
    foreach($tree->getNodes($item) as $n){
        $currentSupport += $n->getCount();
    }

    if($currentSupport < $minimum_support){



        foreach($tree->getNodes($item) as $n){
            $parent = $n->getParent();
            if($parent != null){
                $parent->remove($n);
            }
        }


    }
}
}
// Remove leaves .
foreach($tree->getNodes($conditionItem) as $node){
    $parent = $node->getParent();
    if($parent != null){
        $parent->remove($node);
    }
}

return $tree;}

}
class FPNode {
var $tree;
var $item;
var $count;
var $parent;
var $children;
var $neighbor;

/**
 * @param $tree
 * @param $item
 * @param int $count
 */
public function __construct($tree, $item, $count=1){
    $this->tree = $tree;
    $this->item = $item;
    $this->count = $count;
    $this->parent = null;
    $this->children = array();
    $this->neighbor = null;
}

/**
 * @param $child
 */
public function add($child){
    $item = $child->getItem();
    if(!array_key_exists($item, $this->children)){
        $child->setParent($this);
        $this->children[$item] = $child;
    }
}


/**
 * @param $item
 * @return null
 */
public function search($item){
    //return array_key_exists($item, $this->children);
    if(array_key_exists($item, $this->children)){
        return $this->children[$item];
    }else{
        return null;
    }
}

/**
 * @param $child
 */
public function remove($child){
    $item = $child->getItem();
    if($this->search($item)){
        unset($this->children[$item]);
        $child->setParent(null);
        //self._tree._removed(child)
        $this->tree->removed($child);
        foreach($child->getChildren() as $sub_child){
            $sub_item = $sub_child->getItem();
            if($this->search($sub_item)){
                // self._children[sub_child.item]._count += sub_child.count
                $currentCount = $this->children[$sub_child->getItem()]->getCount();
                $subCount = $sub_child->getCount();
                $this->children[$sub_child->getItem()]->setCount($currentCount + $subCount);
                $sub_child->setParent(null);
            }else{
                $this->add($sub_child);
            }
        }
        $child->setChildren(null);
    }
}

/**
 *
 */
public function increment(){
    $this->count++;
}

/**
 * @return bool
 */
public function isRoot(){
    return $this->item == null && $this->count == null;
}
/**
 * @return mixed
 */
public function getTree()
{
    return $this->tree;
}

/**
 * @param mixed $tree
 */
public function setTree($tree)
{
    $this->tree = $tree;
}

/**
 * @return mixed
 */
public function getItem()
{
    return $this->item;
}

/**
 * @param mixed $item
 */
public function setItem($item)
{
    $this->item = $item;
}

/**
 * @return int
 */
public function getCount()
{
    return $this->count;
}

/**
 * @param int $count
 */
public function setCount($count)
{
    $this->count = $count;
}

/**
 * @return null
 */
public function getParent()
{
    return $this->parent;
}

/**
 * @param null $parent
 */
public function setParent($parent)
{
    $this->parent = $parent;
}

/**
 * @return null
 */
public function getNeighbor()
{
    return $this->neighbor;
}

/**
 * @param null $neighbor
 */
public function setNeighbor($neighbor)
{
    $this->neighbor = $neighbor;
}

/**
 * @return array
 */
public function getChildren()
{
    return $this->children;
}

/**
 * @param array $children
 */
public function setChildren($children)
{
    $this->children = $children;
}}class FPTree {

var $root;
var $routes;

public function __construct(){
    $this->root = new FPNode($this, null, null);
    // Item -> (head, tail)
    $this->routes = array();
}

public function add($transaction){
    $point = $this->root;

    foreach($transaction as $item){
        $nextPoint = $point->search($item);

        if($nextPoint != null){
            $nextPoint->increment();
        }else{
            // Null
            $nextPoint = new FPNode($this, $item, 1);
            $point->add($nextPoint);
            $this->updateRoute($nextPoint);
        }

        $point = $nextPoint;
    }
}

public function updateRoute($point){
    $item = $point->getItem();

    if(array_key_exists($item, $this->routes)){
        $route = $this->routes[$item];
        $route[1]->setNeighbor($point);
        $this->routes[$item] = array($route[0], $point);
    }else{
        // $item not found.
        // add new pair with the same item.
        // used as linked list but (head, tail) only.
        $this->routes[$item] = array($point, $point);
    }
}

public function getItems(){
    $items = array();
    foreach(array_keys($this->routes) as $item){
        array_push($items, array($item, $this->getNodes($item)));
    }

    return $items;
}

public function getNodes($item){

    if(array_key_exists($item, $this->routes)){

        $node = $this->routes[$item][0];
        $nodes = array();


        while($node != null){
            array_push($nodes, $node);
            $node = $node->getNeighbor();
        }
        return $nodes;
    }else{
        return null;
    }
}

public function getPath($node){
    $path = array();
    while($node != null && !$node->isRoot()){
        array_push($path, $node);
        $node = $node->getParent();
    }
    return array_reverse($path);
}

public function prefixPaths($item){
    $paths = array();

    foreach($this->getNodes($item) as $node){
        array_push($paths, $this->getPath($node));
    }

    return $paths;
}

public function removed($node){
    $item = $node->getItem();
    if(array_key_exists($item, $this->routes)){
        $route = $this->routes[$item];

        $head = $route[0];
        $tail = $route[1];

        if($node == $head){
            if($node == $tail || $node != $node->getNeighbor()){
                unset($this->routes[$item]);
            }else{

                $this->routes[$item] = array($node->getNeighbor(), $tail);
            }
        }else{

            $nodes = $this->getNodes($item);
            foreach($nodes as $n){
                // if n.neighbor is node:
                if($n->getNeighbor() == $node){
                    $n->setNeighbor($node->getNeighbor());
                    if($node == $tail){
                        $this->routes[$item] = array($head, $n);
                    }
                }

            }
        }
    }
}

public function printTree(){
    echo "Tree\n";
    echo "Root";
    $this->inspect($this->root, 0);
}

public function inspect($node, $level){
    for($i = 0; $i < $level; ++$i){
        echo '..';
    }
    if($node != null){


        echo "(". $node->getItem() . ":" . $node->getCount() . ")" . " ==> ";
        foreach($node->getChildren() as $child){
            echo "(" . $child->getItem() . ":" . $child->getCount() . "), ";
        }
        echo "\n";
        foreach($node->getChildren() as $child){
            $this->inspect($child, $level + 1);
        }
    }
}

public function printNeighbors($item){
    $nodes = $this->getNodes($item);

    if($nodes != null){
        foreach($nodes as $node){
            echo "(" . $node->getItem() . ":" . $node->getCount() . ") -> ";
        }
    }
    echo "\n";
}

public function printAllPrefix($item){
    $paths = $this->prefixPaths($item);

    foreach($paths as $path){
        foreach($path as $node){
            echo "(" . $node->getItem() . ":" . $node->getCount() . ") -> ";
        }

        echo "\n";
    }
}

public function getRoot()
{
    return $this->root;
}


public function setRoot($root)
{
    $this->root = $root;
}


public function getRoutes()
{
    return $this->routes;
}


public function setRoutes ($routes)
{
    $this->routes = $routes;
}

 }

 ?>

 <?php 

 $transactions['0']['0']="AC";
 $transactions['0']['1']="LAPTOP";
 $transactions['0']['2']="TV";

 $transactions['1']['0']="AC";
 $transactions['1']['1']="TV";

 $transactions['2']['0']="MOUSE";
 $transactions['2']['1']="REMOTE";
 $transactions['2']['2']="UPS";

 $transactions['3']['0']="AC";
 $transactions['3']['1']="LAPTOP";
 $transactions['3']['2']="UPS";

 $transactions['4']['0']="AC";
 $transactions['4']['1']="LAPTOP";
 $transactions['4']['2']="TV";

 $transactions['5']['0']="AC";
 $transactions['5']['1']="LAPTOP";
 $transactions['5']['2']="MOUSE";
 $transactions['5']['3']="TV";

 $transactions['6']['0']="KEYBOARD";
 $transactions['6']['1']="STABILIZER";

 $transactions['7']['0']="MOUSE";
 $transactions['7']['1']="UPS";

 $transactions['8']['0']="REMOTE";
 $transactions['8']['1']="UPS";

 $transactions['9']['0']="AC";
 $transactions['9']['1']="LAPTOP";
 $transactions['9']['2']="MOUSE";
 $transactions['9']['3']="TV";

 $aa=new Algorithm();
 $minimum_support = 2;
 foreach($aa->find_frequent_itemsets($transactions,$minimum_support) as $itemset){
    echo '{' . implode(", ", $itemset) . '}' . "\n";
 }

 ?>