Skip to content

Add support for training classifiers with one class present #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ jobs:
- os: macos-latest
version: '1'
arch: x64
env:
PYTHON: ''
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: julia-actions/setup-julia@v1
Expand All @@ -54,6 +52,6 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
- uses: codecov/codecov-action@v3
with:
file: lcov.info
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CatBoost"
uuid = "e2e10f9a-a85d-4fa9-b6b2-639a32100a12"
authors = ["Beacon Biosignals, Inc."]
version = "0.3.2"
version = "0.3.3"

[deps]
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
Expand All @@ -10,11 +10,15 @@ PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
Aqua = "0.5"
Aqua = "0.8"
DataFrames = "1.6"
MLJBase = "1"
MLJModelInterface = "1"
MLJTestInterface = "0.2"
OrderedCollections = "1.4"
PythonCall = "0.9"
Tables = "1.4"
Test = "1.6"
julia = "1.6"

[extras]
Expand Down
109 changes: 42 additions & 67 deletions format/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,61 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[CSTParser]]
deps = ["Tokenize"]
git-tree-sha1 = "60e9121d9ea044c30a04397e59b00c5d9eb826ee"
git-tree-sha1 = "3ddd48d200eb8ddf9cb3e0189fc059fd49b97c1f"
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
version = "2.5.0"
version = "3.3.6"

[[CommonMark]]
deps = ["Crayons", "JSON", "URIs"]
git-tree-sha1 = "7632afc57f92720a01d9aedf23f413f4e5e21015"
deps = ["Crayons", "JSON", "PrecompileTools", "URIs"]
git-tree-sha1 = "532c4185d3c9037c0237546d817858b23cf9e071"
uuid = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
version = "0.8.1"
version = "0.8.12"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "0a817fbe51c976de090aa8c997b7b719b786118d"
deps = ["Dates", "LinearAlgebra", "UUIDs"]
git-tree-sha1 = "886826d76ea9e72b35fcd000e535588f7b60f21d"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.28.0"
version = "4.10.1"

[[Crayons]]
git-tree-sha1 = "3f71217b538d7aaee0b69ab47d9b7724ca8afa0d"
git-tree-sha1 = "249fe38abf76d48563e2f4556bebd215aa317e15"
uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
version = "4.0.4"
version = "4.1.1"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "4437b64df1e0adccc3e5d1adbc3ac741095e4677"
git-tree-sha1 = "ac67408d9ddf207de5cfa9a97e114352430f01ed"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.9"
version = "0.18.16"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[DocStringExtensions]]
deps = ["LibGit2", "Markdown", "Pkg", "Test"]
git-tree-sha1 = "9d4f64f79012636741cf01133158a54b24924c32"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.8.4"

[[Documenter]]
deps = ["Base64", "Dates", "DocStringExtensions", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"]
git-tree-sha1 = "3ebb967819b284dc1e3c0422229b58a40a255649"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "0.26.3"

[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[IOCapture]]
deps = ["Logging"]
git-tree-sha1 = "377252859f740c217b936cebcd918a44f9b53b59"
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
version = "0.1.1"
[[Glob]]
git-tree-sha1 = "97285bbd5230dd766e9ef6749b80fc617126d496"
uuid = "c27321d9-0574-5035-807b-f59d2c89b15c"
version = "1.3.1"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "81690084b6198a2e1da36fcfda16eeca9f9f24e4"
git-tree-sha1 = "31e996f0a15c7b280ba9f76636b3ff9e2ae58c9a"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.1"
version = "0.21.4"

[[JuliaFormatter]]
deps = ["CSTParser", "CommonMark", "DataStructures", "Documenter", "Pkg", "Tokenize"]
git-tree-sha1 = "b947b46a3477e4c1ea32a7db66905d6f63dd7076"
deps = ["CSTParser", "CommonMark", "DataStructures", "Glob", "Pkg", "PrecompileTools", "Tokenize"]
git-tree-sha1 = "8f5295e46f594ad2d8652f1098488a77460080cd"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
version = "0.13.10"
version = "1.0.45"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
Expand Down Expand Up @@ -132,20 +111,32 @@ uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[OrderedCollections]]
git-tree-sha1 = "4fa2ba51070ec13fcc7517db714445b4ab986bdf"
git-tree-sha1 = "dfdf5519f235516220579f949664f1bf44e741c5"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.0"
version = "1.6.3"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "c8abc88faa3f7a3950832ac5d6e690881590d6dc"
deps = ["Dates", "PrecompileTools", "UUIDs"]
git-tree-sha1 = "8489905bcdbcfac64d1daa51ca07c0d8f0283821"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "1.1.0"
version = "2.8.1"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[PrecompileTools]]
deps = ["Preferences"]
git-tree-sha1 = "03b4c25b43cb84cee5c90aa9b5ea0a78fd848d2f"
uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
version = "1.2.0"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "00805cd429dcb4870060ff49ef443486c262e38e"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.4.1"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand All @@ -164,21 +155,9 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Expand All @@ -187,19 +166,15 @@ uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[Tokenize]]
git-tree-sha1 = "45b1932b0ec576159181bf75df71d6d86aa9c850"
git-tree-sha1 = "3ac1ac11b09e8033ec93a7993acdb9b68252be6d"
uuid = "0796e94c-ce3b-5d07-9a54-7f471281c624"
version = "0.5.13"
version = "0.5.27"

