<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Antoine Redier on Medium]]></title>
        <description><![CDATA[Stories by Antoine Redier on Medium]]></description>
        <link>https://medium.com/@antoine.redier2?source=rss-74e0e5c9b3b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-B1DHy_aO1wGbmZR.jpg</url>
            <title>Stories by Antoine Redier on Medium</title>
            <link>https://medium.com/@antoine.redier2?source=rss-74e0e5c9b3b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:41:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@antoine.redier2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introducing Trelawney : a unified Python API for interpretation of Machine Learning Models]]></title>
            <link>https://medium.com/analytics-vidhya/introducing-trelawney-a-unified-python-api-for-interpretation-of-machine-learning-models-6fbc0a1fd6e7?source=rss-74e0e5c9b3b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fbc0a1fd6e7</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Antoine Redier]]></dc:creator>
            <pubDate>Thu, 06 Feb 2020 09:25:59 GMT</pubDate>
            <atom:updated>2020-02-11T08:12:13.015Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introducing Trelawney : a unified Python API for interpretation of Machine Learning Models</h3><p>This article lays out the advantages of explaining black box machine learning models and explains how to do it with <a href="https://github.com/skanderkam/trelawney">trelawney</a>, a package that aims at simplifying the model interpretation workflow.</p><h3>The importance of interpretation: why we built Trelawney</h3><h4>Interpretability : The final step of building Machine Learning Models</h4><p>For a long time, models have focused on reaching high performances and not necessarily on explaining the causes of such results. For instance, computer vision problematics are more about being as precise as possible than about explaining what features led specifically to such outstanding precision… With the democratization of machine learning solutions, the need to understand the drivers of a specific model has become more and more apparent : many applications of machine learning algorithms <strong>cannot only rely on shiny figures</strong> but need the possibility to give easy access to their <strong>interpretation</strong>.</p><p>The interpretation first helps to <strong>assess the model relevance</strong> and to increase the confidence one may have in it. In that sense, it helps both technical and non-technical stakeholders. Indeed, most people will not blindly trust performance metrics, they will want that model and the product that is being built with it to “make sense”. This is the heart of explainability: <strong>making sense of black box models</strong>.</p><p>In addition, explaining how your model works enables non-technical stakeholders to <strong>derive more value from your model</strong>. Is the feature important ? If yes, in which direction does it drive the output? These are the type of questions we want to answer. For instance, in the original project for which we built Trelawney, the interpretation of the model was used to build a sales pitch for bankers.</p><p>Furthermore, being able to explain the predictions of your model will also enable you to <strong>build better products</strong>. For instance, if you are building a fraud detection system, being able to tell a user why their transaction was blocked might reduce friction and educate them.</p><h4>Trelawney : Python Package for all interpretability methods in one place</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*YAzdf3aQJGsmqnTyg5a8Ig.png" /></figure><p>This need for model interpretability led us to build <strong>Trelawney</strong> in collaboration with Quinten, a French consulting company specialised in Data Science and Data Translation. Indeed, Quinten works in applied machine learning, in particular in the healthcare and banking industries. For them, a good solution not only needs to meet analytical expectations, but should also be understandable and actionable on the field.</p><p>Our project during this collaboration was to give an appetence score for a banking product, for each given client, based on its profile.</p><p>In the context of selling a financial product, the banker would call a potential client to explain why the product would be relevant for them. An interpretation of the model therefore helps us build sales pitch (emphasising which characteristics in the client profile the banker should mention, and which one he/she should avoid).</p><p>We built several Machine Learning models, from the most interpretable ones (such as a single decision tree, or logistic regression) to less interpretable models (such as Random Forest, XGBoost and Neural Networks), and then applied several interpretability methods, model-dependent or model-agnostic, and global or local.</p><p>The whole philosophy behind the Trelawney package was to <strong>unify the different existing APIs</strong> of current explainability packages. Indeed, for the different methods we will present, there is an existing python implementation (usually provided by the researchers who introduced said methods). Our idea was to write a <strong>common API</strong> that would allow Data Scientists to learn how to use those methods once and focus on <strong>choosing the best one</strong> for their use case (instead of learning a new package each time). Furthermore, we wanted to create a unique <strong>go-to package</strong> for all-interpretability methods, with a unified syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*jIVNf-MM-AafL2LA-MUdEQ.gif" /></figure><p>We named our package Trelawney in honour of the professor of divination of Hogwarts, known for her capacity to interpret <strong>blackbox tea-cups</strong>.</p><h3>Interpretability methods, theory and implementation with Trelawney</h3><p>Speaking of all the methods we are bringing under the same roof, let’s spend a bit of time explaining each of them and how to use them with Trelawney. This will be the most theoretical part of this article and you might want to skip if you already know it.</p><p>We will do some demonstrations by explaining a Random Forest trained on the <strong>Titanic Dataset</strong>. You can find the <strong>full notebook</strong> <a href="https://www.kaggle.com/aredier/titanic-explaining-predictions-with-trelawney">here</a> .</p><p>We will first explain <strong>global interpretation methods</strong> with SHAP and surrogate models and then look at <strong>local explanation methods</strong> with LIME and SHAP again.</p><h3>Global interpretation</h3><p>The first type of explanation you will want to do is a <strong>general explanation of the model.</strong> The idea here is to tell which features are most influential on the model’s overall output. For this, there are two main methods that have been implemented in Trelawney: SHAP and using a Surrogate model.</p><h4>SHAP</h4><p>The first global interpretation method we will present is <a href="https://github.com/slundberg/shap">SHAP</a>. SHAP (SHapley Additive exPlanations) uses <strong>game theory</strong> to explain the prediction by computing the contribution of each feature to the prediction. It computes a <strong>fair measure</strong> of each feature’s contribution to the final output (increasing or decreasing the prediction). This is divided between <strong>intrinsic contribution</strong> (if the feature was alone) and the <strong>marginal contribution</strong> (when the feature is combined with other features). Bear in mind that SHAP tries to approximate the prediction, and therefore boils down to <strong>building a simpler model </strong>on top of the original one.</p><p>If you sum the absolute value of all the SHAP values for each individual prediction, you get the feature importance.</p><p>Below is the application of SHAP for our Titanic Example :</p><pre>&gt;&gt;&gt; explainer = ShapExplainer()</pre><pre>&gt;&gt;&gt; explainer.fit(model, x_train, y_train)</pre><p>We can then get the importance of each feature:</p><pre>&gt;&gt;&gt; importance_dict = explainer.feature_importance(x_train)</pre><p>Or we can directly use Trelawney’s graphs:</p><pre>&gt;&gt;&gt; explainer.graph_feature_importance(x_train)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*BZ-ZF57CAkTwOEEam5zZ3A.png" /><figcaption>SHAP feature importance graph</figcaption></figure><p>In the above graph we can validate our intuition of what is important in order to determine if one survives the Titanic.</p><p>You might wonder what the ‘rest’ column is. Trelawney lets you limit the graph to the n most impactful features (the rest is the importance of all the other features summed).</p><h4>Surrogate models</h4><p>The second global interpretability method available in Trelawney is using a surrogate model. The main idea behind this method is to fit a <strong>fully explainable model </strong>(such as a decision tree or a linear regression) on the output of your black box model.</p><p>Here is the implementation with a single decision tree:</p><p>We fit our surrogate model:</p><pre>&gt;&gt;&gt; explainer = SurrogateExplainer(DecisionTreeClassifier(max_depth=3))</pre><pre>&gt;&gt;&gt; explainer.fit(model, x_train, y_train)</pre><p>And we see how well it explains our model:</p><pre>&gt;&gt;&gt; explainer.adequation_score()</pre><pre>0.968562874251497</pre><p>This means a single decision Tree with a maximum depth of 3 <strong>explains 96.8% of our random forest</strong>’s predictions. The adequation score here defaults to accuracy but you can choose to use any sci-kit learn metric you wish (if you have an imbalanced dataset for instance).</p><p>The advantage here is that you can then visualize your model in a very intuitive way:</p><pre>&gt;&gt;&gt; explainer.plot_tree(out_path=’./tree_viz’)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K7I3MRct97VogAtr1dboMA.png" /><figcaption>surrogate tree visualisation</figcaption></figure><p>This approach has the advantage of not being an approximation of a value for each of your features (as for SHAP) since it is a <strong>truly global approach</strong> that takes into account the strong correlation that might occur between features. Furthermore, in our experience, this method is also <strong>fairly simple to explain</strong> to non technical stakeholders, as people understand the principle of decision trees.</p><p>Obviously a simple decision tree cannot be as accurate as a more complex model, but we can compute to what extent the decision tree is sufficient, and explain that, in the rest of the cases, the prediction is too complex to be approximated by a simple decision tree.</p><h3>Local interpretation methods</h3><p>The other type of explanation methods that you might want to produce are local explanations. Here you are not trying to get a feel of your model but you want to know <strong>how it came to a result for a given sample</strong>. If we take our fraud detection system example from earlier, your user will not really care that X or Y influences to block transactions globally, he or she wants to know why he or she got blocked right now. This type of explanation are also generally quite useful for us data-scientists as <strong>debugging tools</strong>. Indeed, being able to peek at how our model made outrageous predictions might help unveil some overfitting issues with our model as a whole or biases in our training data.</p><h4>LIME</h4><p>The first local method that is implemented in Trelawney is LIME (Local Interpretable Model-agnostic Explanations). Lime was one of the first model agnostic explainability methods introduced by <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Ribeiro%2C+M+T">Marco Tulio Ribeiro</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Singh%2C+S">Sameer Singh</a>, <a href="https://arxiv.org/search/cs?searchtype=author&amp;query=Guestrin%2C+C">Carlos Guestrin</a> in <a href="https://arxiv.org/abs/1602.04938">this paper</a>. As one of the oldest methods it is fairly well known by data scientists but we will recap quickly. LIME works by building <strong>local approximations</strong> of your model using an <strong>explainable model </strong>(generally linear regressions). Basically, if you want to explain a prediction, LIME will sample points around the input of your prediction, run your model for those artificial points and try to approximate the output using an explainable model. The resulting model will give you the local approximation around the prediction of interest.</p><p>Here is how you can use Lime with trelawney:</p><pre>&gt;&gt;&gt; explainer = LimeExplainer()</pre><pre>&gt;&gt;&gt; explainer.fit(model, x_train, y_train)</pre><pre>&gt;&gt;&gt; explainer.explain_local(x_test.loc[most_probable, :])</pre><pre>[{‘Age’: -0.029329040463817208,</pre><pre>‘Deck’: 0.15191169970680513,</pre><pre>‘Elderly’: 0.004273665522566747,</pre><pre>‘Embarked’: 0.05382052701464114,</pre><pre>‘Family_Size’: -0.020401593852417545,</pre><pre>‘Is_Married’: 0.01739687755247181,</pre><pre>‘Parch’: -0.033679921376068664,</pre><pre>‘Pclass’: 0.11590397989964185,</pre><pre>‘Sex’: 0.0012503083084444357,</pre><pre>‘SibSp’: -0.0029291606226194682,</pre><pre>‘rest’: 0.0}]</pre><p>Here, the local explanation of our biggest false positive can be used to further <strong>improve our model</strong> as we’d think where a passenger embarked shouldn’t have influenced whether they survived the disaster or not that much.</p><h4>SHAP for local interpretation</h4><p>In SHAP for local interpretation, instead of summing the contribution for each individual sample to get the overall SHAP value for a particular feature (as we did above), we only look at the <strong>SHAP values at the observation level.</strong></p><p>Here is the implementation:</p><pre>explainer = ShapExplainer()</pre><pre>explainer.fit(model, x_train, y_train)</pre><pre>explainer.graph_local_explanation(x_test.loc[biggest_false_positive, :])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*niPNh31eohqwfRHLaz2-nQ.png" /><figcaption>shap local explanation graph (biggest false positive)</figcaption></figure><h3>Final Thoughts and further improvements:</h3><p>All in all, explaining your model can <strong>boost</strong> dramatically the <strong>value</strong> you are able to get out of it and we hope Trelawney will <strong>simplify this process</strong> for you (by unifying several packages with the same syntax and workflow). Explaining your models might also become a necessity in a lot of fields as regulations might require to explain the outputs of your models. For instance, the <a href="http://www.privacy-regulation.eu/en/article-22-automated-individual-decision-making-including-profiling-GDPR.htm">article 22 of the European General Data Protection Regulation</a> states that “data subject shall have the right not to be subject to a decision based solely on automated processing”. The data scientist should then be able to provide explanations on the decision process. Such an opportunity is <strong>highly valuable </strong>for experts like Quinten as it allows to use black-box models when an increase in performance is needed <strong>without suffering from the lack of interpretability</strong>. Not to mention all the fields of application these new methods open.</p><p>Although we are proud of the work we have done so far, we still have a long way to go and have plenty of ways to <strong>improve Trelawney</strong>. Here are a few ideas of things that might come along in future versions:</p><ul><li>Support for regression problems (as Trelawney only supports classifiers as of writing)</li><li>More model-focused methods (for intrinsically explainable models)</li><li>Use more readable and faithful graphs</li></ul><p>If any of those projects interests you or if you just see a bug/typo you would feel like fixing, <strong>we would love you to contribute</strong> !</p><p><em>co-authored by </em><a href="https://medium.com/@antoine.redier2"><em>Antoine Redier</em></a><em> and </em><a href="https://medium.com/@amliemeurer"><em>Amélie Meurer</em></a><em>. Trelawney was built thanks to </em><a href="https://medium.com/@ludmilaex"><em>Ludmila Exbrayat</em></a><em>, </em><a href="https://medium.com/@insvanagt"><em>Inès VanAgt</em></a><em> and </em><a href="https://medium.com/@skander.kam2"><em>Skander Kamoun</em></a><em> and thanks to</em><a href="https://medium.com/@a.grangetas"><em> A. Grangetas</em></a><em> for his support and advice.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fbc0a1fd6e7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/introducing-trelawney-a-unified-python-api-for-interpretation-of-machine-learning-models-6fbc0a1fd6e7">Introducing Trelawney : a unified Python API for interpretation of Machine Learning Models</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>