示例#1
0
 /**
  * Computes the beta vector from the given examples and labels. The
  * examples are represented as a sparse matrix where each row is an example
  * and each column a feature, and the labels as an array where each value
  * is either 1 or -1, corresponding to a positive or negative example. Note
  * that the first feature (column 0) corresponds to an intercept term, and
  * is equal to 1 for every example.
  *
  * @param object $X SparseMatrix of training examples
  * @param array $y example labels
  */
 function train(SparseMatrix $X, $y)
 {
     $n = $X->columns();
     $p = array_fill(0, $n, 0);
     $a = array_fill(0, $n, 0);
     $this->beta = array_fill(0, $n, 0.0);
     $beta =& $this->beta;
     foreach ($X as $i => $row) {
         foreach ($row as $j => $Xij) {
             if ($y[$i] == 1) {
                 $p[$j] += 1;
             } else {
                 $a[$j] += 1;
             }
         }
     }
     $beta[0] = $this->logit($p[0], $a[0]);
     for ($j = 1; $j < $n; $j++) {
         $beta[$j] = $this->logit($p[$j], $a[$j]) - $beta[0];
     }
 }
示例#2
0
 /**
  * Converts a SparseMatrix into an InvertedData instance. The data is
  * duplicated.
  *
  * @param object $X SparseMatrix instance to convert
  */
 function __construct(SparseMatrix $X)
 {
     $this->rows = $X->rows();
     $this->columns = $X->columns();
     $this->data = array();
     $this->index = array();
     foreach ($X as $i => $row) {
         foreach ($row as $j => $Xij) {
             $this->data[] = array($j, $i, $Xij);
         }
     }
     sort($this->data);
     $lastVar = -1;
     foreach ($this->data as $dataOffset => $x) {
         $currVar = $x[0];
         if ($currVar != $lastVar) {
             for ($var = $lastVar + 1; $var <= $currVar; $var++) {
                 $this->index[$var] = $dataOffset;
             }
             $lastVar = $currVar;
         }
     }
 }