@param array $sample

@return mixed
967869
protected function predictSample(array $sample)
    {
        if (count($this->allLabels) == 2) {
            return $this->classifiers[0]->predictSampleBinary($sample);
        }

        $probs = [];

        foreach ($this->classifiers as $label => $predictor) {
            $probs[$label] = $predictor->predictProbability($sample, $label);
        }

        arsort($probs, SORT_NUMERIC);
        return key($probs);
    }
549409
public static function population(array $a, $sample = true)
    {
        if (empty($a)) {
            throw InvalidArgumentException::arrayCantBeEmpty();
        }

        $n = count($a);

        if ($sample && $n === 1) {
            throw InvalidArgumentException::arraySizeToSmall(2);
        }

        $mean = Mean::arithmetic($a);
        $carry = 0.0;
        foreach ($a as $val) {
            $d = $val - $mean;
            $carry += $d * $d;
        }

        if ($sample) {
            --$n;
        }

        return sqrt((float) ($carry / $n));
    }
397909
public function range(array $sample, float $radius) : array
    {
        if ($radius <= 0.) {
            throw new InvalidArgumentException('Radius must be'
                . " greater than 0, $radius given.");
        }

        $samples = $labels = $distances = [];

        $stack = [$this->root];

        while ($stack) {
            $current = array_pop($stack);

            if (!$current instanceof BinaryNode) {
                continue 1;
            }

            if ($current instanceof Cluster) {
                foreach ($current->samples() as $i => $neighbor) {
                    $distance = $this->kernel->compute($sample, $neighbor);

                    if ($distance <= $radius) {
                        $samples[] = $neighbor;
                        $labels[] = $current->label($i);
                        $distances[] = $distance;
                    }
                }

                continue 1;
            }

            foreach ($current->children() as $child) {
                if ($child instanceof Ball) {
                    $distance = $this->kernel->compute($sample, $child->center());

                    if ($distance <= ($child->radius() + $radius)) {
                        $stack[] = $child;
                    }
                }
            }
        }

        return [$samples, $labels, $distances];
    }
602165
public function transform(array &$samples) : void
    {
        if ($this->means === null or $this->stddevs === null) {
            throw new RuntimeException('Transformer has not been fitted.');
        }

        foreach ($samples as &$sample) {
            foreach ($this->stddevs as $column => $stddev) {
                $feature = $sample[$column];

                if ($this->center) {
                    $feature -= $this->means[$column];
                }

                $sample[$column] = $feature / $stddev;
            }
        }
    }
371021
public function transform(array &$samples) : void
    {
        if ($this->vocabulary === null) {
            throw new RuntimeException('Transformer is not fitted.');
        }

        $templates = [];

        foreach ($this->vocabulary as $column => $vocabulary) {
            $templates[$column] = array_fill(0, count($vocabulary), 0);
        }

        foreach ($samples as &$sample) {
            $vectors = [];

            foreach ($this->vocabulary as $column => $vocabulary) {
                $text = $sample[$column];

                $tokens = $this->tokenizer->tokenize($text);

                $counts = array_count_values($tokens);

                $features = $templates[$column];

                foreach ($counts as $token => $count) {
                    if (isset($vocabulary[$token])) {
                        $features[$vocabulary[$token]] = $count;
                    }
                }

                $vectors[] = $features;

                unset($sample[$column]);
            }

            $sample = array_merge($sample, ...$vectors);
        }
    }
974196
public static SimonType getValueFromInstance(Sample sample) {
		return sample == null ? null : getValueFromSampleType(sample.getClass());
	}
649886
public function transform(array &$samples) : void
    {
        if (!$this->r) {
            throw new RuntimeException('Transformer has not been fitted.');
        }

        $samples = Matrix::quick($samples)
            ->matmul($this->r)
            ->asArray();
    }
275594
public function extract(array $context)
    {
        $params = [];

        foreach ($this->fields as $field) {
            if (isset($context[$field])) {
                $params[$field] = $context[$field];
                unset($context[$field]);
            }
        }

        if (!empty($context)) {
            $params['context'] = $context;
        }

        return $params;
    }
