<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/exploring-lime-on-the-house-prices-dataset/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.haunschmid.name/lime-on-regression-model-house-prices/"> r-bloggers – verenahaunschmid</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Pretty recently I found a paper with the title <a rel="nofollow" href="https://arxiv.org/abs/1602.04938" target="_blank">“Why Should I Trust You?”: Explaining the Predictions of Any Classifier</a>. The topic of interpretability is very important in the times of complex machine learning models and it’s also related to my PhD topic (reliability of machine learning models). Therefore I wanted to play around with the method a little bit. The method that was introduced in the paper is called LIME (Local Interpretable Model-Agnostic Explanations) and comes with a <a rel="nofollow" href="https://github.com/marcotcr/lime" target="_blank">python package</a>. Luckily for me, someone already ported it to R (<a rel="nofollow" href="https://github.com/thomasp85/lime" target="_blank">thomasp85/lime</a>). In this post I will show how I used LIME on regression models.</p>
<h2>What is LIME?</h2>
<p>LIME is a tool for explaining what a complex (often called black-box) machine learning model does. This is achieved by learning simple (e.g. linear regression) models on perturbed input data to figure out which features are important. One example is to segment images into <a rel="nofollow" href="https://www.pyimagesearch.com/2014/07/28/a-slic-superpixel-tutorial-using-python/" target="_blank">super pixels</a> and then turning parts of them “off” (make them gray). This helps the explainer identify important features (e.g. regions of the image). The method does not only work for images, but also text and tabular data. LIME is model-agnostic, which means, that it does not matter which model you are using. To get a better and much more detailed explanation by the author himself, check out <a rel="nofollow" href="https://www.oreilly.com/learning/introduction-to-local-interpretable-model-agnostic-explanations-lime" target="_blank">this article introducing LIME</a>.</p>
<h2>LIME and R</h2>
<p>Before using lime in R, I recommend reading the vignette <a rel="nofollow" href="https://github.com/thomasp85/lime/blob/master/vignettes/Understanding_lime.Rmd" target="_blank">Understanding lime</a>. The <a rel="nofollow" href="https://github.com/thomasp85/lime/blob/master/README.md" target="_blank">README</a> and a <a rel="nofollow" href="https://github.com/thomasp85/lime/issues/62#issuecomment-357206477" target="_blank">comment on a reported issue</a> state that the focus is classification, but that the package also covers regression. I have also noticed during my experiments that the plots are more intuitive for classification tasks (especially <em>plot_features()</em>) or only work for classification results (<em>plot_explanations()</em>).</p>
<h2>My chosen dataset</h2>
<p>Some time ago I stumbled over an <a rel="nofollow" href="https://www.kaggle.com/c/house-prices-advanced-regression-techniques" target="_blank">advanced house prices dataset</a> and since that I wanted to play around with it. Now I finally have a reason <img alt="😉" src="https://s.w.org/images/core/emoji/2.3/72x72/1f609.png" class="wp-smiley"> The dataset has 81 columns (including the target) and 1460 rows. So I guess there really is potential for advance regression techniques.</p>
<p>I chose it for exploring lime because it has many features (categorical and numerical ones), but is different than the datasets that are used in other tutorials/articles (usually text or images).</p>
<h2>LIME on random forests for predicting house prices</h2>
<p>Let’s look into the code I wrote for my analysis (<a rel="nofollow" href="https://github.com/expectopatronum/code-snippets-blog/blob/master/r-lime/lime-house-prices.pdf" target="_blank">markdown report with code</a>).</p>
<h3>Loading data &amp; building a model</h3>
<p>The first steps were loading the data and building a machine learning model. I chose to use random forests and the caret package.</p>
<p>Since I do not compare several models, computing the RMSE or other measures didn’t make to much sense for me. Instead of that I created two plots to show the performance. The model works fine for cheaper houses, but it can easily be seen that it performs poorly for more expensieve houses.</p>
<figure class="wp-caption"><a rel="nofollow" href="https://i2.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/rf_performance.png" target="_blank"><img sizes="(max-width: 400px) 100vw, 400px" src="https://i2.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/rf_performance.png?resize=400%2C370" alt="Predicted vs. reference values on the house prices dataset." width="400" class="wp-image-6909" data-recalc-dims="1" height="370"></a><figcaption class="wp-caption-text">Predicted vs. reference values.</figcaption></figure>
<figure class="wp-caption"><a rel="nofollow" href="https://i1.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/rf_residuals.png" target="_blank"><img sizes="(max-width: 400px) 100vw, 400px" src="https://i1.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/rf_residuals.png?resize=400%2C370" alt="Residuals against reference values." width="400" class="wp-image-6910" data-recalc-dims="1" height="370"></a><figcaption class="wp-caption-text">Residuals against reference values.</figcaption></figure>
<h3>Building an explainer &amp; explanations</h3>
<p>The explainer is built by using the method <em>lime()</em>. Then the explanations can be created with the method <em>explain()</em> by passing single samples, a subset of the samples or the whole test set.</p>
<p><em>lime()</em> mainly gathers information about the data and stores it in the <em>explainer</em> object. It stores the type of column (factor, numeric, …) and computes distributions of the features (either using bins or mean and standard deviation, depending on the settings passed by the user).</p>
<p><em>explain()</em> is the main part of the package in my opinion. In a first step, the samples of the test set get permuted. By default 5000 “new samples” are created for each sample. How the samples are permuted depends on some user settings and the data type of each feature. The user can decide whether continuous variables should be cut into bins or not. Then for each of the permuted samples a prediction is made using the trained machine learing model. The next step is where the magic happens. A loop iterates over each sample (of the test set). In each iteration all permutations of this sample are used to build a linear model only using a few features (n_features is also set by the user). There are different ways how the features can be selected, one of the methods is forward selection. In this step also the feature weights (which are the coefficients of the linear model) are computed. This will later tell us which features are the most important ones for a given new sample and also whether their influence is positive or negative. The return value of this method will be a data.frame with one row per sample and feature.</p>
<h3>Visualising the model explanations</h3>
<p>At first I was confused by the red and green bars. For classification it seems more intuitive to me. My interpretation for regression models (since the feature weights are the same as the coefficients of the local linear model) is that a negative bar just means that a larger value in this feature makes the predicted value smaller. </p>
<p>When you look at the plot and the features, this somehow makes sense. The feature “grlivarea” (above grade (ground) living area square feet) is important for many samples. And when the value is in the bin with the largest values (greater than 1764), the bar is green, since this increases the price. Other samples for which “grlivarea” is also important but smaller have a red bar since this decreases the selling price. It’s also interesting to compare Case 2 (overallqual = overall quality between 6 and 7) and Case 4 (overallqual lower than 5). “overallqual” is on a scale from 1 (Very Poor) to 10 (Very Excellent). For Case 4 the overall quality is “Average” or less, which decreases the prediction of the sale price. For Case 2 the quality is between “Above Average” and “Good” and this has a positive influence on the prediction of the sales price.</p>
<p>We can also see other interesting things in the graph. Only 2 features seem to be enough, because for each case the 3rd feature has a really low feature weight.</p>
<figure class="wp-caption"><a rel="nofollow" href="https://i0.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/lime_unscaled01.png" target="_blank"><img sizes="(max-width: 1000px) 100vw, 1000px" src="https://i0.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/lime_unscaled01.png?w=450" alt="Output of the function plot_features of the R package lime." class="wp-image-6907" data-recalc-dims="1"></a><figcaption class="wp-caption-text">Feature explanations on the unscaled data.</figcaption></figure>
<h2>Perform analysis on scaled data</h2>
<p>To get rid of features that might have a huge influence, I also did the analysis with scaled data. </p>
<p>When looking at the graph, one can see that the ranking and weights of the features changed a bit (but that can also be due to randomisation and not only because of scaling the features). It is still the case that only two features seem to explain most of the prediction. A big disadvantage is that the values of the features cannot be interepreted anymore easily, you would need to transform them back or look at the exact value of each sample if that is relevant.</p>
<figure class="wp-caption"><a rel="nofollow" href="https://i2.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/lime_scaled01.png" target="_blank"><img sizes="(max-width: 1000px) 100vw, 1000px" src="https://i2.wp.com/blog.haunschmid.name/wp-content/uploads/2018/01/lime_scaled01.png?w=450" alt="Output of the function plot_features of the R package lime." class="wp-image-6906" data-recalc-dims="1"></a><figcaption class="wp-caption-text">Feature explanations on the unscaled data.</figcaption></figure>
<h2>Final remarks</h2>
<p>It was quite interesting to read the paper and play around with the R implementation of the paper. I think that this method makes a lot of sense for classification (images, text, …) and I’m looking forward to using it on image data when the R package will also cover this functionality.</p>
<p>I’m not yet sure if my interpretation of the output for regression problems is correct but if it is, then it will also be a useful method for regression. In the case of using scaled data it would be very nice if the outputs (explanations and plots) could use the unscaled values in the feature descriptions to make it easier to interpret. </p>
<p>Now I’m curious what you think about the method. Have you heard about the paper before? Do you see a use case for local interpretable model-agnostic explanations?</p>
<p>The post <a rel="nofollow" href="http://blog.haunschmid.name/lime-on-regression-model-house-prices/" target="_blank">Exploring lime on the house prices dataset</a> appeared first on <a rel="nofollow" href="http://blog.haunschmid.name/" target="_blank">verenahaunschmid</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.haunschmid.name/lime-on-regression-model-house-prices/"> r-bloggers – verenahaunschmid</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/86tbdMh8Wzg" height="1"></a></div>