Transform the dataset in place.

@param array $samples
@throws \RuntimeException
432623
public function transform(array &$samples) : void
    {
        if ($this->idfs === null) {
            throw new RuntimeException('Transformer has not been fitted.');
        }

        foreach ($samples as &$sample) {
            foreach ($sample as $column => &$feature) {
                if ($feature > 0) {
                    $feature *= $this->idfs[$column];
                }
            }
        }
    }
314146
public function transform(array &$samples) : void
    {
        foreach ($samples as &$sample) {
            $vectors = [];

            foreach ($sample as $column => $image) {
                if (is_resource($image) ? get_resource_type($image) === 'gd' : false) {
                    $width = imagesx($image);
                    $height = imagesy($image);

                    $vector = [];

                    for ($x = 0; $x < $width; $x++) {
                        for ($y = 0; $y < $height; $y++) {
                            $pixel = imagecolorat($image, $x, $y);

                            $vector[] = $pixel & 0xFF;
            
                            for ($i = 8; $i <= $this->mu; $i *= 2) {
                                $vector[] = ($pixel >> $i) & 0xFF;
                            }
                        }
                    }

                    unset($sample[$column]);

                    imagedestroy($image);

                    $vectors[] = $vector;
                }
            }

            $sample = array_merge($sample, ...$vectors);
        }
    }
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);
    }
1239098
protected function transform($data)
    {
        if (!$data instanceof GetSchema\Schema) {
            throw new Exception('Data is not a Schema.');
        }

        $definition = $data->getTableDefinition($this->table);
        $defSchema = $definition->getSchema();

        $returnData = [];
        $row = [
            self::KEY_TABLE => $definition->getName(),
            0 => $definition->getName(),
            self::KEY_CREATE_TABLE => $defSchema,
            1 => $defSchema,
        ];
        $returnData[] = $row;

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

        if (!is_array($sample[0])) {
            $sample = [$sample];
        }

        $sample = $this->normalize($sample, count($sample[0]));

        return $this->reduce($sample);
    }
704303
private static function sampleRow(array $sample): string
    {
        $row = [];
        foreach ($sample as $index => $feature) {
            $row[] = sprintf('%s:%s', $index + 1, $feature);
        }

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

        if (!is_array($sample[0])) {
            $sample = [$sample];
        }

        return $this->reduce($sample);
    }
182451
private function antecedents(array $sample): array
    {
        $cardinality = count($sample);
        $antecedents = $this->powerSet($sample);

        return array_filter($antecedents, function ($antecedent) use ($cardinality) {
            return (count($antecedent) != $cardinality) && ($antecedent != []);
        });
    }
457342
private function powerSet(array $sample): array
    {
        $results = [[]];
        foreach ($sample as $item) {
            foreach ($results as $combination) {
                $results[] = array_merge([$item], $combination);
            }
        }

        return $results;
    }
1223107
protected function createInstances(): DataSet
    {
        //calculating the new Average for the new DataSet (which might differ from the raw one depending on cleaning policies applied)
        $this->average = [];
        //got through inputs matrix
        foreach ($this->inputsMatrix as $index => &$inputs) {

            foreach ($inputs as $dimKey => &$value) {
                if ($value === Policy::AVG) {
                    $value = $this->rawAverage[$dimKey];
                } elseif ($value === Policy::MOST_COMMON) {
                    $value = array_search(max($this->frequency[$dimKey]), $this->frequency[$dimKey]) ? : 0;
                }
                $this->average[0][$dimKey] = $this->average[0][$dimKey] ?? 0;
                $this->average[0][$dimKey] += floatval($value) / $this->size;
            }

            foreach ($this->outputsMatrix[$index] as $outputKey => &$value) {
                if ($value === Policy::AVG) {
                    $value = $this->rawAverage[$outputKey];
                } elseif ($value === Policy::MOST_COMMON) {
                    $value = array_search(max($this->frequency[$outputKey]), $this->frequency[$outputKey]) ? : 0;
                }

                $this->average[1][$outputKey] = $this->average[1][$outputKey] ?? 0;
                $this->average[1][$outputKey] += floatval($value) / $this->size;
            }

            $this->instances[$index] = new Instance($this->inputsMatrix[$index], $this->outputsMatrix[$index]);
        }

        return $this;
    }
