Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/transforms/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/transforms/zscore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ assertions(transform::ZScore) = [scitypeassert(Continuous, transform.selector)]
isrevertible(::Type{<:ZScore}) = true

function colcache(::ZScore, x)
μ = mean(x)
σ = std(x, mean=μ)
μ = mean(skipmissing(x))
σ = std(skipmissing(x), mean=μ)
(μ=μ, σ=σ)
end

Expand Down
Binary file modified test/data/eigenanalysis-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/transforms/zscore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,24 @@
@test isapprox(std(n.b), 1; atol=1e-6)
tₒ = revert(T, n, c)
@test Tables.matrix(t) ≈ Tables.matrix(tₒ)

# missing values
a = [rand(Normal(7, 10), 99); missing]
t = Table(; a)
T = ZScore()
n, c = apply(T, t)
@test isapprox(mean(skipmissing(n.a)), 0; atol=1e-6)
@test isapprox(std(skipmissing(n.a)), 1; atol=1e-6)
tₒ = revert(T, n, c)
@test t.a[begin:(end - 1)] ≈ tₒ.a[begin:(end - 1)]
@test ismissing(tₒ.a[end])

# constant values
a = fill(2.0, 100)
t = Table(; a)
T = ZScore()
n, c = apply(T, t)
@test all(==(0), n.a)
tₒ = revert(T, n, c)
@test all(==(2), tₒ.a)
end
Loading