Skip to content

Commit c30fbb4

Browse files
committed
2 parents 1228236 + f823c76 commit c30fbb4

File tree

4 files changed

+98
-100
lines changed

4 files changed

+98
-100
lines changed

src/models/decomposition_models.jl

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ $PCA_DESCR
99
1010
# Keyword Parameters
1111
12-
- `maxoutdim::Int=0`: maximum number of output dimensions, uses the smallest dimension of
12+
- `maxoutdim::Int=0`: maximum number of output dimensions, uses the smallest dimension of
1313
training feature matrix if 0 (default).
14-
- `method::Symbol=:auto`: method to use to solve the problem, one of `:auto`,`:cov`
14+
- `method::Symbol=:auto`: method to use to solve the problem, one of `:auto`,`:cov`
1515
or `:svd`
1616
- `pratio::Float64=0.99`: ratio of variance preserved
17-
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: if set to nothing(default)
18-
centering will be computed and applied, if set to `0` no
19-
centering(assumed pre-centered), if a vector is passed, the centering is done with
17+
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: if set to nothing(default)
18+
centering will be computed and applied, if set to `0` no
19+
centering(assumed pre-centered), if a vector is passed, the centering is done with
2020
that vector.
2121
"""
2222
@mlj_model mutable struct PCA <: MMI.Unsupervised
@@ -34,9 +34,8 @@ function MMI.fit(model::PCA, verbosity::Int, X)
3434
Xarray = MMI.matrix(X)
3535
mindim = minimum(size(Xarray))
3636
maxoutdim = model.maxoutdim == 0 ? mindim : model.maxoutdim
37-
# NOTE: copy/transpose
3837
fitresult = MS.fit(
39-
MS.PCA, transpose(Xarray);
38+
MS.PCA, Xarray';
4039
method=model.method,
4140
pratio=model.pratio,
4241
maxoutdim=maxoutdim,
@@ -74,14 +73,14 @@ $KPCA_DESCR
7473
7574
# Keyword Parameters
7675
77-
- `maxoutdim::Int = 0`: maximum number of output dimensions, uses the smallest
76+
- `maxoutdim::Int = 0`: maximum number of output dimensions, uses the smallest
7877
dimension of training feature matrix if 0 (default).
79-
- `kernel::Function=(x,y)->x'y`: kernel function of 2 vector arguments x and y, returns a
78+
- `kernel::Function=(x,y)->x'y`: kernel function of 2 vector arguments x and y, returns a
8079
scalar value
81-
- `solver::Symbol=:auto`: solver to use for the eigenvalues, one of `:eig`(default),
80+
- `solver::Symbol=:auto`: solver to use for the eigenvalues, one of `:eig`(default),
8281
`:eigs`
83-
- `inverse::Bool=false`: perform calculation for inverse transform
84-
- `beta::Real=1.0`: strength of the ridge regression that learns the inverse transform
82+
- `inverse::Bool=true`: perform calculations needed for inverse transform
83+
- `beta::Real=1.0`: strength of the ridge regression that learns the inverse transform
8584
when inverse is true
8685
- `tol::Real=0.0`: Convergence tolerance for eigs solver
8786
- `maxiter::Int=300`: maximum number of iterations for eigs solver
@@ -90,7 +89,7 @@ $KPCA_DESCR
9089
maxoutdim::Int = 0::(_ ≥ 0)
9190
kernel::Union{Nothing, Function} = default_kernel
9291
solver::Symbol = :eig::(_ in (:eig, :eigs))
93-
inverse::Bool = false
92+
inverse::Bool = true
9493
beta::Real = 1.0::(_ ≥ 0.0)
9594
tol::Real = 1e-6::(_ ≥ 0.0)
9695
maxiter::Int = 300::(_ ≥ 1)
@@ -102,7 +101,7 @@ function MMI.fit(model::KernelPCA, verbosity::Int, X)
102101
# default max out dim if not given
103102
maxoutdim = model.maxoutdim == 0 ? mindim : model.maxoutdim
104103
fitresult = MS.fit(
105-
MS.KernelPCA,
104+
MS.KernelPCA,
106105
permutedims(Xarray);
107106
kernel=model.kernel,
108107
maxoutdim=maxoutdim,
@@ -147,12 +146,12 @@ $ICA_DESCR
147146
- `do_whiten::Bool=true`: whether to perform pre-whitening
148147
- `maxiter::Int=100`: maximum number of iterations
149148
- `tol::Real=1e-6`: convergence tolerance for change in matrix W
150-
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: mean to use, if nothing (default)
151-
centering is computed andapplied, if zero, no centering, a vector of means can
149+
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: mean to use, if nothing (default)
150+
centering is computed andapplied, if zero, no centering, a vector of means can
152151
be passed
153-
- `winit::Union{Nothing,Matrix{<:Real}}=nothing`: initial guess for matrix `W` either
154-
an empty matrix (random initilization of `W`), a matrix of size `k × k` (if `do_whiten`
155-
is true), a matrix of size `m × k` otherwise. If unspecified i.e `nothing` an empty
152+
- `winit::Union{Nothing,Matrix{<:Real}}=nothing`: initial guess for matrix `W` either
153+
an empty matrix (random initilization of `W`), a matrix of size `k × k` (if `do_whiten`
154+
is true), a matrix of size `m × k` otherwise. If unspecified i.e `nothing` an empty
156155
`Matrix{<:Real}` is used.
157156
"""
158157
@mlj_model mutable struct ICA <: MMI.Unsupervised
@@ -177,7 +176,7 @@ function MMI.fit(model::ICA, verbosity::Int, X)
177176
m = min(n, p)
178177
k = ifelse(model.k m, model.k, m)
179178
fitresult = MS.fit(
180-
MS.ICA, transpose(Xarray), k;
179+
MS.ICA, Xarray', k;
181180
alg=model.alg,
182181
fun=icagfun(model.fun, eltype(Xarray)),
183182
do_whiten=model.do_whiten,
@@ -215,14 +214,14 @@ $PPCA_DESCR
215214
216215
# Keyword Parameters
217216
218-
- `maxoutdim::Int=0`: maximum number of output dimensions, uses max(no_of_features - 1, 1)
217+
- `maxoutdim::Int=0`: maximum number of output dimensions, uses max(no_of_features - 1, 1)
219218
if 0 (default).
220219
- `method::Symbol=:ml`: method to use to solve the problem, one of `:ml`, `:em`, `:bayes`.
221220
- `maxiter::Int=1000`: maximum number of iterations.
222221
- `tol::Real=1e-6`: convergence tolerance.
223-
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: if set to nothing(default)
224-
centering will be computed and applied, if set to `0` no
225-
centering(assumed pre-centered), if a vector is passed, the centering is done with
222+
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: if set to nothing(default)
223+
centering will be computed and applied, if set to `0` no
224+
centering(assumed pre-centered), if a vector is passed, the centering is done with
226225
that vector.
227226
"""
228227
@mlj_model mutable struct PPCA <: MMI.Unsupervised
@@ -237,9 +236,8 @@ function MMI.fit(model::PPCA, verbosity::Int, X)
237236
Xarray = MMI.matrix(X)
238237
def_dim = max(1, size(Xarray, 2) - 1)
239238
maxoutdim = model.maxoutdim == 0 ? def_dim : model.maxoutdim
240-
# NOTE: copy/transpose
241239
fitresult = MS.fit(
242-
MS.PPCA, transpose(Xarray);
240+
MS.PPCA, Xarray';
243241
method=model.method,
244242
tol=model.tol,
245243
maxiter=model.maxiter,
@@ -277,14 +275,14 @@ $PPCA_DESCR
277275
# Keyword Parameters
278276
279277
- `method::Symbol=:cm`: Method to use to solve the problem, one of `:ml`, `:em`, `:bayes`.
280-
- `maxoutdim::Int=0`: Maximum number of output dimensions, uses max(no_of_features - 1, 1)
278+
- `maxoutdim::Int=0`: Maximum number of output dimensions, uses max(no_of_features - 1, 1)
281279
if 0 (default).
282280
- `maxiter::Int=1000`: Maximum number of iterations.
283281
- `tol::Real=1e-6`: Convergence tolerance.
284282
- `eta::Real=tol`: Variance lower bound
285-
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: If set to nothing(default)
286-
centering will be computed and applied, if set to `0` no
287-
centering(assumed pre-centered), if a vector is passed, the centering is done with
283+
- `mean::Union{Nothing, Real, Vector{Float64}}=nothing`: If set to nothing(default)
284+
centering will be computed and applied, if set to `0` no
285+
centering(assumed pre-centered), if a vector is passed, the centering is done with
288286
that vector.
289287
"""
290288
@mlj_model mutable struct FactorAnalysis <: MMI.Unsupervised
@@ -300,9 +298,8 @@ function MMI.fit(model::FactorAnalysis, verbosity::Int, X)
300298
Xarray = MMI.matrix(X)
301299
def_dim = max(1, size(Xarray, 2) - 1)
302300
maxoutdim = model.maxoutdim == 0 ? def_dim : model.maxoutdim
303-
# NOTE: copy/transpose
304301
fitresult = MS.fit(
305-
MS.FactorAnalysis, transpose(Xarray);
302+
MS.FactorAnalysis, Xarray';
306303
method=model.method,
307304
maxiter=model.maxiter,
308305
tol=model.tol,
@@ -348,17 +345,17 @@ for (M, MFitResultType) in model_types
348345
end
349346

350347
@eval function MMI.transform(::$M, fr::$MFitResultType, X)
351-
# X is n x d, need to transpose twice
348+
# X is n x d, need to take adjoint twice
352349
Xarray = MMI.matrix(X)
353-
Xnew = transpose(MS.predict(fr, transpose(Xarray)))
350+
Xnew = MS.predict(fr, Xarray')'
354351
return MMI.table(Xnew, prototype=X)
355352
end
356353

357354
if hasmethod(MS.reconstruct, Tuple{MFitResultType{Float64}, Matrix{Float64}})
358355
@eval function MMI.inverse_transform(::$M, fr::$MFitResultType, Y)
359-
# X is n x p, need to transpose twice
356+
# X is n x p, need to take adjoint twice
360357
Yarray = MMI.matrix(Y)
361-
Ynew = transpose(MS.reconstruct(fr, transpose(Yarray)))
358+
Ynew = MS.reconstruct(fr, Yarray')'
362359
return MMI.table(Ynew, prototype=Y)
363360
end
364361
end

0 commit comments

Comments
 (0)