Skip to content
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

Fix Resampler update bug #954

Merged
merged 1 commit into from
Jan 23, 2024
Merged

Fix Resampler update bug #954

merged 1 commit into from
Jan 23, 2024

Conversation

ablaom
Copy link
Member

@ablaom ablaom commented Jan 23, 2024

This PR fixes a problem when the model wrapped by a Resampler instance is mutated to one of different prediction type. I ran into this while working on JuliaAI/MLJTuning.jl#201.

using MLJBase, MLJModels
KNNClassifier = @load KNNClassifier

knn = KNNClassifier()
cnst =DeterministicConstantClassifier()

X, y = make_blobs()

resampler = MLJBase.Resampler(model=knn, measure=accuracy, operation=nothing)
mach = machine(resampler, X, y) |> fit!

resampler.model = cnst
fit!(mach)
# [ Info: Updating machine(Resampler(model = DeterministicConstantClassifier(), …), …).
# ERROR: MethodError: no method matching iterate(::CategoricalArrays.CategoricalValue{Int64, UInt32})

# Closest candidates are:
#   iterate(::LibGit2.GitBranchIter)
#    @ LibGit2 /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/LibGit2/src/reference.jl:343
#   iterate(::LibGit2.GitBranchIter, ::Any)
#    @ LibGit2 /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/LibGit2/src/reference.jl:343
#   iterate(::Parameters.Lines)
#    @ Parameters ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:78
#   ...

# Stacktrace:
#   [1] isempty(itr::CategoricalArrays.CategoricalValue{Int64, UInt32})
#     @ Base ./essentials.jl:952
#   [2] mode(a::CategoricalArrays.CategoricalValue{Int64, UInt32})
#     @ StatsBase ~/.julia/packages/StatsBase/WLz8A/src/scalarstats.jl:112
#   [3] _broadcast_getindex_evalf
#     @ ./broadcast.jl:709 [inlined]
#   [4] _broadcast_getindex
#     @ ./broadcast.jl:682 [inlined]
#   [5] getindex
#     @ ./broadcast.jl:636 [inlined]
#   [6] copy
#     @ ./broadcast.jl:942 [inlined]
#   [7] materialize
#     @ ./broadcast.jl:903 [inlined]
#   [8] predict_mode(m::DeterministicConstantClassifier, fitresult::CategoricalArrays.CategoricalValue{…}, Xnew::@NamedTuple{…})                                
#     @ MLJBase ~/MLJ/MLJBase/src/interface/model_api.jl:11
#   [9] predict_mode(mach::Machine{DeterministicConstantClassifier, true}; rows::UnitRange{Int64})                                                                                    
#     @ MLJBase ~/MLJ/MLJBase/src/operations.jl:86
#  [10] predict_mode
#     @ ~/MLJ/MLJBase/src/operations.jl:82 [inlined]

Copy link

codecov bot commented Jan 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (193ad67) 87.95% compared to head (9742d44) 87.99%.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #954      +/-   ##
==========================================
+ Coverage   87.95%   87.99%   +0.03%     
==========================================
  Files          28       28              
  Lines        2540     2540              
==========================================
+ Hits         2234     2235       +1     
+ Misses        306      305       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ablaom ablaom merged commit d744f3a into dev Jan 23, 2024
5 checks passed
@ablaom ablaom deleted the resampler-glitch branch January 23, 2024 05:01
@ablaom ablaom mentioned this pull request Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant