question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add transformation option to inspect functions

See original GitHub issue

Describe the workflow you want to enable

I like the inspect module very much. Sometimes, interpretation of a model is more natural on a different scale (e.g. log) than on the scale of the predictions.

Here some examples:

  1. We fit a GLM. It is often more natural to inspect the model on the linear scale of its link function, not on the scale of the response variable.
  2. We fit a Poisson regression in XGBoost/LGB. These models are fitted on log scale, so it might be worth inspecting them on the log scale, even if it is only to compare with a benchmark GLM model.

Describe your proposed solution

Add an argument transformation to all explainers (partial dependence, permutation importance). By default, it is None (or the identity). The user can pass e.g. np.log to allow evaluation on the log scale.

  • Partial dependence plot: Here, it suffices to transform the predictions before averaging them.

  • Permutation importance: Here, both the response and the predictions need to be transformed. The scorer must be in line with the transformation and provided by the user.

Describe alternatives you’ve considered, if relevant

An alternative would be to change the prediction function of the Classifier/Regressor.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lorentzenchrcommented, Sep 3, 2020

To make the argument explicit, let us assume a PoissonRegressor (similiar for LogisticRegression) that predicts exp(a + b + c). On the “raw”/“log” scale, there are no interactions, i.e. a + b + c, whereas on the scale of y there is a 3-fold interaction exp(a) * exp(b) * exp(c). Therefore, it would be very convenient for a user, if she or he could switch the analysis scale for partial dependence plots.

TransformedTargetRegressor is a bit overkill. We don’t want to fit at all, but apply a transformation to predict of an already fitted model, i.e. a FunctionTransformer applied to the outcome of predict. But FunctionTransformer(lambda x: np.log(reg.predict(X))) doesn’t work as it has no predict method.

class PredictionTransformRegressor(BaseEstimator):
    """Apply transformation to an already fitted estimator."""
    def __init__(self, estimator=None, transform=np.log):
        self.estimator = estimator
        self.transform = transform
    def fit(self, X, y):
        # TODO: validate the parameters
        # This should be a no-op, i.e. we do not fit anything.
        return self
    def predict(self, X):
        # We do not need to check if self was fitted.
        return transform(self.estimator.predict(X))  # will call check_is_fitted(estimator)

reg = PoissonRegressor().fit(X, y)
transformed_reg = PredictionTransformRegressor(reg, transform=np.log)
partial_dependence(transformed_reg, X=X, features=...)

I wonder if a simple option in partial_dependence would be more user friendly.

0reactions
jnothmancommented, Sep 1, 2020

It sounds like for permutation importance the TransformedTargetRegressor will suffice too? So maybe we should trial such a change on partial dependence? Would you be willing to attempt a pull request?

Read more comments on GitHub >

github_iconTop Results From Across the Web

<transform-function> - CSS: Cascading Style Sheets | MDN
Chrome Edge Fire... Full support. Chrome1. Toggle history Full support. Edge12. Toggle history Full... matrix() Full support. Chrome1. Toggle history Full support. Edge12. Toggle history...
Read more >
Web.config Transformation Syntax for Web Project ...
Transformation actions are specified by using XML attributes that are defined in the XML-Document-Transform namespace, which is mapped to the ...
Read more >
Transform functions
Use transform functions to reformat text, perform mathematical calculations ... Insert a header card in a static or reference choice control.
Read more >
Specifying a transform (mapping operation)
Specify a transform, a cast function, or an XPath function between two or more elements by selecting from the list of available mapping ......
Read more >
Visual Studio 2015 Config Transformation option missing
Make sure the configuration exists first (you probably already have Debug and Release). If it doesn't, then add it in the Configuration Manager...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found