Skip to content

Commit 6d6c467

Browse files
authored
Add missing and constant support for ZScore (#308)
* Add missing and constant support for ZScore * Update test data
1 parent 2234706 commit 6d6c467

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/transforms/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# Licensed under the MIT License. See LICENSE in the project root.
33
# ------------------------------------------------------------------
44

5-
zscore(x, μ, σ) = @. (x - μ) / σ
5+
zscore(x, μ, σ) = iszero(σ) ? fill(zero(μ), length(x)) : @. (x - μ) / σ
66

7-
revzscore(y, μ, σ) = @. σ * y + μ
7+
revzscore(y, μ, σ) = iszero(σ) ? fill(μ, length(y)) : @. σ * y + μ
88

99
_assert(cond, msg) = cond || throw(AssertionError(msg))
1010

src/transforms/zscore.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ assertions(transform::ZScore) = [scitypeassert(Continuous, transform.selector)]
4141
isrevertible(::Type{<:ZScore}) = true
4242

4343
function colcache(::ZScore, x)
44-
μ = mean(x)
45-
σ = std(x, mean=μ)
44+
μ = mean(skipmissing(x))
45+
σ = std(skipmissing(x), mean=μ)
4646
=μ, σ=σ)
4747
end
4848

test/data/eigenanalysis-2.png

68.8 KB
Loading

test/transforms/zscore.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,24 @@
8787
@test isapprox(std(n.b), 1; atol=1e-6)
8888
tₒ = revert(T, n, c)
8989
@test Tables.matrix(t) Tables.matrix(tₒ)
90+
91+
# missing values
92+
a = [rand(Normal(7, 10), 99); missing]
93+
t = Table(; a)
94+
T = ZScore()
95+
n, c = apply(T, t)
96+
@test isapprox(mean(skipmissing(n.a)), 0; atol=1e-6)
97+
@test isapprox(std(skipmissing(n.a)), 1; atol=1e-6)
98+
tₒ = revert(T, n, c)
99+
@test t.a[begin:(end - 1)] tₒ.a[begin:(end - 1)]
100+
@test ismissing(tₒ.a[end])
101+
102+
# constant values
103+
a = fill(2.0, 100)
104+
t = Table(; a)
105+
T = ZScore()
106+
n, c = apply(T, t)
107+
@test all(==(0), n.a)
108+
tₒ = revert(T, n, c)
109+
@test all(==(2), tₒ.a)
90110
end

0 commit comments

Comments
 (0)