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.

could not determine class_counts_ from previously fitted classifier

See original GitHub issue

Describe the bug When running visualizer, I am receiving a warning :
yellowbrick/classifier/base.py:232: YellowbrickWarning: could not determine class_counts_ from previously fitted classifier

which results in fitting again the classifier.

To Reproduce

from yellowbrick.classifier import ClassificationReport, ConfusionMatrix
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb
import matplotlib.pyplot as plt

X, y = datasets.load_iris(return_X_y=True)

x_train, x_val, y_train, y_val = train_test_split(
						                        X,
						                        y,
						                        stratify=y,
						                        test_size = 0.2)

model = xgb.XGBClassifier(objective ='multi:softprob',
                                            num_class=3,
                                            use_label_encoder=False,
                                            enable_categorical=False,
                                            n_estimators=10) 

model.fit(x_train,
               y_train,
               early_stopping_rounds=10,
              eval_set=[(x_train, y_train), (x_val, y_val)])
   

fig, ax = plt.subplots()
visualizer = ClassificationReport(model,
                                                   is_fitted=True)
visualizer.score(x_val, y_val)
visualizer.show()

Dataset from sklearn import datasets

Expected behavior Since, we declare is_fitted=True, it should not fit again.

Traceback

/home/ggous/miniconda3/envs/sklearn/lib/python3.9/site-packages/yellowbrick/classifier/base.py:232: YellowbrickWarning: could not determine class_counts_ from previously fitted classifier
  warnings.warn(

Desktop (please complete the following information):

  • OS: Linux Mint
  • Python Version : 3.9.12 miniconda
  • Yellowbrick Version : 1.5

Additional context I think sometimes it takes too much time to fit again?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bbengfortcommented, Nov 2, 2022

@lwgray thank you for adding those suggestions!

@ggous if you’re in a Jupyter notebook, this StackOverflow post has some suggestions for preventing the image from being rendered. Otherwise clear_figure as @lwgray mentioned is probably your best bet.

1reaction
lwgraycommented, Nov 2, 2022

This code address both concerns but @bbengfort maybe you have a better trick to stop showing the plot. I feel like we answered this question before.

from yellowbrick.classifier import ClassificationReport, ConfusionMatrix
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb
import matplotlib.pyplot as plt

X, y = datasets.load_iris(return_X_y=True)

x_train, x_val, y_train, y_val = train_test_split(X,y,stratify=y,test_size = 0.2)

model = xgb.XGBClassifier(objective ='multi:softprob',
                                            num_class=3,
                                            use_label_encoder=False,
                                            enable_categorical=False,
                                            n_estimators=10) 

model.fit(x_train,
          y_train,
          early_stopping_rounds=10,
          eval_set=[(x_train, y_train), (x_val, y_val)])

# Specify class counts on the model
model.class_counts_ = 3
fig, ax = plt.subplots()
visualizer = ClassificationReport(model, is_fitted=True)
visualizer.score(x_val, y_val)

# Clear Figure works but @bbengfort might have a better approach
visualizer.show('test.png', clear_figure=True);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the yellowbrick.exceptions.NotFitted function in ...
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... "could not determine class_counts_ from...
Read more >
Source code for yellowbrick.classifier.confusion_matrix
If the model is not a classifier, an exception is raised. If the internal model is not fitted, it is fit when the...
Read more >
[Titanic]Decision tree complete evaluation
Anything that cannot be imported from sklearn.metrics is now part of the private ... could not determine class_counts_ from previously fitted classifier ......
Read more >
Can we use numpy array confusion matrix in Yellowbrick ...
You can pass the fitted model into Yellowbrick. The latest version of Yellowbrick checks whether the model is already fitted and will not...
Read more >
SK Part 3: Model Evaluation
Commonly used evaluation metrics for a binary classifier are as follows: ... We do not need to adjust prediction score (in fact, we...
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