|
12 | 12 | import seaborn as sns |
13 | 13 | from sklearn.base import clone |
14 | 14 | from sklearn.linear_model import RidgeCV |
15 | | -from sklearn.metrics import hinge_loss |
| 15 | +from sklearn.metrics import hinge_loss, accuracy_score |
16 | 16 | from sklearn.model_selection import KFold, train_test_split |
17 | 17 | from sklearn.svm import SVC |
18 | 18 |
|
19 | | -from hidimstat import CFI |
| 19 | +from hidimstat import CFI, LOCI |
20 | 20 |
|
21 | 21 | ############################################################################# |
22 | 22 | # To solve the XOR problem, we will use a Support Vector Classier (SVC) with Radial Basis Function (RBF) kernel. The decision function of |
|
82 | 82 | cv = KFold(n_splits=5, shuffle=True, random_state=0) |
83 | 83 | clf = SVC(kernel="rbf", random_state=0) |
84 | 84 | # 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_) |
100 | 88 |
|
101 | 89 | ########################################################################### |
102 | 90 |
|
|
129 | 117 | fig, axes = plt.subplots(1, 2, sharey=True, figsize=(6, 2.5)) |
130 | 118 | # Marginal scores boxplot |
131 | 119 | sns.boxplot( |
132 | | - data=np.array(marginal_scores).T, |
| 120 | + data=marginal_importances, |
133 | 121 | orient="h", |
134 | 122 | ax=axes[0], |
135 | 123 | fill=False, |
|
0 commit comments