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

Use std of 0 for singleton vectors #90

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FeatureTransforms"
uuid = "8fd68953-04b8-4117-ac19-158bf6de9782"
authors = ["Invenia Technical Computing Corporation"]
version = "0.3.6"
version = "0.3.7"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
3 changes: 2 additions & 1 deletion src/scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ struct MeanStdScaling <: AbstractScaling
end
end

compute_stats(x) = (mean(x), std(x))
# Set std to 0 using corrected=false if x is a singleton
compute_stats(x) = (mean(x), std(x; corrected=(length(x) != 1)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's better to pass this via MeanStdScaling as a kwarg? since it's already exposed via Statistics.std anyway?

Copy link
Contributor Author

@bencottier bencottier May 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see where you're coming from by leaving it up to the user. I guess it would just be inconvenient in the case that came up for me. I would rather change my downstream test case so that the data doesn't end up being one row, than add corrected=(length(x) != 1) to MeanStdScaling in the transform pipeline, in the source code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think it's better to not make those kinds of assumptions in the code here, especially when Statistics.std doesn't. In the instance of edge-case we just have to handle it on that end.


function _apply(A::AbstractArray, scaling::MeanStdScaling; inverse=false, eps=1e-3, kwargs...)
inverse && return scaling.μ .+ scaling.σ .* A
Expand Down
6 changes: 6 additions & 0 deletions test/scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@
scaling = MeanStdScaling(x)
@test FeatureTransforms.apply_append(x, scaling, append_dim=1) == vcat(x, expected)
end

@testset "singleton" begin
x = [2.]
scaling = MeanStdScaling(x)
@test FeatureTransforms.apply(x, scaling) == [0.]
end
end

@testset "Matrix" begin
Expand Down