Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i am working on Bayesian code for classification. i have error of undefined index in learning and classify function in undefined index. i used the code from http://www.ibm.com/developerworks/library/wa-bayes3/

coding

  <?php
    /**
    * @package NaiveBayes
    * @author  Paul Meagher <[email protected]> 
    * @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 />";
        }
      }


    }
    ?>

i have below error in its

Notice: Undefined index: q1 in C:\wamp\www\Bayes3\NaiveBayes.php on line 136 @  `$attribute_value = $row[$attribute];`  
    Notice: Undefined index: 0 in C:\wamp\www\Bayes3\NaiveBayes.php on line 137 @

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

     Notice: Undefined index: q1 in C:\wamp\www\Bayes3\NaiveBayes.php on line 137
      Notice: Undefined index: in C:\wamp\www\Bayes3\NaiveBayes.php on line 137
    Notice: Undefined index: q2 in C:\wamp\www\Bayes3\NaiveBayes.php on line 137
     Notice: Undefined index: in C:\wamp\www\Bayes3\NaiveBayes.php on line 137
    Notice: Undefined index: q3 in C:\wamp\www\Bayes3\NaiveBayes.php on line 137
     Notice: Undefined index: 1 in C:\wamp\www\Bayes3\NaiveBayes.php on line 137
    Notice: Undefined index: q1 in C:\wamp\www\Bayes3\NaiveBayes.php on line 136
     Notice: Undefined index: q2 in C:\wamp\www\Bayes3\NaiveBayes.php on line 136

    Notice: Undefined index: 0 in C:\wamp\www\Bayes3\NaiveBayes.php on line 156 @


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

    Notice: Undefined index: 1 in C:\wamp\www\Bayes3\NaiveBayes.php on line 156 @
share|improve this question
Code with errors should be posted on Stack Overflow instead. – Jamal Jun 19 at 21:48
sir they said that post your code here – asmii Jun 20 at 14:19
This are php warnings. I assume the original author just ignored them. They are thrown if any segment of the array did not exists. Check them with isset and initialize them with empty array were needed. – mnhg 2 days ago

closed as off topic by svick, Yuushi, Jeff Vanzella, Glenn Rogers, mnhg 2 days ago

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

Browse other questions tagged or ask your own question.