Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
tlienart committed Sep 23, 2022
2 parents 5bb7c6d + 1e8afa9 commit 7e34598
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MLJLinearModels"
uuid = "6ee0df7b-362f-4a72-a706-9e79364fb692"
authors = ["Thibaut Lienart <tlienart@me.com>"]
version = "0.7.0"
version = "0.7.1"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand All @@ -25,6 +25,7 @@ julia = "1.5"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
MLJ = "add582a8-e3ab-11e8-2d5e-e98b27df1bc7"
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
RCall = "6f49c342-dc21-5d91-9882-a32aef131414"
Expand All @@ -34,4 +35,4 @@ StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DelimitedFiles", "PyCall", "ForwardDiff", "Test", "Random", "RDatasets", "RCall", "MLJBase", "StableRNGs", "DataFrames"]
test = ["DelimitedFiles", "PyCall", "ForwardDiff", "Test", "Random", "RDatasets", "RCall", "MLJ", "MLJBase", "StableRNGs", "DataFrames"]
20 changes: 15 additions & 5 deletions src/mlj/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ function MMI.fit(m::Union{CLF_MODELS...}, verb::Int, X, y)
classes = MMI.classes(y[1])
nclasses = length(classes)
if nclasses < 2
throw(DomainError("The target `y` needs to have two or more levels."))
throw(
DomainError(
"The target `y` needs to have two or more levels."
)
)
elseif nclasses == 2 && m isa LogisticClassifier
# recode to ± 1
yplain[yplain .== 1] .= -1
Expand All @@ -71,8 +75,11 @@ function MMI.fit(m::Union{CLF_MODELS...}, verb::Int, X, y)
# in the Logistic case and not in the Multinomial {0} case!
nclasses = 0
end
# NOTE: here the number of classes is either 0 or > 2
clf = glr(m, nclasses)
# NOTE: here nclasses is either
# - 0 Logistic, binary
# - 2 Multinomial, binary
# - >2 or > 2 (Multinomial)
clf = glr(m, nclasses)
solver = m.solver === nothing ? _solver(clf, size(Xmatrix)) : m.solver
verb > 0 && @info "Solver: $(solver)"
# get the parameters
Expand All @@ -84,13 +91,16 @@ end
function MMI.predict(m::Union{CLF_MODELS...}, (θ, features, classes, c), Xnew)
Xmatrix = MMI.matrix(Xnew)
preds = apply_X(Xmatrix, θ, c)
if c > 2 # multiclass

if c > 2 || m isa MultinomialClassifier # multiclass
preds .= softmax(preds)
else # binary (necessarily c==0)

else # binary logistic (necessarily c==0)
preds .= sigmoid.(preds)
preds = hcat(1.0 .- preds, preds) # scores for -1 and 1
return MMI.UnivariateFinite(classes, preds)
end

return MMI.UnivariateFinite(classes, preds)
end

Expand Down
19 changes: 19 additions & 0 deletions test/interface/extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ end
y = MLJBase.coerce(vcat(fill("a", 10), ["b", ]), MLJBase.Multiclass)[1:10]
mach = MLJBase.machine(MultinomialClassifier(), X, y) |> MLJBase.fit!
end

# https://github.com/JuliaAI/MLJLinearModels.jl/issues/129
@testset "Crabs" begin
data = MLJ.load_crabs()
y_, X = MLJ.unpack(data, ==(:sp), col->col in [:FL, :RW]);
y = MLJ.coerce(y_, MLJ.OrderedFactor);
model = MultinomialClassifier()
mach = MLJ.machine(model, X, y) |> MLJ.fit!
yhat = MLJ.predict_mode(mach, X)

# crappy "test" but we're just testing that predict works fine
@test MLJ.misclassification_rate(yhat, y) < 0.4

model = LogisticClassifier()
mach = MLJ.machine(model, X, y) |> MLJ.fit!
yhat = MLJ.predict_mode(mach, X)

@test MLJ.misclassification_rate(yhat, y) < 0.4
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using MLJLinearModels, Test, LinearAlgebra
using Random, StableRNGs, DataFrames, ForwardDiff
import MLJBase # not MLJModelInterface, to mimic the full interface
import MLJ, MLJBase

DO_COMPARISONS = false; include("testutils.jl")

Expand Down

2 comments on commit 7e34598

@tlienart
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/68843

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.1 -m "<description of version>" 7e34598fa69099f480409e474857abd289e95427
git push origin v0.7.1

Please sign in to comment.