Normalization.AbstractModifier Type
AbstractModifier{N<:AbstractNormalization} <: AbstractNormalization{eltype(N)}
Abstract type for modifiers that wrap an existing normalization, altering its behavior (for example, by making it robust to outliers or NaN-safe).
Interface
Any concrete modifier type Modifier <: AbstractModifier
(such as Robust
, Mixed
, or NaNSafe
) can be applied to a concrete normalization type Normalization <: AbstractNormalization
:
N = Modifier{Normalization} # A combined type with a free `eltype` of `Any`
N = Modifier{Normalization{Float64}} # A concrete `eltype` of `Float64`
All AbstractNormalization
constructors and traits are then defined for AbstractModifier
types.
Normalization.AbstractNormalization Type
AbstractNormalization
Abstract type for normalizations.
Constructors
You can work with AbstractNormalization
s as either types or instances. The type approach is useful for concise code, whereas the instance approach is useful for performant mutations. In the examples below we use the ZScore
normalization, but the same syntax applies to all Normalization
s.
Fit to a type
X = randn(100, 10)
N = fit(ZScore, X; dims=nothing) # eltype inferred from X
N = fit(ZScore{Float32}, X; dims=nothing) # eltype set to Float32
N isa AbstractNormalization && N isa ZScore # Returns a concrete AbstractNormalization
Fit to an instance
X = randn(100, 10)
N = ZScore{Float64}(; dims=2) # Initializes with empty parameters
N isa AbstractNormalization && N isa ZScore # Returns a concrete AbstractNormalization
!isfit(N)
fit!(N, X; dims=1) # Fit normalization in-place, and update the `dims`
Normalization.dims(N) == 1
Normalization and denormalization
With a fit normalization, there are two approaches to normalizing data: in-place and out-of-place.
_X = copy(X)
normalize!(_X, N) # Normalizes in-place, updating _X
Y = normalize(X, N) # Normalizes out-of-place, returning a new array
normalize(X, ZScore; dims=1) # For convenience, fits and then normalizes
For most normalizations, there is a corresponding denormalization that transforms data to the original space.
Z = denormalize(Y, N) # Denormalizes out-of-place, returning a new array
Z ≈ X
denormalize!(Y, N) # Denormalizes in-place, updating Y
Properties and traits
Type traits
Normalization.estimators(N::Union{<:AbstractNormalization,Type{<:AbstractNormalization})
returns the estimatorsN
as a tuple of functionsforward(N::Union{<:AbstractNormalization,Type{<:AbstractNormalization})
returns the forward normalization function (e.g. x-> x - 𝜇 / 𝜎 for theZScore
)inverse(N::Union{<:AbstractNormalization,Type{<:AbstractNormalization}})
returns the inverse normalization function e.g.
forward(N)(ps...) |> InverseFunctions.inverse`eltype(N::Union{<:AbstractNormalization,Type{<:AbstractNormalization})
returns the eltype of the normalization parameters
Concrete properties
Normalization.dims(N::<:AbstractNormalization)
returns the dimensions of the normalization. The dimensions are determined bydims
and correspond to the mapped slices of the input array.params(N::<:AbstractNormalization)
returns the parameters ofN
as a tuple of arrays. The dimensions of arrays are the complement ofdims
.isfit(N::<:AbstractNormalization)
checks if all parameters are non-empty
Normalization.Mixed Type
Mixed{N<:AbstractNormalization} <: AbstractModifier{N}
A modifier type that wraps an existing normalization and replaces its estimators with a mixture of robust and classical statistics.
Mixed
uses the median instead of the mean if the interquartile range (IQR) is nonzero, otherwise it falls back to the mean. For the standard deviation, it uses the IQR (divided by 1.35) if nonzero, otherwise it falls back to the standard deviation. This makes the normalization robust to outliers while still using classical estimators when the data is degenerate.
Normalization.NaNSafe Type
NaNSafe{N<:AbstractNormalization} <: AbstractModifier{N}
A modifier type that wraps an existing normalization and replaces its estimators with NaN-safe versions.
NaNSafe
modifies the estimators of the underlying normalization so that they ignore any NaN
values in the input data.
Normalization.Robust Type
Robust{N<:AbstractNormalization} <: AbstractModifier{N}
A modifier type that wraps an existing normalization and replaces its estimators with robust statistics.
Robust
replaces the mean with the median and the standard deviation with the interquartile range (IQR, divided by 1.35 so that it matches the standard deviation for normally distributed data). This makes the normalization more robust to outliers.