626779
public function train(Dataset $dataset) : void
    {
        DatasetIsCompatibleWithEstimator::check($dataset, $this);

        [$m, $n] = $dataset->shape();

        $this->r = Matrix::gaussian($n, $this->estimators);

        if ($n >= self::MIN_SPARSE_DIMENSIONS) {
            $mask = Matrix::rand($n, $this->estimators)
                ->less(sqrt($n) / $n);

            $this->r = $this->r->multiply($mask);
        }

        $z = Matrix::quick($dataset->samples())
            ->matmul($this->r)
            ->transpose();

        foreach ($z as $values) {
            $min = min($values) - EPSILON;
            $max = max($values) + EPSILON;

            $edges = Vector::linspace($min, $max, $this->bins + 1)->asArray();

            $edges[] = INF;

            $counts = array_fill(0, $this->bins + 2, 0);

            $interior = array_slice($edges, 1, $this->bins);

            foreach ($values as $value) {
                foreach ($interior as $k => $edge) {
                    if ($value < $edge) {
                        $counts[$k]++;

                        continue 2;
                    }
                }

                $counts[$this->bins]++;
            }

            $densities = [];

            foreach ($counts as $count) {
                $densities[] = $count > 0
                    ? -log($count / $m)
                    : -LOG_EPSILON;
            }

            $this->histograms[] = [$edges, $counts, $densities];
        }

        $this->n = $m;
    }
1023816
public function update(Dataset $dataset) : void
    {
        if ($this->minimums === null or $this->maximums === null) {
            $this->fit($dataset);
            
            return;
        }

        $columns = $dataset->columnsByType(DataType::CONTINUOUS);

        foreach ($columns as $column => $values) {
            $min = min($values);
            $max = max($values);

            $min = min($min, $this->minimums[$column]);
            $max = max($max, $this->maximums[$column]);

            $scale = ($this->max - $this->min)
                / (($max - $min) ?: EPSILON);

            $minHat = $this->min - $min * $scale;

            $this->minimums[$column] = $min;
            $this->maximums[$column] = $max;
            $this->scales[$column] = $scale;
            $this->mins[$column] = $minHat;
        }
    }
1101414
protected function transform($data)
    {
        if (!$data instanceof GetSchema\Schema) {
            throw new Exception('Schema instance missing.');
        }

        $field = $this->specializedFIelds[0];
        $returnData = [];

        foreach ($data->getTableDefinitions() as $definition) {
            $returnData[] = [
                $field => $definition->getName(),
                0 => $definition->getName(),
            ];
        }

        return $returnData;
    }
878624
protected function localOutlierFactor(array $sample) : float
    {
        if (empty($this->lrds)) {
            throw new RuntimeException('Local reachability distances have'
                . ' not been computed, must train estimator first.');
        }

        [$indices, $distances] = $this->tree->nearest($sample, $this->k);

        $lrd = $this->localReachabilityDensity($indices, $distances);

        $lrds = [];
        
        foreach ($indices as $index) {
            $lrds[] = $this->lrds[$index];
        }

        $ratios = [];

        foreach ($lrds as $lHat) {
            $ratios[] = $lHat / $lrd;
        }

        return Stats::mean($ratios);
    }
201473
public function transform(array &$samples) : void
    {
        if ($this->strategies === null) {
            throw new RuntimeException('Transformer has not been fitted.');
        }

        foreach ($samples as $row => &$sample) {
            foreach ($sample as $column => &$feature) {
                if ($feature === $this->placeholder) {
                    $strategy = $this->strategies[$column];

                    $feature = $strategy->guess();
                }
            }
        }
    }
1209964
public function fit(Dataset $dataset) : void
    {
        $this->strategies = [];

        foreach ($dataset->types() as $column => $type) {
            $values = [];

            foreach ($dataset->column($column) as $value) {
                if ($value !== $this->placeholder) {
                    $values[] = $value;
                }
            }

            switch ($type) {
                case DataType::CATEGORICAL:
                    $strategy = clone $this->categorical;
                    break 1;

                case DataType::CONTINUOUS:
                    $strategy = clone $this->continuous;
                    break 1;

                default:
                    throw new InvalidArgumentException('This transformer'
                        . ' only handles categorical and continuous'
                        . ' features, ' . DataType::TYPES[$type]
                        . ' found.');
            }

            $strategy->fit($values);

            $this->strategies[$column] = $strategy;
        }
    }
399823
public function transform(array &$samples) : void
    {
        if ($this->intervals === null) {
            throw new RuntimeException('Transformer has not been fitted.');
        }
        
        $last = end($this->categories);

        foreach ($samples as &$sample) {
            foreach ($this->intervals as $column => $interval) {
                $value = &$sample[$column];

                foreach ($interval as $i => $edge) {
                    if ($value < $edge) {
                        $value = $this->categories[$i];

                        continue 2;
                    }
                }

                $value = $last;
            }
        }
    }