Skip to content

Commit d6f223e

Browse files
committed
Add LOCI
1 parent 08565c0 commit d6f223e

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

docs/src/api.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ Classes
4040
CFI
4141
PFI
4242
D0CRT
43+
44+
Marginal Importance
45+
===================
46+
47+
.. autosummary::
48+
:toctree: ./generated/api/marginal
49+
:template: class.rst
50+
51+
LOCI
52+
LeaveOneCovariateIn

docs/tools/references.bib

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ @article{eshel2003yule
144144
year = {2003}
145145
}
146146

147+
@inproceedings{ewald2024guide,
148+
title = {A guide to feature importance methods for scientific inference},
149+
author = {Ewald, Fiona Katharina and Bothmann, Ludwig and Wright, Marvin N and Bischl, Bernd and Casalicchio, Giuseppe and K{\"o}nig, Gunnar},
150+
booktitle = {World Conference on Explainable Artificial Intelligence},
151+
pages = {440--464},
152+
year = {2024},
153+
organization = {Springer}
154+
}
155+
147156
@article{fan2012variance,
148157
author = {Fan, Jianqing and Guo, Shaojun and Hao, Ning},
149158
journal = {Journal of the Royal Statistical Society Series B: Statistical Methodology},

examples/plot_conditional_vs_marginal_xor_data.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import seaborn as sns
1313
from sklearn.base import clone
1414
from sklearn.linear_model import RidgeCV
15-
from sklearn.metrics import hinge_loss
15+
from sklearn.metrics import hinge_loss, accuracy_score
1616
from sklearn.model_selection import KFold, train_test_split
1717
from sklearn.svm import SVC
1818

19-
from hidimstat import CFI
19+
from hidimstat import CFI, LOCI
2020

2121
#############################################################################
2222
# To solve the XOR problem, we will use a Support Vector Classier (SVC) with Radial Basis Function (RBF) kernel. The decision function of
@@ -82,21 +82,9 @@
8282
cv = KFold(n_splits=5, shuffle=True, random_state=0)
8383
clf = SVC(kernel="rbf", random_state=0)
8484
# Compute marginal importance using univariate models
85-
marginal_scores = []
86-
for i in range(X.shape[1]):
87-
feat_scores = []
88-
for train_index, test_index in cv.split(X):
89-
X_train, X_test = X[train_index], X[test_index]
90-
y_train, y_test = Y[train_index], Y[test_index]
91-
92-
X_train_univariate = X_train[:, i].reshape(-1, 1)
93-
X_test_univariate = X_test[:, i].reshape(-1, 1)
94-
95-
univariate_model = clone(clf)
96-
univariate_model.fit(X_train_univariate, y_train)
97-
98-
feat_scores.append(univariate_model.score(X_test_univariate, y_test))
99-
marginal_scores.append(feat_scores)
85+
loci = LOCI(estimator=clone(clf).fit(X, Y), method="decision_function", loss=hinge_loss)
86+
mean_importances = loci.fit_importance(X, Y, cv=cv)
87+
marginal_importances = np.array(loci.importances_)
10088

10189
###########################################################################
10290

@@ -129,7 +117,7 @@
129117
fig, axes = plt.subplots(1, 2, sharey=True, figsize=(6, 2.5))
130118
# Marginal scores boxplot
131119
sns.boxplot(
132-
data=np.array(marginal_scores).T,
120+
data=marginal_importances,
133121
orient="h",
134122
ax=axes[0],
135123
fill=False,

src/hidimstat/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
from .noise_std import reid
2929
from .permutation_feature_importance import PFI
3030

31+
# marginal methods
32+
from .marginal import LeaveOneCovariateIn # for having documentation
33+
from .marginal import LeaveOneCovariateIn as LOCI
34+
3135
from .statistical_tools.aggregation import quantile_aggregation
3236

3337
try:
@@ -54,4 +58,6 @@
5458
"CFI",
5559
"LOCO",
5660
"PFI",
61+
# marginal methods
62+
"LOCI",
5763
]

0 commit comments

Comments
 (0)