Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]
1 <?php 2 3 declare(strict_types=1); 4 5 namespace Phpml\Preprocessing; 6 7 use Phpml\Exception\InvalidOperationException; 8 use Phpml\Preprocessing\Imputer\Strategy; 9 10 class Imputer implements Preprocessor 11 { 12 public const AXIS_COLUMN = 0; 13 14 public const AXIS_ROW = 1; 15 16 /** 17 * @var mixed 18 */ 19 private $missingValue; 20 21 /** 22 * @var Strategy 23 */ 24 private $strategy; 25 26 /** 27 * @var int 28 */ 29 private $axis; 30 31 /** 32 * @var mixed[] 33 */ 34 private $samples = []; 35 36 /** 37 * @param mixed $missingValue 38 */ 39 public function __construct($missingValue, Strategy $strategy, int $axis = self::AXIS_COLUMN, array $samples = []) 40 { 41 $this->missingValue = $missingValue; 42 $this->strategy = $strategy; 43 $this->axis = $axis; 44 $this->samples = $samples; 45 } 46 47 public function fit(array $samples, ?array $targets = null): void 48 { 49 $this->samples = $samples; 50 } 51 52 public function transform(array &$samples): void 53 { 54 if ($this->samples === []) { 55 throw new InvalidOperationException('Missing training samples for Imputer.'); 56 } 57 58 foreach ($samples as &$sample) { 59 $this->preprocessSample($sample); 60 } 61 } 62 63 private function preprocessSample(array &$sample): void 64 { 65 foreach ($sample as $column => &$value) { 66 if ($value === $this->missingValue) { 67 $value = $this->strategy->replaceValue($this->getAxis($column, $sample)); 68 } 69 } 70 } 71 72 private function getAxis(int $column, array $currentSample): array 73 { 74 if ($this->axis === self::AXIS_ROW) { 75 return array_diff($currentSample, [$this->missingValue]); 76 } 77 78 $axis = []; 79 foreach ($this->samples as $sample) { 80 if ($sample[$column] !== $this->missingValue) { 81 $axis[] = $sample[$column]; 82 } 83 } 84 85 return $axis; 86 } 87 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body