<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/understanding-titanic-dataset-with-h2os-automl-dalex-and-lares-library/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/understanding-titanic-dataset-with-h2os-automl-dalex-and-lares-library/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>If you have been studying or working with Machine Learning for at least a week, I am sure you have already played with the Titanic dataset! Today I bring some fun <code>DALEX</code> (Descriptive mAchine Learning EXplanations) functions to study the whole set’s response to the Survival feature and some individual explanation examples. </p>
<p>Before we start, I invite you all to install my personal library <a rel="nofollow" href="https://github.com/laresbernardo/lares" target="_blank">lares</a> so you can follow step by step the following examples and use it to speed up your daily ML and Analytics tasks:</p>
<pre>
devtools::install_github(&quot;laresbernardo/lares&quot;)
</pre>
<h2>A quick good model with h2o_automl</h2>
<p>Let’s run the <code>lares::h2o_automl</code> function to generate a quick good model on the Titanic dataset. I can’t remember from which source I downloaded the files originally but I will re-share them with you so you can re-produce the following examples: <a rel="nofollow" href="http://bit.ly/titanicTrain" target="_blank">Titanic Train Dataset</a> (used as only dataset because we need the Survival data to train and test our supervised model).</p>
<p>Having the data in our working directory, we can now join them, label them, do some quick cleanings, and run <code>h2o_automl</code> to create a fast model. The outcome is an object (results) with all the model’s results: name, metrics, predictions, datasets, variable importances, etc. </p>
<p>NOTE: when using the <code>lares::h2o_automl</code> function with our data frame as it is, with no ‘train_test’ parameter, it will automatically split 70/30 for our training and testing sets (use ‘split’ in the function if you want to change this relation).</p>
<pre>
library(dplyr)
library(lares)

seed &lt;- 123

train &lt;- read.csv(&quot;train.csv&quot;)

dfm &lt;- df %>%
  select(-Name, -Ticket, -PassengerId, -Cabin) %>%
  rename(&quot;tag&quot; = &quot;Survived&quot;) %>% 
  mutate(tag = as.factor(tag), 
         Pclass = as.factor(Pclass))

results &lt;- lares::h2o_automl(df = dfm, seed = seed, max_time = 60)
</pre>
<p>Let’s quickly check our model’s performance:</p>
<pre>
lares::mplot_full(tag = results$score$tag, 
                  score = results$scores$score,
                  subtitle = &quot;Titanic dataset&quot;)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_full-4.png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/07/viz_full-4-1024x640.png?w=450&ssl=1" alt="" class="wp-image-15965" data-recalc-dims="1"></a></p>
<p>Basically, we got a very nice performing model, with an AUC of 88.9% which splits quite well the Titanic’s survivals. Check out that from the top 25% scored-people, only 3% did not survive (the Captain? Sorry…) and 61% are true survivors. </p>
<p>Once we have this model, we can study which features did it chose as the most relevant and why. To understand this, there are probably more than a million ways, but today we are going to check the <a rel="nofollow" href="https://github.com/pbiecek/DALEX" target="_blank">DALEX</a> results. </p>
<h2>The most important variables</h2>
<p>Every dataset has relevant and irrelevant features. Sometimes it is our work as data scientist or analysts to detect which ones are these, how do they affect our independent variable and, most importantly, why. I happen to have the following function to help us see these results quickly:</p>
<pre>
lares::mplot_importance(var = results$importance$variable, 
                        imp = results$importance$percentage,
                        subtitle = &quot;Titanic dataset&quot;)
</pre>
<p>Which will plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-10.56.27-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-10.56.27-p.-m.-1024x498.png?w=450&ssl=1" alt="" class="wp-image-15966" data-recalc-dims="1"></a><br>
You can also plot something similar with the <code>DALEX</code> library but personally, I do like mine better! :$</p>
<h2>Partial Dependency Plots (PDP)</h2>
<p>Now that we know that Sex, Age, Fare, and Pclass are the most relevant features, we should check how the model detects the relationship between the target (Survival) and these features. Besides, these plots not only are incredibly powerful for communicating our insights to non-technical users, but will also help us implement with more confidence our models (less black-boxes) into production.</p>
<p>To start plotting with DALEX, we have to create an explainer. If you are using h2o or my functions above, this is all you need to do:</p>
<pre>
explainer &lt;- lares::dalex_explainer(df = results$datasets$test, 
                                       model = results$model)
