Generate a random weighted subset with replacement.

@param int $n
@param (int|float)[] $weights
@throws \InvalidArgumentException
@return self
186430
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);
    }
946843
public function setN($n)
    {
        if (is_int($n) || (gettype($n) === 'string' && Format::isVariableRef($n))) {
            $this->n = $n;
        } else {
            $msg = "The n attribute must be an integer or a variable reference, '" . gettype($n) . "' given.";
            throw new InvalidArgumentException($msg);
        }
    }
16493
public function limit(int $n): self
    {
        return new self(new Limit($this->getIterator(), 0, $n), $this->type);
    }
361796
protected function sumWeights(array $weights, string $vatNumber, int $start = 0): int
    {
        $checkval = 0;
        $count = count($weights);
        for ($i = $start; $i < $count; $i++) {
            $checkval += (int)$vatNumber[$i] * $weights[$i];
        }

        return $checkval;
    }
1324927
public int randomIndexChoice(List<Integer> weights) {
        int result = 0, index;
        double maxKey = 0.0;
        double u, key;
        int weight;

        for (ListIterator<Integer> it = weights.listIterator(); it.hasNext(); ) {
            index = it.nextIndex();
            weight = it.next();
            u = random.nextDouble();
            key = Math.pow(u, (1.0/weight)); // Protect from zero division?

            if (key > maxKey) {
                maxKey = key;
                result = index;
            }
        }

        return result;
    }
175506
def init_weights(self):
        """"""

        self.W = np.random.randn(self.n_neurons, self.n_inputs) * np.sqrt(2 / self.n_inputs)
        self.b = np.zeros((self.n_neurons, 1))
152848
def weighted(weights, sample_size, with_replacement=False):
        
        assert sample_size <= len(weights), "The sample size must be smaller \
than or equal to the number of weights it's taken from."

        # Convert weights to floats
        weights = [float(w) for w in weights]
        weight_indexes = list(range(0, len(weights)))

        samples = []
        while len(samples) < sample_size:
            # Choice a weight
            sample = OneOf.weighted(weights)

            # Add the choosen weight to our samples
            samples.append(weight_indexes[sample])

            if not with_replacement:
                # Remove the weight from the list of weights we can select from
                del weights[sample]
                del weight_indexes[sample]

        return samples
1276185
public static function weightedMean(array $values, array $weights, ?int $n = null) : float
    {
        $n = $n ?? count($values);

        if ($n < 1) {
            throw new InvalidArgumentException('Mean is undefined for empty'
                . ' set.');
        }

        if (count($weights) !== $n) {
            throw new InvalidArgumentException('The number of weights must'
                . ' equal the number of values.');
        }

        $total = array_sum($weights) ?: EPSILON;

        $temp = 0.;

        foreach ($values as $i => $value) {
            $temp += $value * ($weights[$i] ?: EPSILON);
        }

        return $temp / $total;
    }
970338
private function fromZigZag($n)
    {
        if (PHP_INT_SIZE === 4) {
            $n = gmp_init($n, 10);
        }
        $result = ($n >> 1) ^ -($n & 1);
        if (PHP_INT_SIZE === 4) {
            $result = gmp_strval($result, 10);
        }

        return $result;
    }
753799
public function setWeight($weight)
    {

        if (!is_null($weight) && ($weight > 100)) {
            throw new \InvalidArgumentException('invalid value for $weight when calling CreateSenderIps., must be smaller than or equal to 100.');
        }
        if (!is_null($weight) && ($weight < 1)) {
            throw new \InvalidArgumentException('invalid value for $weight when calling CreateSenderIps., must be bigger than or equal to 1.');
        }

        $this->container['weight'] = $weight;

        return $this;
    }
54135
public function insert($data, $weight = 0)
    {
        if ($this->isEmpty()) {
            $this->_nodes[] = ['data' => $data, 'weight' => $weight];
            return $this;
        }

        $inserted = false;
        $new = [];
        while(count($this->_nodes) > 0) {
            $element = array_shift($this->_nodes);
            if (
                !$inserted &&
                $this->compare((int) $weight, $element['weight']) <= 0
            ) {
                $new[] = ['data' => $data, 'weight' => (int) $weight];
                $new[] = ['data' => $element['data'], 'weight' => $element['weight']];
                $inserted = true;
            } else {
                $new[] = ['data' => $element['data'], 'weight' => $element['weight']];
            }
        }
        if (!$inserted) {
            $new[] = ['data' => $data, 'weight' => $weight];
        }
        $this->_nodes = $new;
        return $this;
    }
1285013
function only ($n)
  {
    $this->setIterator ($it = new LoopIterator ($this->getIterator ()));
    $it->limit ($n);
    return $this;
  }
699347
public static function mean(array $values, ?int $n = null) : float
    {
        $n = $n ?? count($values);

        if ($n < 1) {
            throw new InvalidArgumentException('Mean is undefined for empty'
                . ' set.');
        }

        return array_sum($values) / $n;
    }
43754
public function generate(int $n) : Dataset
    {
        $t = Vector::rand($n)->multiply(2)->add(1)
            ->multiply(1.5 * M_PI);

        $x = $t->multiply($t->cos());
        $y = Vector::rand($n)->multiply($this->depth);
        $z = $t->multiply($t->sin());

        $noise = Matrix::gaussian($n, 3)
            ->multiply($this->noise);
            
        $samples = Matrix::stack([$x, $y, $z])
            ->transpose()
            ->add($noise)
            ->multiply($this->scale)
            ->add($this->center)
            ->asArray();

        $labels = $t->asArray();

        return Labeled::quick($samples, $labels);
    }
807563
function SetFieldWeights ( $weights )
    {
        assert ( is_array($weights) );
        foreach ( $weights as $name=>$weight )
        {
            assert ( is_string($name) );
            assert ( is_int($weight) );
        }
        $this->_fieldweights = $weights;
    }
151374
public static function gaussian(int $m, int $n) : self
    {
        if ($m < 1 or $n < 1) {
            throw new InvalidArgumentException('Dimensionality must be'
                . ' greater than 0 on all axes.');
        }

        $a = $extras = [];

        for ($i = 0; $i < $m; $i++) {
            $row = [];
            
            if (!empty($extras)) {
                $row[] = array_pop($extras);
            }

            for ($j = count($row); $j < $n; $j += 2) {
                $r1 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
                $r2 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;

                $r = sqrt(-2. * log($r1));

                $row[] = $r * sin($r2 * self::TWO_PI);
                $row[] = $r * cos($r2 * self::TWO_PI);
            }

            if (count($row) > $n) {
                $extras[] = array_pop($row);
            }

            $a[] = $row;
        }

        return self::quick($a);
    }
19329
public static function gaussian(int $n) : self
    {
        if ($n < 1) {
            throw new InvalidArgumentException('The number of elements'
                . " must be greater than 0, $n given.");
        }

        $a = [];

        for ($i = 0; $i < $n; $i += 2) {
            $r1 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
            $r2 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
            
            $r = sqrt(-2. * log($r1));

            $a[] = $r * sin($r2 * self::TWO_PI);
            $a[] = $r * cos($r2 * self::TWO_PI);
        }

        if (count($a) > $n) {
            $a = array_slice($a, 0, $n);
        }

        return static::quick($a);
    }