-
Notifications
You must be signed in to change notification settings - Fork 2
/
locwlv.jl
62 lines (60 loc) · 2.12 KB
/
locwlv.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
"""
locwlv(Xtrain, Ytrain, X; listnn, listw = nothing, fun, nlv, verbose = true,
kwargs...)
Compute predictions for a given kNN model.
* `Xtrain` : Training X-data.
* `Ytrain` : Training Y-data.
* `X` : X-data (m observations) to predict.
Keyword arguments:
* `listnn` : List (vector) of m vectors of indexes.
* `listw` : List (vector) of m vectors of weights.
* `fun` : Function computing the model on
the m neighborhoods.
* `nlv` : Nb. or collection of nb. of latent variables (LVs).
* `verbose` : Boolean. If `true`, fitting information
are printed.
* `kwargs` : Keywords arguments to pass in function `fun`.
Each argument must have length = 1 (not be a collection).
Same as [`locw`](@ref) but specific and much faster
for LV-based models (e.g. PLSR).
"""
function locwlv(Xtrain, Ytrain, X; listnn, listw = nothing, fun, nlv, verbose = true,
kwargs...)
p = nco(Xtrain)
m = nro(X)
q = nco(Ytrain)
nlv = max(0, minimum(nlv)):min(p, maximum(nlv))
le_nlv = length(nlv)
zpred = similar(Ytrain, m, q, le_nlv)
Threads.@threads for i = 1:m
#@inbounds for i = 1:m
verbose ? print(i, " ") : nothing
s = listnn[i]
length(s) == 1 ? s = (s:s) : nothing
zYtrain = Ytrain[s, :]
## For discrimination,
## case where all the neighbors have the same class
if q == 1 && length(unique(zYtrain)) == 1
@inbounds for a = 1:le_nlv
zpred[i, :, a] .= zYtrain[1]
end
## End
else
if isnothing(listw)
fm = fun(Xtrain[s, :], zYtrain; nlv = maximum(nlv), kwargs...)
else
fm = fun(Xtrain[s, :], zYtrain, mweight(listw[i]); nlv = maximum(nlv), kwargs...)
end
@inbounds for a = 1:le_nlv
zpred[i, :, a] = predict(fm, vrow(X, i:i); nlv = nlv[a]).pred
end
end
end
verbose ? println() : nothing
pred = list(Union{Matrix{Int}, Matrix, Matrix}, le_nlv)
@inbounds for a = 1:le_nlv
pred[a] = zpred[:, :, a]
end
le_nlv == 1 ? pred = pred[1] : nothing
(pred = pred, )
end