</pre>
<p>So, let’s check our PDPs on our main features. Note that we will have different outputs regarding our variables’ class: if it’s a numerical value, then well have a Partial Dependency Plot (line-plot); if we have a categorical or factor variable, then we will get a Merging Path Plot (dendogram-plot).</p>
<pre>
lares::dalex_variable(explainer, &quot;Sex&quot;)
</pre>
<p>We get this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-10.58.13-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-10.58.13-p.-m.-1024x481.png?w=450&ssl=1" alt="" class="wp-image-15969" data-recalc-dims="1"></a></p>
<p>How chivalrous of us! Basically, if you were a man and survived, you were quite lucky (spoiler: and rich!).</p>
<pre>
lares::dalex_variable(explainer, &quot;Age&quot;)
</pre>
<p>We get this plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-10.59.11-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-10.59.11-p.-m.-1024x640.png?w=450&ssl=1" alt="" class="wp-image-15970" data-recalc-dims="1"></a></p>
<p>If you were a child, you shouldn’t even have to be scared when it all went ‘down’. Independently of your class, sex, and age, children were the ones who had a higher score, thus the highest probability of surviving. We get some picks around 33 years old and to hell with the elders. </p>
<pre>
lares::dalex_variable(explainer, &quot;Fare&quot;)
</pre>
<p>Another plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.00.48-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.00.48-p.-m.-1024x638.png?w=450&ssl=1" alt="" class="wp-image-15971" data-recalc-dims="1"></a></p>
<p>Basically, if you paid less than 50 bucks, you didn’t pay for the boats nor the lifesavers. So rude!</p>
<pre>
lares::dalex_variable(explainer, &quot;Pclass&quot;, force_class = &quot;factor&quot;)
</pre>
<p>The plot:<br>
<a rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.01.26-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.01.26-p.-m.-1024x393.png?w=450&ssl=1" alt="" class="wp-image-15972" data-recalc-dims="1"></a></p>
<p>To emphasize once more on the economical situation vs survivals, we see how the 1st and 2nd classes were “luckier” than the 3rd class passengers. </p>
<p>With these plots (and with a little help from the movie to put us in perspective) we now can understand better the macro situation of Titanic’s survivals. Now, let’s check some particular cases, as individuals, to go further into our analysis.</p>
<h2>Individual Interpretation</h2>
<p>The DALEX’s local interpretation functions are awesome! If you have used <a rel="nofollow" href="https://github.com/thomasp85/lime" target="_blank">LIME</a> before, in my taste, these are quite similar but better. Note than we can see that several predictors have zero contribution, while others have positive, and others negative contributions. </p>
<p>Before you start, let me tell you that each example we run lasts almost a minute to the plot… some patient when using this function!</p>
<h3>Subject #1 (Randomly chose #23):</h3>
<pre>
local23 &lt;- lares::dalex_local(explainer, 
                              row = results$datasets$test[&quot;23&quot;,], 
                              plot = TRUE)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.28.05-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.28.05-p.-m.-1024x388.png?w=450&ssl=1" alt="" class="wp-image-15981" data-recalc-dims="1"></a></p>
<p>Here we can see a specific woman who scored pretty OK: 0.699 (can’t be seen in the image). The predicted value for this individual observation was positively and strongly influenced by the Sex = Female and Age = 15. Alternatively, the Pclass = 3 variable reduced this person’s probability of surviving. </p>
<h3>Subject #2 (Worst score):</h3>
<pre>
results$datasets$test[results$scores$score == min(results$scores$score),]
<em> tag Pclass Sex Age SibSp Parch Fare Embarked
60 0 3 male 11 5 2 46.9 S
</em>
</pre>
<p>We have 1 person (out of 268) on our test set which scored 0.01. (Can’t help to mention that he really didn’t survive). Let’s study now this guy guy with our DALEX function:</p>
<pre>
local60 &lt;- lares::dalex_local(explainer,
                              row = results$datasets$test[&quot;60&quot;,], 
                              plot = TRUE)
