-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73322b3
commit 71abde0
Showing
6 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"Estimate the importance of a rule." | ||
function _rule_importance(weight::Number, rule::Rule) | ||
importance = 0.0 | ||
thens = rule.then::Vector{Float64} | ||
otherwises = rule.otherwise::Vector{Float64} | ||
for (then, otherwise) in zip(thens, otherwises) | ||
importance += weight * abs(then - otherwise) | ||
end | ||
return importance | ||
end | ||
|
||
""" | ||
feature_importance( | ||
model::StableRules, | ||
feature_name::AbstractString | ||
) | ||
Estimate the importance of the given `feature_name`. | ||
The aim of this function is to satisfy the following property: | ||
> Given two features X and Y, if X has more effect on the outcome, then | ||
> feature_importance(model, X) > feature_importance(model, Y). | ||
This function provides only an estimation of the importance because | ||
the effect on the outcome depends on the data. | ||
""" | ||
function feature_importance( | ||
model::StableRules, | ||
feature_name::AbstractString | ||
) | ||
importance = 0.0 | ||
for (i, rule) in enumerate(model.rules) | ||
for clause::Split in rule.path.splits | ||
if _feature_name(clause) == feature_name | ||
weight = model.weights[i] | ||
importance += _rule_importance(weight, rule) | ||
end | ||
end | ||
end | ||
return importance | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function _haberman_data() | ||
df = haberman() | ||
X = MLJBase.table(MLJBase.matrix(df[:, Not(:survival)])) | ||
y = categorical(df.survival) | ||
(X, y) | ||
end | ||
|
||
X, y = _haberman_data() | ||
|
||
classifier = StableRulesClassifier(; max_depth=2, max_rules=8, n_trees=1000, rng=_rng()) | ||
mach = machine(classifier, X, y) | ||
fit!(mach) | ||
|
||
model = mach.fitresult::StableRules | ||
|
||
importance = SIRUS.feature_importance(model, "x1") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters