Skip to content

Commit

Permalink
Solve issues #56 and #57 with Scaler()
Browse files Browse the repository at this point in the history
Issue #56 and issue #57 did refer to errors with scaler with integer matrice and vectors.
Thsy should nw be solved
  • Loading branch information
sylvaticus committed Sep 4, 2023
1 parent 6a01b6b commit 4e19c54
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/Utils/Processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,12 @@ end

Base.@kwdef mutable struct StandardScalerLearnableParameters <: AbstractScalerLearnableParameter
sfμ::Vector{Float64} = Float64[] # scale factor of mean
sfσ::Vector{Float64} = Float64[] # scale vactor of st.dev.
sfσ::Vector{Float64} = Float64[] # scale vector of st.dev.
end

function _fit(m::MinMaxScaler,skip,X,cache)
actualRanges = Tuple{Float64,Float64}[]
X_scaled = cache ? deepcopy(X) : nothing
X_scaled = cache ? float.(X) : nothing
for (ic,c) in enumerate(eachcol(X))
if !(ic in skip)
imin,imax = (m.inputRange[1](skipmissing(c)), m.inputRange[2](skipmissing(c)) )
Expand All @@ -641,11 +641,13 @@ function _fit(m::MinMaxScaler,skip,X,cache)
end
return X_scaled, MinMaxScalerLearnableParameters(actualRanges)
end
function _fit(m::StandardScaler,skip,X,cache)
nR,nD = size(X)
function _fit(m::StandardScaler,skip,X::AbstractArray,cache)
nDims = ndims(X)
nR = size(X,1)
nD = (nDims == 1) ? 1 : size(X,2)
sfμ = zeros(nD)
sfσ = ones(nD)
X_scaled = cache ? deepcopy(X) : nothing
X_scaled = cache ? float.(X) : nothing
for (ic,c) in enumerate(eachcol(X))
if !(ic in skip)
μ = m.center ? mean(skipmissing(c)) : 0.0
Expand All @@ -663,7 +665,7 @@ end

function _predict(m::MinMaxScaler,pars::MinMaxScalerLearnableParameters,skip,X;inverse=false)
if !inverse
xnew = deepcopy(X)
xnew = float.(X)
for (ic,c) in enumerate(eachcol(X))
if !(ic in skip)
imin,imax = pars.inputRangeApplied[ic]
Expand All @@ -686,7 +688,7 @@ function _predict(m::MinMaxScaler,pars::MinMaxScalerLearnableParameters,skip,X;i
end
function _predict(m::StandardScaler,pars::StandardScalerLearnableParameters,skip,X;inverse=false)
if !inverse
xnew = deepcopy(X)
xnew = float.(X)
for (ic,c) in enumerate(eachcol(X))
if !(ic in skip)
xnew[:,ic] = (c .+ pars.sfμ[ic]) .* pars.sfσ[ic]
Expand Down Expand Up @@ -805,7 +807,7 @@ mutable struct Scaler <: BetaMLUnsupervisedModel
hpar::ScalerHyperParametersSet
opt::BetaMLDefaultOptionsSet
par::Union{Nothing,ScalerLearnableParameters}
cres::Union{Nothing,Matrix}
cres::Union{Nothing,Array}
fitted::Bool
info::Dict{String,Any}
end
Expand Down
30 changes: 30 additions & 0 deletions test/Utils_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ fit!(m4,x)
ŷ4 =predict(m4,x)
@test all(isequal.(ŷ4,x))

# Check it works with integer matrices..
X = [1 10 100; 2 20 200; 3 30 300]
m = Scaler()
Xs1 = fit!(m,X)
Xs2 = predict(m,X)
@test Xs1 == Xs2 [-1.224744871391589 -1.224744871391589 -1.224744871391589; 0.0 0.0 0.0; 1.224744871391589 1.224744871391589 1.224744871391589]
@test inverse_predict(m,Xs1) == float.(X)
m2 = Scaler(MinMaxScaler())
Xs1b = fit!(m2,X)
Xs2b = predict(m2,X)
@test Xs1b == Xs2b [0.0 0.0 0.0; 0.5 0.5 0.5; 1.0 1.0 1.0]
@test inverse_predict(m2,Xs1b) == float.(X)

# Check it works with arrays and can get back to matrix or vector
X = [1,2,3]
m = Scaler()
Xs1c = fit!(m,X)
Xs2c = predict(m,X)
@test Xs1c == Xs2c == Xs1[:,1]
inverse_predict(m,Xs1c) == X
m2 = Scaler(MinMaxScaler())
Xs1d = fit!(m2,X)
Xs2e = predict(m2,X)
@test Xs1d == Xs2e == Xs1b[:,1]
@test inverse_predict(m2,Xs1d) == float.(X)

X2 = makematrix([1,2,3])
m = Scaler()
X2s1c = fit!(m,X2)
inverse_predict(m,X2s1c) == X2

# ==================================
# New test
Expand Down

0 comments on commit 4e19c54

Please sign in to comment.