Skip to content

Commit

Permalink
Merge pull request #15 from HelmholtzAI-Consultants-Munich/neuronflow…
Browse files Browse the repository at this point in the history
…-patch-6

Update Tutorial_PermutationFeatureImportance.ipynb
  • Loading branch information
neuronflow committed Dec 4, 2023
2 parents 20ba57d + 269ae65 commit 41da5d1
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions xai-for-tabular-data/Tutorial_PermutationFeatureImportance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"source": [
"# Model-Agnostic Interpretation with Permutation Feature Importance\n",
"\n",
"In this Notebook we will demonstrate how to use the Permutation Feature Importance method and interpret its results.\n",
"In this Notebook, we will demonstrate how to use the Permutation Feature Importance method and interpret its results.\n",
"\n",
"--------\n"
]
Expand All @@ -35,8 +35,8 @@
"\n",
"### Setup Colab environment\n",
"\n",
"If you installed the packages and requirements on your own machine, you can skip this section and start from the import section.\n",
"Otherwise, you can follow and execute the tutorial on your browser. In order to start working on the notebook, click on the following button, this will open this page in the Colab environment and you will be able to execute the code on your own.\n",
"If you installed the packages and requirements on your machine, you can skip this section and start from the import section.\n",
"Otherwise, you can follow and execute the tutorial on your browser. To start working on the notebook, click on the following button. This will open this page in the Colab environment, and you will be able to execute the code on your own.\n",
"\n",
"<a href=\"https://colab.research.google.com/github/HelmholtzAI-Consultants-Munich/Zero2Hero---Introduction-to-XAI/blob/Juelich-2023/xai-for-tabular-data/Tutorial_PermutationFeatureImportance.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
Expand All @@ -46,13 +46,13 @@
"id": "cf1df952",
"metadata": {},
"source": [
"Now that you opened the notebook in Colab, follow the next step:\n",
"Now that you opened the notebook in Google Colab, follow the next step:\n",
"\n",
"1. Run this cell to connect your Google Drive to Colab and install packages\n",
"2. Allow this notebook to access your Google Drive files. Click on 'Yes', and select your account.\n",
"3. \"Google Drive for desktop wants to access your Google Account\". Click on 'Allow'.\n",
" \n",
"At this point, a folder has been created in your Drive and you can navigate it through the lefthand panel in Colab, you might also have received an email that informs you about the access on your Google Drive."
"At this point, a folder has been created in your Drive, and you can navigate it through the lefthand panel in Colab. You might also have received an email that informs you about the access on your Google Drive."
]
},
{
Expand Down Expand Up @@ -295,9 +295,9 @@
"id": "59fb5ef7",
"metadata": {},
"source": [
"Check again how the model performed on train and test set.\n",
"Check again how the model performed on the train and test set.\n",
"\n",
"*Note: The $R^2$ is the coefficient of determination and the closer this value is to 1, the better our model explains the data. A constant model that always predicts the average target value disregarding the input features would get an $R^2$ score of 0. However, $R^2$ score can also be negative because the model can be arbitrarily worse.*"
"*Note: The $R^2$ is the coefficient of determination, and the closer this value is to 1, the better our model explains the data. A constant model that always predicts the average target value disregarding the input features would get an $R^2$ score of 0. However, the $R^2$ score can also be negative because the model can be arbitrarily worse.*"
]
},
{
Expand Down Expand Up @@ -333,7 +333,7 @@
"source": [
"As we can see by the numbers above, our model performs well on the training set and also generalizes well to the independent test set.\n",
"\n",
"*Note: you should keep in mind that interpreting a low performing model can lead to wrong conclusions.*"
"*Note: you should keep in mind that interpreting a low-performing model can lead to wrong conclusions.*"
]
},
{
Expand Down Expand Up @@ -403,9 +403,9 @@
"id": "029ea700",
"metadata": {},
"source": [
"Now lets use Permutation Feature Importance to get some insights into the Random Forest Regression model we loaded above. We can use the scikit-learn implementation called `permutation_importance` to get the importance values for the features in our model. For measuring the performance drop when permuting a feature, we use the standard metric of our trained model, which is in our case the $R^2$ score. Using the same score enables us to evaluate the performace drop in relation to the baseline performance. We do 20 repetitions of permutation for each feature to get more reliable results.\n",
"Now, let's use Permutation Feature Importance to get insights into the Random Forest Regression model we loaded above. We can use the scikit-learn implementation called `permutation_importance` to get the importance values for the features in our model. For measuring the performance drop when permuting a feature, we use the standard metric of our trained model, which is, in our case, the $R^2$ score. Using the same score enables us to evaluate the performance drop in relation to the baseline performance. We do 20 repetitions of permutation for each feature to get more reliable results.\n",
"\n",
"*Note: this method is a **global** method which means that it does only provide explanations for the full dataset, but not for individual examples.*"
"*Note: this method is a **global** method, which means that it only provides explanations for the full dataset but not for individual examples.*"
]
},
{
Expand Down Expand Up @@ -473,9 +473,9 @@
"id": "dc5b6d1d",
"metadata": {},
"source": [
"Features with the largest importance score are the ones whose score is most decreased when the feature is permuted, indicating high relevance for prediction of the target. For our California Housing model, the *median income in a block group* seems to have the highest impact on the model performance. Permuting this feature leads to a $R^2$ score decrease of ~ 0.6. When comparing it to the baseline $R^2$ score of 0.84, we notice that permuting the *median income* feature leads to a model with $R^2$ close to 0, i.e. a constant model that always predicts the average target value disregarding the input features.\n",
"Features with the largest importance score are the ones whose score is most decreased when the feature is permuted, indicating high relevance for the prediction of the target. For our California Housing model, the *median income in a block group* seems to have the highest impact on the model performance. Permuting this feature leads to a $R^2$ score decrease of ~ 0.6. When comparing it to the baseline $R^2$ score of 0.84, we notice that permuting the *median income* feature leads to a model with $R^2$ close to 0, i.e., a constant model that always predicts the average target value disregarding the input features.\n",
"\n",
"We are not restricted to obtain feature importances of the same data set that we used to train the model. Instead we could use the same approach to identify the most important features in the test set."
"We are not restricted to obtaining feature importances of the same data set we used to train the model. Instead, we could use the same approach to identify the most important features in the test set."
]
},
{
Expand Down Expand Up @@ -818,7 +818,7 @@
"id": "21daa328",
"metadata": {},
"source": [
"As can be seen from above, the model performance does not change much when removing feature 1 from the dataset, which indicated that both models are comparable. However, if we look at the feature importance of the full model, we can see that the importance is split between the correlated feature 1 and feature 2. Even though the data we simulated does not depend on feature 1, the model still assigns quite high importance to this feature as well, due to its high correlation with feature 2, which can lead to wrong conclusions during model interpretation! If we compare the feature importance of the full model to the one where we removed feature 1, we can see that the importance of feature 2 increases, although the importance of the remaining features stays the same. This illustrates why you have to be careful when using Permutation Feature Importance for models that were trained on datasets with high feature correlations!"
"As can be seen from above, the model performance does not change much when removing feature 1 from the dataset, which indicates that both models are comparable. However, if we look at the feature importance of the full model, we can see that the importance is split between the correlated feature 1 and feature 2. Even though the data we simulated does not depend on feature 1, the model still assigns quite high importance to this feature as well, due to its high correlation with feature 2, which can lead to wrong conclusions during model interpretation! If we compare the feature importance of the full model to the one where we removed feature 1, we can see that the importance of feature 2 increases, although the importance of the remaining features stays the same. This illustrates why you have to be careful when using Permutation Feature Importance for models that were trained on datasets with high feature correlations!"
]
}
],
Expand Down

0 comments on commit 41da5d1

Please sign in to comment.