/
transformations.jl
368 lines (309 loc) · 10.6 KB
/
transformations.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
### Transformations
abstract type AbstractDataTransform end
# apply the transform
"""
transform!(t::AbstractDataTransform, x)
Apply transformation `t` to vector or matrix `x` in place.
"""
transform!(t::AbstractDataTransform, x::AbstractMatrix{<:Real}) =
transform!(x, t, x)
transform!(t::AbstractDataTransform, x::AbstractVector{<:Real}) =
(transform!(t, reshape(x, :, 1)); x)
"""
transform(t::AbstractDataTransform, x)
Return a standardized copy of vector or matrix `x` using transformation `t`.
"""
transform(t::AbstractDataTransform, x::AbstractMatrix{<:Real}) =
transform!(similar(x), t, x)
transform(t::AbstractDataTransform, x::AbstractVector{<:Real}) =
vec(transform(t, reshape(x, :, 1)))
# reconstruct the original data from transformed values
"""
reconstruct!(t::AbstractDataTransform, y)
Perform an in-place reconstruction into an original data scale from a transformed
vector or matrix `y` using transformation `t`.
"""
reconstruct!(t::AbstractDataTransform, y::AbstractMatrix{<:Real}) =
reconstruct!(y, t, y)
reconstruct!(t::AbstractDataTransform, y::AbstractVector{<:Real}) =
(reconstruct!(t, reshape(y, :, 1)); y)
"""
reconstruct(t::AbstractDataTransform, y)
Return a reconstruction of an originally scaled data from a transformed vector
or matrix `y` using transformation `t`.
"""
reconstruct(t::AbstractDataTransform, y::AbstractMatrix{<:Real}) =
reconstruct!(similar(y), t, y)
reconstruct(t::AbstractDataTransform, y::AbstractVector{<:Real}) =
vec(reconstruct(t, reshape(y, :, 1)))
"""
Standardization (Z-score transformation)
"""
struct ZScoreTransform{T<:Real, U<:AbstractVector{T}} <: AbstractDataTransform
len::Int
dims::Int
mean::U
scale::U
function ZScoreTransform(l::Int, dims::Int, m::U, s::U) where {T<:Real, U<:AbstractVector{T}}
lenm = length(m)
lens = length(s)
lenm == l || lenm == 0 || throw(DimensionMismatch("Inconsistent dimensions."))
lens == l || lens == 0 || throw(DimensionMismatch("Inconsistent dimensions."))
new{T, U}(l, dims, m, s)
end
end
function Base.getproperty(t::ZScoreTransform, p::Symbol)
if p === :indim || p === :outdim
return t.len
else
return getfield(t, p)
end
end
"""
fit(ZScoreTransform, X; dims=nothing, center=true, scale=true)
Fit standardization parameters to vector or matrix `X`
and return a `ZScoreTransform` transformation object.
# Keyword arguments
* `dims`: if `1` fit standardization parameters in column-wise fashion;
if `2` fit in row-wise fashion. The default is `nothing`, which is equivalent to `dims=2` with a deprecation warning.
* `center`: if `true` (the default) center data so that its mean is zero.
* `scale`: if `true` (the default) scale the data so that its variance is equal to one.
# Examples
```jldoctest
julia> using StatsBase
julia> X = [0.0 -0.5 0.5; 0.0 1.0 2.0]
2×3 Matrix{Float64}:
0.0 -0.5 0.5
0.0 1.0 2.0
julia> dt = fit(ZScoreTransform, X, dims=2)
ZScoreTransform{Float64, Vector{Float64}}(2, 2, [0.0, 1.0], [0.5, 1.0])
julia> StatsBase.transform(dt, X)
2×3 Matrix{Float64}:
0.0 -1.0 1.0
-1.0 0.0 1.0
```
"""
function fit(::Type{ZScoreTransform}, X::AbstractMatrix{<:Real};
dims::Union{Integer,Nothing}=nothing, center::Bool=true, scale::Bool=true)
if dims === nothing
Base.depwarn("fit(t, x) is deprecated: use fit(t, x, dims=2) instead", :fit)
dims = 2
end
if dims == 1
n, l = size(X)
n >= 2 || error("X must contain at least two rows.")
m, s = mean_and_std(X, 1)
elseif dims == 2
l, n = size(X)
n >= 2 || error("X must contain at least two columns.")
m, s = mean_and_std(X, 2)
else
throw(DomainError(dims, "fit only accept dims to be 1 or 2."))
end
return ZScoreTransform(l, dims, (center ? vec(m) : similar(m, 0)),
(scale ? vec(s) : similar(s, 0)))
end
function fit(::Type{ZScoreTransform}, X::AbstractVector{<:Real};
dims::Integer=1, center::Bool=true, scale::Bool=true)
if dims != 1
throw(DomainError(dims, "fit only accepts dims=1 over a vector. Try fit(t, x, dims=1)."))
end
return fit(ZScoreTransform, reshape(X, :, 1); dims=dims, center=center, scale=scale)
end
function transform!(y::AbstractMatrix{<:Real}, t::ZScoreTransform, x::AbstractMatrix{<:Real})
if t.dims == 1
l = t.len
size(x,2) == size(y,2) == l || throw(DimensionMismatch("Inconsistent dimensions."))
n = size(y,1)
size(x,1) == n || throw(DimensionMismatch("Inconsistent dimensions."))
m = t.mean
s = t.scale
if isempty(m)
if isempty(s)
if x !== y
copyto!(y, x)
end
else
broadcast!(/, y, x, s')
end
else
if isempty(s)
broadcast!(-, y, x, m')
else
broadcast!((x,m,s)->(x-m)/s, y, x, m', s')
end
end
elseif t.dims == 2
t_ = ZScoreTransform(t.len, 1, t.mean, t.scale)
transform!(y', t_, x')
end
return y
end
function reconstruct!(x::AbstractMatrix{<:Real}, t::ZScoreTransform, y::AbstractMatrix{<:Real})
if t.dims == 1
l = t.len
size(x,2) == size(y,2) == l || throw(DimensionMismatch("Inconsistent dimensions."))
n = size(y,1)
size(x,1) == n || throw(DimensionMismatch("Inconsistent dimensions."))
m = t.mean
s = t.scale
if isempty(m)
if isempty(s)
if y !== x
copyto!(x, y)
end
else
broadcast!(*, x, y, s')
end
else
if isempty(s)
broadcast!(+, x, y, m')
else
broadcast!((y,m,s)->y*s+m, x, y, m', s')
end
end
elseif t.dims == 2
t_ = ZScoreTransform(t.len, 1, t.mean, t.scale)
reconstruct!(x', t_, y')
end
return x
end
"""
Unit range normalization
"""
struct UnitRangeTransform{T<:Real, U<:AbstractVector} <: AbstractDataTransform
len::Int
dims::Int
unit::Bool
min::U
scale::U
function UnitRangeTransform(l::Int, dims::Int, unit::Bool, min::U, max::U) where {T, U<:AbstractVector{T}}
lenmin = length(min)
lenmax = length(max)
lenmin == l || lenmin == 0 || throw(DimensionMismatch("Inconsistent dimensions."))
lenmax == l || lenmax == 0 || throw(DimensionMismatch("Inconsistent dimensions."))
new{T, U}(l, dims, unit, min, max)
end
end
function Base.getproperty(t::UnitRangeTransform, p::Symbol)
if p === :indim || p === :outdim
return t.len
else
return getfield(t, p)
end
end
# fit a unit transform
"""
fit(UnitRangeTransform, X; dims=nothing, unit=true)
Fit a scaling parameters to vector or matrix `X`
and return a `UnitRangeTransform` transformation object.
# Keyword arguments
* `dims`: if `1` fit standardization parameters in column-wise fashion;
if `2` fit in row-wise fashion. The default is `nothing`.
* `unit`: if `true` (the default) shift the minimum data to zero.
# Examples
```jldoctest
julia> using StatsBase
julia> X = [0.0 -0.5 0.5; 0.0 1.0 2.0]
2×3 Matrix{Float64}:
0.0 -0.5 0.5
0.0 1.0 2.0
julia> dt = fit(UnitRangeTransform, X, dims=2)
UnitRangeTransform{Float64, Vector{Float64}}(2, 2, true, [-0.5, 0.0], [1.0, 0.5])
julia> StatsBase.transform(dt, X)
2×3 Matrix{Float64}:
0.5 0.0 1.0
0.0 0.5 1.0
```
"""
function fit(::Type{UnitRangeTransform}, X::AbstractMatrix{<:Real};
dims::Union{Integer,Nothing}=nothing, unit::Bool=true)
if dims === nothing
Base.depwarn("fit(t, x) is deprecated: use fit(t, x, dims=2) instead", :fit)
dims = 2
end
dims ∈ (1, 2) || throw(DomainError(dims, "fit only accept dims to be 1 or 2."))
tmin, tmax = _compute_extrema(X, dims)
@. tmax = 1 / (tmax - tmin)
l = length(tmin)
return UnitRangeTransform(l, dims, unit, tmin, tmax)
end
function _compute_extrema(X::AbstractMatrix, dims::Integer)
dims == 2 && return _compute_extrema(X', 1)
l = size(X, 2)
tmin = similar(X, l)
tmax = similar(X, l)
for i in 1:l
@inbounds tmin[i], tmax[i] = extrema(@view(X[:, i]))
end
return tmin, tmax
end
function fit(::Type{UnitRangeTransform}, X::AbstractVector{<:Real};
dims::Integer=1, unit::Bool=true)
if dims != 1
throw(DomainError(dims, "fit only accept dims=1 over a vector. Try fit(t, x, dims=1)."))
end
tmin, tmax = extrema(X)
tmax = 1 / (tmax - tmin)
return UnitRangeTransform(1, dims, unit, [tmin], [tmax])
end
function transform!(y::AbstractMatrix{<:Real}, t::UnitRangeTransform, x::AbstractMatrix{<:Real})
if t.dims == 1
l = t.len
size(x,2) == size(y,2) == l || throw(DimensionMismatch("Inconsistent dimensions."))
n = size(x,1)
size(y,1) == n || throw(DimensionMismatch("Inconsistent dimensions."))
tmin = t.min
tscale = t.scale
if t.unit
broadcast!((x,s,m)->(x-m)*s, y, x, tscale', tmin')
else
broadcast!(*, y, x, tscale')
end
elseif t.dims == 2
t_ = UnitRangeTransform(t.len, 1, t.unit, t.min, t.scale)
transform!(y', t_, x')
end
return y
end
function reconstruct!(x::AbstractMatrix{<:Real}, t::UnitRangeTransform, y::AbstractMatrix{<:Real})
if t.dims == 1
l = t.len
size(x,2) == size(y,2) == l || throw(DimensionMismatch("Inconsistent dimensions."))
n = size(y,1)
size(x,1) == n || throw(DimensionMismatch("Inconsistent dimensions."))
tmin = t.min
tscale = t.scale
if t.unit
broadcast!((y,s,m)->y/s+m, x, y, tscale', tmin')
else
broadcast!(/, x, y, tscale')
end
elseif t.dims == 2
t_ = UnitRangeTransform(t.len, 1, t.unit, t.min, t.scale)
reconstruct!(x', t_, y')
end
return x
end
"""
standardize(DT, X; dims=nothing, kwargs...)
Return a standardized copy of vector or matrix `X` along dimensions `dims`
using transformation `DT` which is a subtype of `AbstractDataTransform`:
- `ZScoreTransform`
- `UnitRangeTransform`
# Example
```jldoctest
julia> using StatsBase
julia> standardize(ZScoreTransform, [0.0 -0.5 0.5; 0.0 1.0 2.0], dims=2)
2×3 Matrix{Float64}:
0.0 -1.0 1.0
-1.0 0.0 1.0
julia> standardize(UnitRangeTransform, [0.0 -0.5 0.5; 0.0 1.0 2.0], dims=2)
2×3 Matrix{Float64}:
0.5 0.0 1.0
0.0 0.5 1.0
```
"""
function standardize(::Type{DT}, X::AbstractVecOrMat{<:Real}; kwargs...) where {DT <: AbstractDataTransform}
return transform(fit(DT, X; kwargs...), X)
end