0

我从站点获取贝叶斯代码http://www.ibm.com/developerworks/library/wa-bayes3/ 我无法从选择查询中删除未定义的错误。这是代码:

     <?php
/**
* @package NaiveBayes
* @author  Paul Meagher <paul@datavore.com> 
* @license PHP License v3.0    
* @version 0.2
*
* This class must be supplied with training example data, attribute names, 
* and class names.  Once this information is supplied, you can then invoke
* the learn and classify methods. 
*/
class NaiveBayes {

  /**
  * Database table to use.  
  */
  var $table = null;      

  /**  
  * 1D array of attributes names.  Attribute names should
  * correspond to field names in the specified database 
  * table. 
  */
  var $attributes = array();   

  /** 
  * 1D array of attribute values to classify.
  */
  var $attribute_values = null;     

  /**  
  * Specifies table column holding classification values.
  */
  var $class = array();   

  /**  
  * Specifies allowable classification values.
  */
  var $class_values = array();   

  /**
  * 3D array containing joint frequency infomation about  
  * how class names and attribute names covary.  Used to 
  * derive the likelihood array in the classify method.
  */
  var $joint_frequency = array();   

  /**  
  * 1D array containing prior probability of each class name.
  */
  var $priors = array();   

  /**    
  * 1D array containing likeliood of the data for each class.
  */
  var $likelihoods = array();   

  /**      
  * 1D array containing the posterior probability of each class 
  * name given the supplied attribute values.
  */
  var $posterior = array();   

  /** 
  * Denotes the number of training examples used for learning.
  */
  var $n = 0;      

  /** 
  * When the classifier method is called, $predict is set to 
  * class name with the highest posterior probability.         
  */
  var $predict = null;     

  /**  
  * Set database table to use.
  */
  function setTable($table) 
  {    
    $this->table = $table;
  }

  /**  
  * Set attribute columns to use.  The attribute columns should 
  * correspond to fields in your database table.
  */
  function setAttributes($columns)
   {
    foreach($columns as $column)

     {
      $this->attributes[] = $column;
    }
  }

  /**  
  * Set classification column names to use. 
  */  
  function setClass($column) {
    $this->class = $column;
  }

  /**  
  * Set classification names to use. 
  */  
  function setClassValues($values) {
    foreach($values as $value) {
      $this->class_values[] = $value;
    }
  }

  function inimat($lr, $ur, $lc, $uc, &$a, $x){
    for(; $lr<=$ur; $lr++)
      for($j=$lc; $j<=$uc; $j++) $a[$lr][$j]=$x;
  }


/**  
 * Learn the prior probability of each class and the 
 * joint frequency of each class and attribute.
 */
function learn()
{
    // include connection file  
    include("connect.php");
    // parse array  
    foreach ($this->attributes as $attribute)
    // get field list   
        $field_list = substr($attribute, 0, -1);
    // parse array  
    foreach ($this->class_values as $class) {
        // make an sql query
        $sql                    = "SELECT $field_list FROM " . $this->table . " WHERE " . $this->class . "='$class'";
        // execute sql query      
        $result                 = mysql_query($sql);
        $this->priors["$class"] = mysql_num_rows($result);
        while ($row = mysql_fetch_assoc($result)) 
        {
            foreach ($this->attributes as $attribute) {
                // if row attribute haven't any data then set default value
                $attribute_value = isset($row[$attribute]) ? $row[$attribute] : 0;
                $this->joint_frequency[$class][$attribute][$attribute_value]++;
            }
        }
    }
}


  /**  
  * Given a set of attribute values, this routine will 
  * predict the class they most likley belong to.
  */                          
  function classify($attribute_values) {    
    $this->attribute_values = $attribute_values;
    $this->n = array_sum($this->priors);
    $this->max = 0;
    foreach($this->class_values as $class) {    
      $counter = 0;
      $this->likelihoods[$class] = 1;      
      foreach($this->attributes as $attribute) {
        $attribute_value = $attribute_values[$counter];
        $joint_freq = $this->joint_frequency[$class][$attribute][$attribute_value];
        $likelihood = $joint_freq / $this->priors[$class]; 
        if ($joint_freq > 0) {
          $this->likelihoods[$class] = $this->likelihoods[$class] * $likelihood;
        }
        $counter++;
      }
      $prior = $this->priors[$class] / $this->n;
      $this->posterior[$class] = $this->likelihoods[$class] * $prior; 
      if ($this->posterior[$class] > $this->max) {
        $this->predict = $class;
        $this->max = $this->posterior[$class];
      } 
    }
  }

  /**
  * Output the posterior probabilities that were computed by 
  * the classify method.
  */
  function toHTML() {
    foreach($this->class_values as $class) {  
      $equation = "P($this->class = ". $class ." | ";        
      $counter = 0;
      foreach($this->attributes as $attribute) {    
        $attribute_value = $this->attribute_values[$counter];
        $equation .= $attribute ."=". $attribute_value ." &amp; ";
        $counter++;      
      }  
      $equation = substr($equation, 0, -7);                  
      $equation .= ") = ". $this->posterior[$class];
      if ($class == $this->predict) {
        $equation .= " <sup>*</sup>";
      }            
      echo $equation  ."<br />";
    }
  }


}
?>

它给出错误通知:未定义的索引:第 130 行 C:\wamp\www\Bayes3\NaiveBayes.php 中的 q1:$attribute_value = $row[$attribute];

注意:未定义的索引:0 在 C:\wamp\www\Bayes3\NaiveBayes.php 第 131 行:

  $this->joint_frequency[$class][$attribute][$attribute_value]++;

注意:未定义的索引:C:\wamp\www\Bayes3\NaiveBayes.php 中的 150 行:

$joint_freq = $this->joint_frequency[$class][$attribute][$attribute_value];

请帮助我

4

3 回答 3

0

您可以使用isset功能:

<?php
/**  
 * Learn the prior probability of each class and the 
 * joint frequency of each class and attribute.
 */
function learn()
{
    // include connection file  
    include("connect.php");
    // parse array  
    foreach ($this->attributes as $attribute)
    // get field list   
        $field_list = substr($attribute, 0, -1);
    // parse array  
    foreach ($this->class_values as $class) {
        // make an sql query
        $sql                    = "SELECT $field_list FROM " . $this->table . " WHERE " . $this->class . "='$class'";
        // execute sql query      
        $result                 = mysql_query($sql);
        $this->priors["$class"] = mysql_num_rows($result);
        while ($row = mysql_fetch_assoc($result)) {
            foreach ($this->attributes as $attribute) {
                // if row attribute haven't any data then set default value
                $attribute_value = isset($row[$attribute]) ? $row[$attribute] : 0;
                $this->joint_frequency[$class][$attribute][$attribute_value]++;
            }
        }
    }
}
?>
于 2013-06-18T12:48:13.623 回答
0

要抑制错误,您可以在引发错误的变量之前添加“@”符号。

添加@like $attribute_value = @$row[$attribute];

于 2013-06-18T13:17:31.987 回答
0

这可以通过 isset 函数完成

于 2013-06-18T17:26:28.463 回答