Esempio n. 1
0
 /**
  * Constructor function
  * 
  * @param array $observations An array of 1 x n matrices, where n is how many dimensions the data has
  * @param int $k The number of clusters to use
  */
 public function __construct(array $observations, $k, GeneratorInterface $gen, MatrixBuilder $mBuilder, MatrixMath $mMath)
 {
     $this->generator = $gen;
     $this->mbuilder = $mBuilder;
     $this->mmath = $mMath;
     for ($i = 0; $i < $k; $i++) {
         $this->centroids[] = $observations[0];
         //Fill centroids with vectors.  The specific values will be overwritten before use.
     }
     foreach ($observations as $observation) {
         $obs_array = array();
         $obs_array['coordinates'] = $observation;
         $obs_array['centroid'] = $this->generator->generate(0, $k - 1);
         //Randomly assign observations to centroids, according to the Random Partition method.
         $this->observations[] = $obs_array;
     }
     //Iterate until convergence is reached. i.e. when cluster assignments no longer change
     $change = true;
     while ($change) {
         $this->update();
         $change = $this->assign();
     }
 }
Esempio n. 2
0
 /**
  *   Fetch a random float value
  *
  *   @access protected
  *   @return float the random value between 0 and 1
  */
 protected function randFloat()
 {
     return $this->generator->generate(0, $this->generator->max()) / $this->generator->max();
     //A number between 0 and 1.
 }
 /**
  *  Return the smallest possible random value
  *
  *  @access public
  *  @return double
  */
 public function min($value = null)
 {
     return $this->internal->min($value);
 }
Esempio n. 4
0
 /**
  *  Calculate a random numer of repeats given the current min-max range
  *
  *  @access public
  *  @param GeneratorInterface $generator
  *  @return Integer
  */
 public function calculateRepeatQuota(GeneratorInterface $generator)
 {
     $repeat_x = $this->getMinOccurances();
     if ($this->getOccuranceRange() > 0) {
         $repeat_x = (int) \round($generator->generate($this->getMinOccurances(), $this->getMaxOccurances()));
     }
     return $repeat_x;
 }
Esempio n. 5
0
 /**
  * This function is like rand
  *
  * @param array $weights
  * @param GeneratorInterface $random
  * @return float
  */
 public function getWeightedRand($weights, GeneratorInterface $random)
 {
     $r = $random->generate(1, 1000);
     $offset = 0;
     foreach ($weights as $k => $w) {
         $offset += $w * 1000;
         if ($r <= $offset) {
             return $k;
         }
     }
 }