128356
public function transform(array $sample): array
    {
        if (!$this->fit) {
            throw new InvalidOperationException('KernelPCA has not been fitted with respect to original dataset, please run KernelPCA::fit() first');
        }

        if (is_array($sample[0])) {
            throw new InvalidArgumentException('KernelPCA::transform() accepts only one-dimensional arrays');
        }

        $pairs = $this->getDistancePairs($sample);

        return $this->projectSample($pairs);
    }
190543
protected function jointLogLikelihood(array $sample) : array
    {
        $likelihoods = [];

        foreach ($this->probs as $class => $probs) {
            $likelihood = $this->priors[$class] ?? LOG_EPSILON;

            foreach ($sample as $column => $feature) {
                $likelihood += $probs[$column][$feature] ?? LOG_EPSILON;
            }

            $likelihoods[$class] = $likelihood;
        }

        return $likelihoods;
    }
253750
static public function mean(array $sample)
    {
        $count = count($sample);
        if ($count === 0) {
            return false;
        }

        $sum = 0;
        foreach ($sample as $s) {
            $sType = gettype($s);

            if ($sType !== 'integer' && $sType !== 'double') {
                // only deal with numeric values.
                return false;
            }

            $sum += $s;
        }

        return $sum / $count;
    }
463250
public function calculate_sample($sampleid, $sampleorigin, $notusedstarttime = false, $notusedendtime = false) {

        $context = $this->retrieve('context', $sampleid);

        if (is_null($this->studentroleids)) {
            $this->studentroleids = array_keys(get_archetype_roles('student'));
        }

        foreach ($this->studentroleids as $role) {
            // We look for roles, not enrolments as a student assigned at category level is supposed to be a
            // course student.
            $students = get_role_users($role, $context, false, 'u.id', 'u.id');
            if ($students) {
                return self::get_max_value();
            }
        }

        return self::get_min_value();
    }
317000
protected function isolationScore(array $sample) : float
    {
        $depth = 0.;

        foreach ($this->forest as $tree) {
            $node = $tree->search($sample);

            $depth += $node ? $node->depth() : EPSILON;
        }

        $depth /= $this->estimators;

        return 2. ** -($depth / $this->pHat);
    }
272913
protected function predictProbability(array $sample, $label)
    {
        $predicted = $this->predictSampleBinary($sample);

        if (strval($predicted) == strval($label)) {
            $sample = $this->checkNormalizedSample($sample);
            return abs($this->output($sample) - 0.5);
        }

        return 0.0;
    }
918530
public static function variance($values, $sample = true)
    {
        $numberOfValues = count($values);
        $mean = self::mean($values);

        $squaredDifferences = [];
        foreach ($values as $value) {
            $squaredDifferences[] = self::squaredDifference($value, $mean);
        }
        $sumOfSquaredDifferences = self::sum($squaredDifferences);

        if ($sample) {
            $variance = $sumOfSquaredDifferences / ($numberOfValues - 1);
        } else {
            $variance = $sumOfSquaredDifferences / $numberOfValues;
        }

        return $variance;
    }
706067
protected static function setSampled($sampler)
    {
        if ($sampler === null) {
            static::$sampled = true;
            return null;
        }

        if (is_bool($sampler)) {
            static::$sampled = $sampler;
            return null;
        }

        if (!is_bool($sampler) && !($sampler instanceof Sampler)) {
            throw new \InvalidArgumentException('$sampler must be instance of Sampler or boolean');
        }

        static::$sampled = $sampler->isSampled();
        return null;
    }
647504
public function search(array $sample) : ?Neighborhood
    {
        $current = $this->root;

        while ($current) {
            if ($current instanceof Coordinate) {
                if ($sample[$current->column()] < $current->value()) {
                    $current = $current->left();
                } else {
                    $current = $current->right();
                }

                continue 1;
            }

            if ($current instanceof Neighborhood) {
                return $current;
            }
        }

        return null;
    }