-
Notifications
You must be signed in to change notification settings - Fork 2
/
scale.jl
179 lines (166 loc) · 3.52 KB
/
scale.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
"""
center(X)
center(X, weights::Weight)
Column-wise centering of X-data.
* `X` : X-data (n, p).
## Examples
```julia
using JchemoData, JLD2, CairoMakie
path_jdat = dirname(dirname(pathof(JchemoData)))
db = joinpath(path_jdat, "data/cassav.jld2")
@load db dat
pnames(dat)
X = dat.X
year = dat.Y.year
s = year .<= 2012
Xtrain = X[s, :]
Xtest = rmrow(X, s)
wlst = names(dat.X)
wl = parse.(Float64, wlst)
plotsp(dat.X, wl; nsamp = 20).f
mod = model(center)
fit!(mod, Xtrain)
Xptrain = transf(mod, Xtrain)
Xptest = transf(mod, Xtest)
colmean(Xptrain)
@head Xptest
@head Xtest .- colmean(Xtrain)'
plotsp(Xptrain).f
plotsp(Xptest).f
```
"""
function center(X)
xmeans = colmean(X)
Center(xmeans)
end
function center(X, weights::Weight)
xmeans = colmean(X, weights)
Center(xmeans)
end
"""
transf(object::Center, X)
transf!(object::Center, X::Matrix)
Compute the preprocessed data from a model.
* `object` : Model.
* `X` : X-data to transform.
"""
function transf(object::Center, X)
X = copy(ensure_mat(X))
transf!(object, X)
X
end
function transf!(object::Center, X::Matrix)
fcenter!(X, object.xmeans)
end
"""
scale(X)
scale(X, weights::Weight)
Column-wise scaling of X-data.
* `X` : X-data (n, p).
## Examples
```julia
using JchemoData, JLD2, CairoMakie
path_jdat = dirname(dirname(pathof(JchemoData)))
db = joinpath(path_jdat, "data/cassav.jld2")
@load db dat
pnames(dat)
X = dat.X
year = dat.Y.year
s = year .<= 2012
Xtrain = X[s, :]
Xtest = rmrow(X, s)
wlst = names(dat.X)
wl = parse.(Float64, wlst)
plotsp(dat.X, wl; nsamp = 20).f
mod = model(scale)
fit!(mod, Xtrain)
Xptrain = transf(mod, Xtrain)
Xptest = transf(mod, Xtest)
colstd(Xptrain)
@head Xptest
@head Xtest ./ colstd(Xtrain)'
plotsp(Xptrain).f
plotsp(Xptest).f
```
"""
function scale(X)
xscales = colstd(X)
Scale(xscales)
end
function scale(X, weights::Weight)
xscales = colstd(X, weights)
Scale(xscales)
end
"""
transf(object::Scale, X)
transf!(object::Scale, X::Matrix)
Compute the preprocessed data from a model.
* `object` : Model.
* `X` : X-data to transform.
"""
function transf(object::Scale, X)
X = copy(ensure_mat(X))
transf!(object, X)
X
end
function transf!(object::Scale, X::Matrix)
fscale!(X, object.xscales)
end
"""
cscale()
cscale(X)
cscale(X, weights::Weight)
Column-wise centering and scaling of X-data.
* `X` : X-data (n, p).
## Examples
```julia
using JchemoData, JLD2, CairoMakie
path_jdat = dirname(dirname(pathof(JchemoData)))
db = joinpath(path_jdat, "data/cassav.jld2")
@load db dat
pnames(dat)
X = dat.X
year = dat.Y.year
s = year .<= 2012
Xtrain = X[s, :]
Xtest = rmrow(X, s)
wlst = names(dat.X)
wl = parse.(Float64, wlst)
plotsp(dat.X, wl; nsamp = 20).f
mod = model(cscale)
fit!(mod, Xtrain)
Xptrain = transf(mod, Xtrain)
Xptest = transf(mod, Xtest)
colmean(Xptrain)
colstd(Xptrain)
@head Xptest
@head (Xtest .- colmean(Xtrain)') ./ colstd(Xtrain)'
plotsp(Xptrain).f
plotsp(Xptest).f
```
"""
function cscale(X)
xmeans = colmean(X)
xscales = colstd(X)
Cscale(xmeans, xscales)
end
function cscale(X, weights::Weight)
xmeans = colmean(X, weights)
xscales = colstd(X, weights)
Cscale(xmeans, xscales)
end
"""
transf(object::Cscale, X)
transf!(object::Cscale, X::Matrix)
Compute the preprocessed data from a model.
* `object` : Model.
* `X` : X-data to transform.
"""
function transf(object::Cscale, X)
X = copy(ensure_mat(X))
transf!(object, X)
X
end
function transf!(object::Cscale, X::Matrix)
fcscale!(X, object.xmeans, object.xscales)
end