Skip to content

Commit e8da6ba

Browse files
ablaomOkonSamuel
andauthored
For a 1.5 release (#154)
* add `feature_importances` stub (#148) * add intrinsic_importances stub and set fallback to othing. * fix error in intrinsic_importances docstring. * rename intrinsic_importances method to eature_importances. * remove fallback for eature_importances. * Update src/model_api.jl Co-authored-by: Anthony Blaom, PhD <[email protected]> * bump 1.5 Co-authored-by: Okon Samuel <[email protected]>
1 parent f6432bc commit e8da6ba

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MLJModelInterface"
22
uuid = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
33
authors = ["Thibaut Lienart and Anthony Blaom"]
4-
version = "1.4.4"
4+
version = "1.5"
55

66
[deps]
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

src/MLJModelInterface.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const MODEL_TRAITS = [
3030
:hyperparameter_ranges,
3131
:iteration_parameter,
3232
:supports_training_losses,
33+
:reports_feature_importances,
3334
:deep_properties
3435
]
3536

src/model_api.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ implement this method to return an `AbstractVector` of the losses
4040
in historical order. If the model calculates scores instead, then the
4141
sign of the scores should be reversed.
4242
43-
The following trait overload is alse required:
43+
The following trait overload is also required:
4444
`supports_training_losses(::Type{<:M}) = true`
4545
4646
"""
@@ -164,3 +164,20 @@ function restore end
164164
some meta-models may choose to implement the `evaluate` operations
165165
"""
166166
function evaluate end
167+
168+
"""
169+
feature_importances(model::M, fitresult, report)
170+
171+
For a given `model` of model type `M` supporting intrinsic feature importances, calculate
172+
the feature importances from the model's `fitresult` and `report` as an
173+
abstract vector of `feature::Symbol => importance::Real` pairs
174+
(e.g `[:gender =>0.23, :height =>0.7, :weight => 0.1]`).
175+
176+
The following trait overload is also required:
177+
`reports_feature_importances(::Type{<:M}) = true`
178+
179+
If for some reason a model is sometimes unable to report feature importances then
180+
`feature_importances` should return all importances as 0.0, as in
181+
`[:gender =>0.0, :height =>0.0, :weight => 0.0]`.
182+
"""
183+
function feature_importances end

test/model_api.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ end
55
f0::Int
66
end
77

8+
89
mutable struct APIx1 <: Static end
910

1011
@testset "selectrows(model, data...)" begin
@@ -30,12 +31,18 @@ end
3031
s1 = APIx1()
3132
@test fit(s1, 1, 0) == (nothing, nothing, nothing)
3233

33-
#update fallback = fit
34+
# update fallback = fit
3435
@test update(m0, 1, 5, nothing, randn(2), 5) == (5, nothing, nothing)
3536

3637
# training losses:
3738
f, c, r = MLJModelInterface.fit(m0, 1, rand(2), rand(2))
3839
@test M.training_losses(m0, r) === nothing
40+
41+
# intrinsic_importances
42+
f, c, r = MLJModelInterface.fit(m0, 1, rand(2), rand(2))
43+
MLJModelInterface.reports_feature_importances(::Type{APIx0}) = true
44+
MLJModelInterface.feature_importances(::APIx0, fitresult, report) = [:a=>0, :b=>0]
45+
@test MLJModelInterface.feature_importances(m0, f, r) == [:a=>0, :b=>0]
3946
end
4047

4148
struct DummyUnivariateFinite end

0 commit comments

Comments
 (0)