</pre>
<p>Gives this plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.10.46-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.10.46-p.-m.-1024x401.png?w=450&ssl=1" alt="" class="wp-image-15974" data-recalc-dims="1"></a></p>
<p>This poor boy sailed with a 3rd class ticket, having 11 years, traveling with 5 siblings and bot parents. The most important features were his Sex = male, and his low Fare/Pclass. Even though we noticed that children are more probable to have survived, this little man might be one of the exceptions because of (maybe) his amount of familiars onboard. If you think about it, the story behind it might be that he was in the ship with all his brothers and parents, which were 3rd class as well, and sinked with them instead of being save alone. Sad (but true) story.</p>
<h3>Subject #3 (Best score):</h3>
<p>If we repeat the prior example but with the highest score, we get 4 women, all 1st class, embarked through C Gates, and all 100% survivors. Let’s take a look at one:</p>
<pre>
local196 &lt;- lares::dalex_local(explainer,
                               row = results$datasets$test[&quot;196&quot;,], 
                               plot = TRUE)
</pre>
<p>The plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.17.02-p.-m..png?ssl=1" target="_blank"><img sizes="(max-width: 1024px) 100vw, 1024px" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/07/Captura-de-pantalla-2018-07-31-a-las-11.17.02-p.-m.-1024x388.png?w=450&ssl=1" alt="" class="wp-image-15975" data-recalc-dims="1"></a><br>
This handsome old lady, 58, traveled in first class with no family members, and payed a 147$ fare. Our model detects that this person had a very high probability of surviving, mainly because she payed a lot and is a woman!</p>
<h2>Conclussions</h2>
<ul>
<li>One way to understand a dataset is running a model and analyzing the Machine Learning’s intelligence behind. </li>
<li>Studying the important variables on a macro view and some particular cases on a micro view will give us confidence and a global understanding. </li>
<li>Partial dependence plots are a great way to extract insights from complex models. They can be very useful when showing our insights and results with other people.</li>
<li>With <code>DALEX</code> we can stop showing our Machine Learning models to the world as plain black boxes.</li>
<li>With the <code>lares</code> library we can automate and fasten our daily tasks for Analytics and Machine Learning jobs.</li>
</ul>
<p>Hope this article was fun to read and something could be learnt! Don’t hesitate to comment beloew if you have any further question, comment or insight and I’ll be delighted to answer back as soon as possible. Please, keep in touch and feel free to contact me via <a rel="nofollow" href="https://www.linkedin.com/in/laresbernardo/" target="_blank">Linkedin</a> or <a rel="nofollow" href="https://datascienceplus.com/author/bernardo-lares/" target="_blank">email</a>.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/k-fold-cross-validation-in-stan/" title="Permanent Link toK-fold cross-validation in Stan" target="_blank">K-fold cross-validation in Stan</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/automated-text-feature-engineering-using-textfeatures-in-r/" title="Permanent Link toAutomated Text Feature Engineering using textfeatures in R" target="_blank">Automated Text Feature Engineering using textfeatures in R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/explaining-keras-image-classification-models-with-lime/" title="Permanent Link toExplaining Keras image classification models with LIME" target="_blank">Explaining Keras image classification models with LIME</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/image-classification-with-keras-in-roughly-100-lines-of-code/" title="Permanent Link toImage classification with keras in roughly 100 lines of code" target="_blank">Image classification with keras in roughly 100 lines of code</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/r-vs-python-image-classification-with-keras/" title="Permanent Link toR vs Python: Image Classification with Keras" target="_blank">R vs Python: Image Classification with Keras</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/understanding-titanic-dataset-with-h2os-automl-dalex-and-lares-library/"> R Programming – DataScience+</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/6i1tkf686Rs" height="1"></a></div>