|
| 1 | +# Sebastian Raschka 2014-2018 |
| 2 | +# mlxtend Machine Learning Library Extensions |
| 3 | +# |
| 4 | +# Feature Importance Estimation Through Permutation |
| 5 | +# Author: Sebastian Raschka <sebastianraschka.com> |
| 6 | +# |
| 7 | +# License: BSD 3 clause |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from sklearn.datasets import make_classification |
| 11 | +from sklearn.datasets import make_regression |
| 12 | +from sklearn.model_selection import train_test_split |
| 13 | +from sklearn.svm import SVC |
| 14 | +from sklearn.svm import SVR |
| 15 | +from mlxtend.utils import assert_raises |
| 16 | +from mlxtend.evaluate import feature_importance_permutation |
| 17 | + |
| 18 | + |
| 19 | +def test_num_rounds_not_int(): |
| 20 | + assert_raises(ValueError, |
| 21 | + 'num_rounds must be an integer.', |
| 22 | + feature_importance_permutation, |
| 23 | + lambda x, y: (x, y), |
| 24 | + np.array([[1], [2], [3]]), |
| 25 | + np.array([1, 2, 3]), |
| 26 | + 'accuracy', |
| 27 | + 1.23) |
| 28 | + |
| 29 | + |
| 30 | +def test_num_rounds_negative_int(): |
| 31 | + assert_raises(ValueError, |
| 32 | + 'num_rounds must be greater than 1.', |
| 33 | + feature_importance_permutation, |
| 34 | + lambda x, y: (x, y), |
| 35 | + np.array([[1], [2], [3]]), |
| 36 | + np.array([1, 2, 3]), |
| 37 | + 'accuracy', |
| 38 | + -1) |
| 39 | + |
| 40 | + |
| 41 | +def test_metric_wrong(): |
| 42 | + assert_raises(ValueError, |
| 43 | + ('metric must be either "r2", "accuracy", or a ' |
| 44 | + 'function with signature ' |
| 45 | + 'func(y_true, y_pred).'), |
| 46 | + feature_importance_permutation, |
| 47 | + lambda x, y: (x, y), |
| 48 | + np.array([[1], [2], [3]]), |
| 49 | + np.array([1, 2, 3]), |
| 50 | + 'some-metric') |
| 51 | + |
| 52 | + |
| 53 | +def test_classification(): |
| 54 | + |
| 55 | + X, y = make_classification(n_samples=1000, |
| 56 | + n_features=6, |
| 57 | + n_informative=3, |
| 58 | + n_redundant=0, |
| 59 | + n_repeated=0, |
| 60 | + n_classes=2, |
| 61 | + random_state=0, |
| 62 | + shuffle=False) |
| 63 | + |
| 64 | + X_train, X_test, y_train, y_test = train_test_split( |
| 65 | + X, y, test_size=0.3, random_state=0, stratify=y) |
| 66 | + |
| 67 | + svm = SVC(C=1.0, kernel='rbf', random_state=0) |
| 68 | + svm.fit(X_train, y_train) |
| 69 | + |
| 70 | + imp_vals, imp_all = feature_importance_permutation( |
| 71 | + predict_method=svm.predict, |
| 72 | + X=X_test, |
| 73 | + y=y_test, |
| 74 | + metric='accuracy', |
| 75 | + num_rounds=1, |
| 76 | + seed=1) |
| 77 | + |
| 78 | + assert imp_vals.shape == (X_train.shape[1], ) |
| 79 | + assert imp_all.shape == (X_train.shape[1], 1) |
| 80 | + assert imp_vals[0] > 0.2 |
| 81 | + assert imp_vals[1] > 0.2 |
| 82 | + assert imp_vals[2] > 0.2 |
| 83 | + assert sum(imp_vals[3:]) <= 0.02 |
| 84 | + |
| 85 | + |
| 86 | +def test_regression(): |
| 87 | + |
| 88 | + X, y = make_regression(n_samples=1000, |
| 89 | + n_features=5, |
| 90 | + n_informative=2, |
| 91 | + n_targets=1, |
| 92 | + random_state=123, |
| 93 | + shuffle=False) |
| 94 | + |
| 95 | + X_train, X_test, y_train, y_test = train_test_split( |
| 96 | + X, y, test_size=0.3, random_state=123) |
| 97 | + |
| 98 | + svm = SVR(kernel='rbf') |
| 99 | + svm.fit(X_train, y_train) |
| 100 | + |
| 101 | + imp_vals, imp_all = feature_importance_permutation( |
| 102 | + predict_method=svm.predict, |
| 103 | + X=X_test, |
| 104 | + y=y_test, |
| 105 | + metric='r2', |
| 106 | + num_rounds=1, |
| 107 | + seed=123) |
| 108 | + |
| 109 | + assert imp_vals.shape == (X_train.shape[1], ) |
| 110 | + assert imp_all.shape == (X_train.shape[1], 1) |
| 111 | + assert imp_vals[0] > 0.2 |
| 112 | + assert imp_vals[1] > 0.2 |
| 113 | + assert sum(imp_vals[3:]) <= 0.01 |
| 114 | + |
| 115 | + |
| 116 | +def test_n_rounds(): |
| 117 | + |
| 118 | + X, y = make_classification(n_samples=1000, |
| 119 | + n_features=6, |
| 120 | + n_informative=3, |
| 121 | + n_redundant=0, |
| 122 | + n_repeated=0, |
| 123 | + n_classes=2, |
| 124 | + random_state=0, |
| 125 | + shuffle=False) |
| 126 | + |
| 127 | + X_train, X_test, y_train, y_test = train_test_split( |
| 128 | + X, y, test_size=0.3, random_state=0, stratify=y) |
| 129 | + |
| 130 | + svm = SVC(C=1.0, kernel='rbf', random_state=0) |
| 131 | + svm.fit(X_train, y_train) |
| 132 | + |
| 133 | + imp_vals, imp_all = feature_importance_permutation( |
| 134 | + predict_method=svm.predict, |
| 135 | + X=X_test, |
| 136 | + y=y_test, |
| 137 | + metric='accuracy', |
| 138 | + num_rounds=100, |
| 139 | + seed=1) |
| 140 | + |
| 141 | + assert imp_vals.shape == (X_train.shape[1], ) |
| 142 | + assert imp_all.shape == (X_train.shape[1], 100) |
| 143 | + assert imp_vals[0].mean() > 0.2 |
| 144 | + assert imp_vals[1].mean() > 0.2 |
0 commit comments