Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KElbow get_params #1251

Merged

Conversation

bbengfort
Copy link
Member

@bbengfort bbengfort commented May 21, 2022

This PR fixes #1232 which reported a bug that caused a problem to occur when users call KElbow.get_params(), e.g. in a notebook when they want to render the scikit-learn rich visualization of the estimator.

I have made the following changes:

  1. Updated KElbow to ensure it stores all hyperparams and only creates learned params in fit()
  2. Added tests to ensure get_params works for KElbow
  3. Updated the wrapper to prevent infinite recursion and give a more meaningful attribute error for future debugging

Sample Code and Plot

from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer
visualizer = KElbowVisualizer(KMeans(), k=(2,12))
visualizer.get_params()

The above code snippet should not produce an exception: AttributeError: 'KMeans' object has no attribute 'k'

Questions for the @DistrictDataLabs/team-oz-maintainers:

  • Should we add a test for get_params for all our models to make sure this doesn't happen?
  • @lwgray this is related to an issue we were experiencing in notebooks before; where the exception happened if the visualizer was the last line in the cell and scikit-learn was trying to render it; do you remember what Visualizer that was?

CHECKLIST

  • Is the commit message formatted correctly?
  • Have you noted the new functionality/bugfix in the release notes of the next release?
  • Included a sample plot to visually illustrate your changes?
  • Do all of your functions and methods have docstrings?
  • Have you added/updated unit tests where appropriate?
  • Have you updated the baseline images if necessary?
  • Have you run the unit tests using pytest?
  • Is your code style correct (are you using PEP8, pyflakes)?
  • Have you documented your new feature/functionality in the docs?
  • Have you built the docs using make html?

@bbengfort bbengfort requested a review from lwgray May 21, 2022 18:20
@codecov
Copy link

codecov bot commented May 21, 2022

Codecov Report

Merging #1251 (941e793) into develop (233d9d1) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           develop    #1251   +/-   ##
========================================
  Coverage    90.57%   90.58%           
========================================
  Files           92       92           
  Lines         5209     5213    +4     
========================================
+ Hits          4718     4722    +4     
  Misses         491      491           
Impacted Files Coverage Δ
yellowbrick/model_selection/dropping_curve.py 92.18% <ø> (ø)
yellowbrick/cluster/elbow.py 97.81% <100.00%> (-0.04%) ⬇️
yellowbrick/utils/wrapper.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 233d9d1...941e793. Read the comment docs.

@lwgray
Copy link
Contributor

lwgray commented May 21, 2022

It was dropping curve...

@lwgray
Copy link
Contributor

lwgray commented May 21, 2022

@bbengfort You should open an issue to create get_params test.

@bbengfort
Copy link
Member Author

I just checked DroppingCurve - get_params works fine, so I guess you fixed it.

@bbengfort
Copy link
Member Author

@lwgray Issue #1252 opened at your request.

@bbengfort
Copy link
Member Author

I also added a test for DroppingCurve.get_params.

Copy link
Contributor

@lwgray lwgray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@lwgray lwgray merged commit ad0d133 into DistrictDataLabs:develop May 22, 2022
@bbengfort bbengfort deleted the gh-1232/fix-kelbow-get-params branch May 22, 2022 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KElbowVisualizer wrapping functionality does not work with notebook rendering of estimator
2 participants