__ __ __ __ _____ _ _ _____ _ _ _ | \/ | \ \ / / | __ \ (_) | | / ____| | | | | | \ / |_ __\ V / | |__) | __ ___ ____ _| |_ ___ | (___ | |__ ___| | | | |\/| | '__|> < | ___/ '__| \ \ / / _` | __/ _ \ \___ \| '_ \ / _ \ | | | | | | |_ / . \ | | | | | |\ V / (_| | || __/ ____) | | | | __/ | | |_| |_|_(_)_/ \_\ |_| |_| |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1 if you need WebShell for Seo everyday contact me on Telegram Telegram Address : @jackleetFor_More_Tools:
<?php
declare(strict_types=1);
namespace Phpml\Preprocessing;
use Phpml\Exception\InvalidArgumentException;
final class OneHotEncoder implements Preprocessor
{
/**
* @var bool
*/
private $ignoreUnknown;
/**
* @var array
*/
private $categories = [];
public function __construct(bool $ignoreUnknown = false)
{
$this->ignoreUnknown = $ignoreUnknown;
}
public function fit(array $samples, ?array $targets = null): void
{
foreach (array_keys(array_values(current($samples))) as $column) {
$this->fitColumn($column, array_values(array_unique(array_column($samples, $column))));
}
}
public function transform(array &$samples, ?array &$targets = null): void
{
foreach ($samples as &$sample) {
$sample = $this->transformSample(array_values($sample));
}
}
private function fitColumn(int $column, array $values): void
{
$count = count($values);
foreach ($values as $index => $value) {
$map = array_fill(0, $count, 0);
$map[$index] = 1;
$this->categories[$column][$value] = $map;
}
}
private function transformSample(array $sample): array
{
$encoded = [];
foreach ($sample as $column => $feature) {
if (!isset($this->categories[$column][$feature]) && !$this->ignoreUnknown) {
throw new InvalidArgumentException(sprintf('Missing category "%s" for column %s in trained encoder', $feature, $column));
}
$encoded = array_merge(
$encoded,
$this->categories[$column][$feature] ?? array_fill(0, count($this->categories[$column]), 0)
);
}
return $encoded;
}
}
| Name | Type | Size | Permission | Actions |
|---|---|---|---|---|
| Imputer | Folder | 0777 |
|
|
| ColumnFilter.php | File | 1.01 KB | 0777 |
|
| Imputer.php | File | 1.94 KB | 0777 |
|
| LabelEncoder.php | File | 1.01 KB | 0777 |
|
| LambdaTransformer.php | File | 589 B | 0777 |
|
| Normalizer.php | File | 3 KB | 0777 |
|
| NumberConverter.php | File | 1.15 KB | 0777 |
|
| OneHotEncoder.php | File | 1.76 KB | 0777 |
|
| Preprocessor.php | File | 136 B | 0777 |
|