public function randomWeightedSubsetWithReplacement(int $n, array $weights) : self
    {
        if ($n < 1) {
            throw new InvalidArgumentException('Cannot generate a'
                . " subset of less than 1 sample, $n given.");
        }

        if (count($weights) !== count($this->samples)) {
            throw new InvalidArgumentException('The number of weights'
                . ' must be equal to the number of samples in the'
                . ' dataset, ' . count($this->samples) . ' needed'
                . ' but ' . count($weights) . ' given.');
        }

        $total = array_sum($weights);
        $max = (int) round($total * self::PHI);

        $samples = $labels = [];

        for ($i = 0; $i < $n; $i++) {
            $delta = rand(0, $max) / self::PHI;

            foreach ($weights as $index => $weight) {
                $delta -= $weight;

                if ($delta <= 0.) {
                    $samples[] = $this->samples[$index];
                    $labels[] = $this->labels[$index];

                    break 1;
                }
            }
        }

        return self::quick($samples, $labels);
    }
public function randomWeightedSubsetWithReplacement(int $n, array $weights) : self
    {
        if ($n < 1) {
            throw new InvalidArgumentException('Cannot generate a'
                . " subset of less than 1 sample, $n given.");
        }

        if (count($weights) !== count($this->samples)) {
            throw new InvalidArgumentException('The number of weights'
                . ' must be equal to the number of samples in the'
                . ' dataset, ' . count($this->samples) . ' needed'
                . ' but ' . count($weights) . ' given.');
        }

        $total = array_sum($weights);
        $max = (int) round($total * self::PHI);

        $samples = $labels = [];

        for ($i = 0; $i < $n; $i++) {
            $delta = rand(0, $max) / self::PHI;

            foreach ($weights as $index => $weight) {
                $delta -= $weight;

                if ($delta <= 0.) {
                    $samples[] = $this->samples[$index];
                    $labels[] = $this->labels[$index];

                    break 1;
                }
            }
        }

        return self::quick($samples, $labels);
    }
public function randomWeightedSubsetWithReplacement ( int $ n , array $ weights ) : self { if ( $ n < 1 ) { throw new InvalidArgumentException ( 'Cannot generate a' . " subset of less than 1 sample, $n given." ) ; } if ( count ( $ weights ) !== count ( $ this -> samples ) ) { throw new InvalidArgumentException ( 'The number of weights' . ' must be equal to the number of samples in the' . ' dataset, ' . count ( $ this -> samples ) . ' needed' . ' but ' . count ( $ weights ) . ' given.' ) ; } $ total = array_sum ( $ weights ) ; $ max = ( int ) round ( $ total * self :: PHI ) ; $ samples = $ labels = [ ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ delta = rand ( 0 , $ max ) / self :: PHI ; foreach ( $ weights as $ index => $ weight ) { $ delta -= $ weight ; if ( $ delta <= 0. ) { $ samples [ ] = $ this -> samples [ $ index ] ; $ labels [ ] = $ this -> labels [ $ index ] ; break 1 ; } } } return self :: quick ( $ samples , $ labels ) ; }
Generate a random weighted subset with replacement.

@param int $n
@param (int|float)[] $weights
@throws \InvalidArgumentException
@return self
Generate a random weighted subset with replacement .