Skip to content

Add friedman's H statistic #22383

Open

Description

@amueller

Describe the workflow you want to enable

It's a relatively straight-forward measure of whether there's significant interaction between variables in a model given the marginal and pairwise partial dependence.
We don't do a lot of statistics, but arguably this is more an interpretability feature.
See https://www.firmai.org/bit/interaction.html#theory-friedmans-h-statistic

Describe your proposed solution

We could implement it either as a function or at least as an example to copy &paste.

Describe alternatives you've considered, if relevant

People can always implement it themselves, but it might not be super straight-forward? We could also have an example, though.

Additional context

No response

Activity

  1. amueller commented on Feb 5, 2022

    @amueller
    MemberAuthor

    Something like this?

    from sklearn.datasets import load_diabetes
    from sklearn.ensemble import RandomForestRegressor
    import itertools
    import numpy as np
    
    diabetes = load_diabetes()
    rf = RandomForestRegressor(n_estimators=10).fit(diabetes.data, diabetes.target)
    
    from sklearn.inspection import partial_dependence
    univariate = {}
    for i in range(diabetes.data.shape[1]):
        univariate[i] = partial_dependence(rf, diabetes.data, features=[i], kind='average')['average']
        
    bivariate = {}
    for i, j in itertools.combinations(range(diabetes.data.shape[1]), 2):
        bivariate[(i, j)] = partial_dependence(rf, diabetes.data, features=[i, j], kind='average')['average']
    
    h = np.zeros((diabetes.data.shape[1], diabetes.data.shape[1]))
    for i, j in itertools.combinations(range(diabetes.data.shape[1]), 2):
        h[i, j] = ((bivariate[(i, j)] - univariate[i].reshape(1, -1, 1) - univariate[j].reshape(1, 1, -1) + diabetes.target.mean() ) ** 2).sum() / ((bivariate[(i, j)] - diabetes.target.mean())** 2).sum()

    having the means in the partial dependence plot makes it a bit weird but I think this should be about it?

    image

    plt.matshow((bivariate[(0, 9)] - univariate[0].reshape(1, -1, 1) - univariate[9].reshape(1, 1, -1)+ diabetes.target.mean()).squeeze())
    plt.colorbar()

    image

  2. niranjanorkat commented on Feb 6, 2022

    @niranjanorkat

    @amueller I'm adding the pull request for this feature

  3. added
    Hard
    and removed
    Needs Triage
    on Feb 6, 2022
  4. lorentzenchr commented on Feb 13, 2022

    @lorentzenchr
    Member

    I‘d be in favor of having it in scikit-learn, not just as example. It would be wonderful in combination with #15263.

  5. mayer79 commented on Feb 13, 2022

    @mayer79
    Contributor

    Good idea!

    Note: The partial dependence values are to be calculated not on a grid but rather on a random sample from the empirical distribution.

  6. mayer79 commented on Feb 13, 2022

    @mayer79
    Contributor

    IMHO, the model agnostic reference implementation is in Christoph Molnar's iml package in R https://github.com/christophM/iml/blob/main/R/Interaction.R

    It is not trivial to correctly calculate the H statistic, but a cool thing to have.

  7. amueller commented on Feb 28, 2022

    @amueller
    MemberAuthor

    @mayer79 hm the distribution makes sense, I hadn't thought about that in my quick hack. But that should be possible with the more low-level method, right? _partial_dependence_brute has grid. Or are there other differences?

  8. mayer79 commented on Mar 1, 2022

    @mayer79
    Contributor

    @amueller :

    • Parameter grid will solve the problem. Unfortunately, the computational burden is tremendous because the original H-statistic uses all rows as grid (one column for the one-dimensional PDP, two columns for the two-dimensional one). The next bullet points explain two usual tricks to reduce the burden to a realistic level.
    • Subsampling: Use only a sample of size m = 200 (or so) from all rows as grid. m should not be too small because we are representing a two-dimensional empirical distribution.
    • Weighting: If there are many discrete variables, the grids can be compressed to distinct rows and then use weighted PDP.
    • So far, you have calculated H-squared, which I actually find easier to interpret as the original H.
  9. Simar-B commented on Mar 23, 2022

    @Simar-B

    @amueller Why did you add and subtract diabetes.target.mean() mean in the numerator and denominator respectively for the h statistic

  10. amueller commented on Mar 29, 2022

    @amueller
    MemberAuthor

    @Simar-B because our partial dependence is a bit weird about the mean and it shouldn't be included.

    @mayer79 We can use the usual PDP trick for tree-based models, right? That seems more tricky, though, as we have to think about how to include the weighting. But as Hutter did it for functional anova, I think we should be able to do it here. @NicolasHug might have thoughts? Having just a slow implementation to start might still be useful, though, or maybe have some "reasonable" subsampling?

  11. fractionalhare commented on Jul 18, 2022

    @fractionalhare

    /take

  12. fractionalhare commented on Jul 18, 2022

    @fractionalhare

    I actually have a fully working implementation of this now, so I can open a PR soon. I am designing and implementing tests for it.

  13. lorentzenchr commented on Aug 12, 2022

    @lorentzenchr
    Member

    @fractionalhare It is usually best practice to open a PR even if the implementation is only work in progress. This way, it is visible that you are working on it, and you might even get some early review comments preventing work in wrong directions (depending on reviewer capacity, of course).

  14. conorosully commented on Nov 17, 2023

    @conorosully

    @fractionalhare Was this feature added in the end? I think it would be very useful.

  15. mayer79 commented on Nov 17, 2023

    @mayer79
    Contributor

    @conorosully: Not an answer to your question, just two comments:

    1. There is https://github.com/pyartemis/artemis/tree/master/artemis that includes H-statistics.
    2. I have recently summarized a couple of formulas and ideas in this pdf. It would be absolutely fantastic to have something like this in scikit-learn!
  16. linked a pull request that will close this issueENH Add Friedman's H-squared #28375on Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    HardNew Featuremodule:metrics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions