Estimating feature importance, the easy way

How to compute the relative importance of features in neural networks

Romain Hardy
3 min readNov 9, 2020

Introduction

Given sufficient data, machine learning models can learn complex relationships between input features and output labels. Often, we are interested in the importances of features — the relative contributions of features to predictions made by a model. Feature importances are generally not evident, but there is a straightforward way to estimate them, which I will introduce in this article.

A simple example

In order to get an intuitive sense of how to estimate feature importances, we’ll work through an example using the Iris data set. Let’s load the data, split it, and preprocess it appropriately.

# Imports
import numpy as np
import sklearn
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
# Load the data
iris = load_iris()
X = iris.data
y = iris.target
# Create a train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Preprocess the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

Next, we’ll build a neural network classifier using scikit-learn’s MLPClassifier. There are many parameters we could tweak, but we will only specify two of them here: the hidden layer sizes and the learning rate.

# Create a classifier
clf = MLPClassifier(hidden_layer_sizes(8, 4),
learning_rate_init=0.01)
# Fit the classifier using the training set
clf.fit(X_train, y_train)
# Evaluate the classifier using the test set
acc = accuracy_score(y_test, y_pred)
print(acc)

Since this data set is rather simple, the accuracy score is close to 1. We’re now ready to estimate feature importances. Formally, the importance of feature j is given by

To summarize, a feature’s importance is the difference between the baseline score s and the average score obtained by permuting the corresponding column of the test set. If the difference is small, then the model is insensitive to permutations of the feature, so its importance is low. Conversely, if the difference is large, then the feature’s importance is high. The parameter n controls the number of permutations per feature — more permutations yields better estimates, at the cost of computation time. Note that we could have chosen a scoring metric other than accuracy, such as the F1 score.

The code for estimating feature importances is as follows:

def get_feature_importance(j, n):
s = accuracy_score(y_test, y_pred) # baseline score
total = 0.0
for i in range(n):
perm = np.random.permutation(range(X_test.shape[0]))
X_test_ = X_test.copy()
X_test_[:, j] = X_test[perm, j]
y_pred_ = clf.predict(X_test_)
s_ij = accuracy_score(y_test, y_pred_)
total += s_ij
return s - total / n

Let’s use this to compute and plot the importances of all features in the Iris data set.

# Feature importances
f = []
for j in range(X_test.shape[1]):
f_j = get_feature_importance(j, 100)
f.append(f_j)
# Plot
plt.figure(figsize=(10, 5))
plt.bar(range(X_test.shape[1]), f, color="r", alpha=0.7)
plt.xticks(ticks=range(X_test.shape[1]))
plt.xlabel("Feature")
plt.ylabel("Importance")
plt.title("Feature importances (Iris data set)")
plt.show()

As you can see, features 2 and 3 are more important to our model than features 0 and 1. It is tempting to interpret feature importances as probabilities, but this is incorrect. In general, feature importances do not sum to 1, as their scale depends on the score used to compute them. Therefore, feature importances should be interpreted relative to other features. It is possible to convert feature importances to percentages by rescaling them, however. I will also mention that feature importances can be negative. This occurs when the model’s predictions improve upon shuffling a feature, and signifies that the model is poor or that n should be increased.

--

--