Skip to content

Commit

Permalink
support 32-bit MulticlassLDA (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
bicycle1885 authored and ararslan committed Apr 23, 2017
1 parent 3e044f5 commit 8cd7205
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/lda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function multiclass_lda_stats{T<:AbstractFloat}(nc::Int, X::DenseMatrix{T}, y::A
Sw = A_mul_Bt(Z, Z)

# compute between-class scattering
mean = cmeans * (cweights ./ n)
mean = cmeans * (cweights ./ T(n))
U = scale!(cmeans .- mean, sqrt.(cweights))
Sb = A_mul_Bt(U, U)

Expand Down Expand Up @@ -137,7 +137,7 @@ transform{T<:AbstractFloat}(M::MulticlassLDA, x::AbstractVecOrMat{T}) = M.proj'x
function fit{T<:AbstractFloat}(::Type{MulticlassLDA}, nc::Int, X::DenseMatrix{T}, y::AbstractVector{Int};
method::Symbol=:gevd,
outdim::Int=min(size(X,1), nc-1),
regcoef::T=1.0e-6)
regcoef::T=T(1.0e-6))

multiclass_lda(multiclass_lda_stats(nc, X, y);
method=method,
Expand All @@ -148,7 +148,7 @@ end
function multiclass_lda{T<:AbstractFloat}(S::MulticlassLDAStats{T};
method::Symbol=:gevd,
outdim::Int=min(size(X,1), S.nclasses-1),
regcoef::T=1.0e-6)
regcoef::T=T(1.0e-6))

P = mclda_solve(S.Sb, S.Sw, method, outdim, regcoef)
MulticlassLDA(P, P'S.cmeans, S)
Expand Down
28 changes: 15 additions & 13 deletions test/mclda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,21 @@ test_approx_eq_vecs(P1, P2)

## LDA

M = fit(MulticlassLDA, nc, X, y; method=:gevd, regcoef=lambda)
@test indim(M) == d
@test outdim(M) == nc - 1
@test projection(M) P1
@test M.pmeans M.proj'cmeans
@test transform(M, X) M.proj'X

M = fit(MulticlassLDA, nc, X, y; method=:whiten, regcoef=lambda)
@test indim(M) == d
@test outdim(M) == nc - 1
# @test projection(M) P2 # signs may change
@test M.pmeans M.proj'cmeans
@test transform(M, X) M.proj'X
for T in (Float32, Float64)
M = fit(MulticlassLDA, nc, convert(Matrix{T}, X), y; method=:gevd, regcoef=convert(T, lambda))
@test indim(M) == d
@test outdim(M) == nc - 1
@test projection(M) P1
@test M.pmeans M.proj'cmeans
@test transform(M, X) M.proj'X

M = fit(MulticlassLDA, nc, convert(Matrix{T}, X), y; method=:whiten, regcoef=convert(T, lambda))
@test indim(M) == d
@test outdim(M) == nc - 1
# @test projection(M) P2 # signs may change
@test M.pmeans M.proj'cmeans
@test transform(M, X) M.proj'X
end


## High-dimensional LDA (subspace LDA)
Expand Down

0 comments on commit 8cd7205

Please sign in to comment.