forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Preprocessing; | ||
|
||
final class LabelEncoder implements Preprocessor | ||
{ | ||
/** | ||
* @var int[] | ||
*/ | ||
private $classes = []; | ||
|
||
public function fit(array $samples, ?array $targets = null): void | ||
{ | ||
$this->classes = []; | ||
|
||
foreach ($samples as $sample) { | ||
if (!isset($this->classes[(string) $sample])) { | ||
$this->classes[(string) $sample] = count($this->classes); | ||
} | ||
} | ||
} | ||
|
||
public function transform(array &$samples): void | ||
{ | ||
foreach ($samples as &$sample) { | ||
$sample = $this->classes[(string) $sample]; | ||
} | ||
} | ||
|
||
public function inverseTransform(array &$samples): void | ||
{ | ||
$classes = array_flip($this->classes); | ||
foreach ($samples as &$sample) { | ||
$sample = $classes[$sample]; | ||
} | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function classes(): array | ||
{ | ||
return array_keys($this->classes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Tests\Preprocessing; | ||
|
||
use Phpml\Preprocessing\LabelEncoder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class LabelEncoderTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider labelEncoderDataProvider | ||
*/ | ||
public function testFitAndTransform(array $samples, array $transformed): void | ||
{ | ||
$le = new LabelEncoder(); | ||
$le->fit($samples); | ||
$le->transform($samples); | ||
|
||
self::assertEquals($transformed, $samples); | ||
} | ||
|
||
public function labelEncoderDataProvider(): array | ||
{ | ||
return [ | ||
[['one', 'one', 'two', 'three'], [0, 0, 1, 2]], | ||
[['one', 1, 'two', 'three'], [0, 1, 2, 3]], | ||
[['one', null, 'two', 'three'], [0, 1, 2, 3]], | ||
[['one', 'one', 'one', 'one'], [0, 0, 0, 0]], | ||
[['one', 'one', 'one', 'one', null, null, 1, 1, 2, 'two'], [0, 0, 0, 0, 1, 1, 2, 2, 3, 4]], | ||
]; | ||
} | ||
|
||
public function testResetClassesAfterNextFit(): void | ||
{ | ||
$samples = ['Shanghai', 'Beijing', 'Karachi']; | ||
|
||
$le = new LabelEncoder(); | ||
$le->fit($samples); | ||
|
||
self::assertEquals(['Shanghai', 'Beijing', 'Karachi'], $le->classes()); | ||
|
||
$samples = ['Istanbul', 'Dhaka', 'Tokyo']; | ||
|
||
$le->fit($samples); | ||
|
||
self::assertEquals(['Istanbul', 'Dhaka', 'Tokyo'], $le->classes()); | ||
} | ||
|
||
public function testFitAndTransformFullCycle(): void | ||
{ | ||
$samples = ['Shanghai', 'Beijing', 'Karachi', 'Beijing', 'Beijing', 'Karachi']; | ||
$encoded = [0, 1, 2, 1, 1, 2]; | ||
|
||
$le = new LabelEncoder(); | ||
$le->fit($samples); | ||
|
||
self::assertEquals(['Shanghai', 'Beijing', 'Karachi'], $le->classes()); | ||
|
||
$transformed = $samples; | ||
$le->transform($transformed); | ||
self::assertEquals($encoded, $transformed); | ||
|
||
$le->inverseTransform($transformed); | ||
self::assertEquals($samples, $transformed); | ||
} | ||
} |