[[URIs]]
git-tree-sha1 = "97bbe755a53fe859669cd907f2d96aee8d2c1355"
git-tree-sha1 = "67db6cc7b3821e19ebe75791a9dd19c9b1188f2b"
uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
version = "1.3.0"
version = "1.5.1"

[[UUIDs]]
deps = ["Random", "SHA"]
Expand Down
26 changes: 24 additions & 2 deletions src/mlj_catboostclassifier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,22 @@ function model_init(mlj_model::CatBoostClassifier; kw...)
end

function MMI.fit(mlj_model::CatBoostClassifier, verbosity::Int, data_pool, y_first)
verbose = verbosity <= 1 ? false : true
# Check if y_first has only one unique value
unique_classes = pyconvert(Vector, numpy.unique(data_pool.get_label()))
if length(unique_classes) == 1
# Skip training and store the single class
fitresult = (model=nothing, single_class=unique_classes[1], y_first=y_first)
cache = (; mlj_model=deepcopy(mlj_model))
report = (feature_importances=[],) # No feature importances in this case
return (fitresult, cache, report)
end

verbose = verbosity <= 1 ? false : true
model = model_init(mlj_model; verbose)
model.fit(data_pool)

cache = (; mlj_model=deepcopy(mlj_model))
report = (feature_importances=feature_importance(model),)

fitresult = (model, y_first)

return (fitresult, cache, report)
Expand All @@ -90,6 +98,14 @@ MMI.fitted_params(::CatBoostClassifier, model) = (model=model,)
MMI.reports_feature_importances(::Type{<:CatBoostClassifier}) = true

function MMI.predict(mlj_model::CatBoostClassifier, fitresult, X_pool)
if fitresult[1] === nothing
# Always predict the single class
n = nrow(X_pool)
classes = [fitresult.single_class]
probs = ones(n, 1)
return MMI.UnivariateFinite(classes, probs; pool=fitresult.y_first)
end

model, y_first = fitresult
classes = pyconvert(Array, model.classes_.tolist())
py_preds = predict_proba(model, X_pool)
Expand All @@ -98,6 +114,12 @@ function MMI.predict(mlj_model::CatBoostClassifier, fitresult, X_pool)
end

function MMI.predict_mode(mlj_model::CatBoostClassifier, fitresult, X_pool)
if fitresult[1] === nothing
# Return probability 1 for the single class
n = nrow(X_pool)
return hcat(ones(n), zeros(n))
end

model, y_first = fitresult
py_preds = predict(model, X_pool)
preds = pyconvert(Array, py_preds)
Expand Down
5 changes: 3 additions & 2 deletions src/wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ function feature_importance(py_model::Py)
py_df_importance["importance"] = py_model.feature_importances_
tbl_importance = pandas_to_tbl(py_df_importance)
n_features = size(tbl_importance.name, 1)
feat_importance = [Symbol(tbl_importance.name[i]) => tbl_importance.importance[i] for i in
1:n_features]
feat_importance = [Symbol(tbl_importance.name[i]) => tbl_importance.importance[i]
for i in
1:n_features]
return feat_importance
end

Expand Down
9 changes: 4 additions & 5 deletions test/mlj_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@
@test isempty(failures)
end
@testset "CatBoostClassifier" begin
for data in [MLJTestInterface.make_binary(), MLJTestInterface.make_multiclass()]
for data in [MLJTestInterface.make_binary(),
MLJTestInterface.make_multiclass(),
MLJTestInterface.make_binary(; row_table=true),
MLJTestInterface.make_multiclass(; row_table=false)]
X = data[1]
y = data[2]
# catboost fails if only 1 class is present when training
# MLJTestInterface splits the data down the middle, so the binary
# data only has one class during training
y[1] = y[end]
failures, summary = MLJTestInterface.test([CatBoostClassifier], X, y;
mod=@__MODULE__, verbosity=0, # bump to debug
throw=false)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const MLJ_EXAMPLES_DIR = joinpath(@__DIR__, "..", "examples/mlj")
include("wrapper.jl")
include("mlj_interface.jl")

Aqua.test_all(CatBoost; ambiguities=false)
Aqua.test_all(CatBoost)