Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow normalizers to skip NaN values #333

Open
wants to merge 2 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Transformers/MaxAbsoluteScaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function update(Dataset $dataset) : void
foreach ($this->maxabs as $column => $oldMax) {
$values = $dataset->feature($column);

$max = max(array_map('abs', $values));
$max = max(array_map('abs', array_filter($values, 'is_finite') ?: [0]));

$max = max($oldMax, $max);

Expand Down
12 changes: 10 additions & 2 deletions src/Transformers/MinMaxNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ public function fit(Dataset $dataset) : void
$values = $dataset->feature($column);

/** @var int|float $min */
$min = min($values);
$min = min(array_filter($values, 'is_finite') ?: [0]);

/** @var int|float $max */
$max = max($values);
$max = max(array_filter($values, 'is_finite') ?: [0]);

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

Expand Down Expand Up @@ -199,6 +199,10 @@ public function transform(array &$samples) : void
foreach ($this->scales as $column => $scale) {
$value = &$sample[$column];

if (!is_finite($value)) {
continue;
}

$min = $this->minimums[$column];

$value *= $scale;
Expand All @@ -224,6 +228,10 @@ public function reverseTransform(array &$samples) : void
foreach ($this->scales as $column => $scale) {
$value = &$sample[$column];

if (!is_finite($value)) {
continue;
}

$min = $this->minimums[$column];

$value -= $this->min - $min * $scale;
Expand Down
11 changes: 11 additions & 0 deletions tests/Transformers/MaxAbsoluteScalerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@

$this->transformer->reverseTransform($samples);
}

/**
* @test
*/
public function skipsNonFinite(): void
{
$samples = Unlabeled::build([[0.0, 3000.0, NAN, -6.0], [1.0, 30.0, NAN, 0.001]]);

Check failure on line 118 in tests/Transformers/MaxAbsoluteScalerTest.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.

Check failure on line 118 in tests/Transformers/MaxAbsoluteScalerTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.

Check failure on line 118 in tests/Transformers/MaxAbsoluteScalerTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.

Check failure on line 118 in tests/Transformers/MaxAbsoluteScalerTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.
$this->transformer->fit($samples);
$this->assertNan($samples[0][2]);
$this->assertNan($samples[1][2]);
}
}
11 changes: 11 additions & 0 deletions tests/Transformers/MinMaxNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,15 @@

$this->transformer->transform($samples);
}

/**
* @test
*/
public function skipsNonFinite(): void
{
$samples = Unlabeled::build([[0.0, 3000.0, NAN, -6.0], [1.0, 30.0, NAN, 0.001]]);

Check failure on line 111 in tests/Transformers/MinMaxNormalizerTest.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.

Check failure on line 111 in tests/Transformers/MinMaxNormalizerTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.

Check failure on line 111 in tests/Transformers/MinMaxNormalizerTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.

Check failure on line 111 in tests/Transformers/MinMaxNormalizerTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 on ubuntu-latest

Call to static method build() on an unknown class Rubix\ML\Tests\Transformers\Unlabeled.
$this->transformer->fit($samples);
$this->assertNan($samples[0][2]);
$this->assertNan($samples[1][2]);
}
}
Loading