Skip to content

[WIP] Add classes parameter to all classifiers - #9532

Closed
thismlguy wants to merge 121 commits into
scikit-learn:mainfrom
thismlguy:add_classes_all_classifiers
Closed

thismlguy wants to merge 121 commits into
scikit-learn:mainfrom
thismlguy:add_classes_all_classifiers

Conversation

@thismlguy

@thismlguy thismlguy commented Aug 11, 2017 •

Copy link
Copy Markdown
Contributor

A WIP PR, ref: #8100 and #9290

Added classes to following classifiers:

  • GaussianNB
  • BernoulliNB
  • MultinomialNB
  • DecisionTreeClassifier
  • ExtraTreeClassifier
  • RandomForestClassifier
  • ExtraTreesClassifier
  • GradientBoostingClassifier [WIP]
  • LinearSVC
  • SVC (on hold)
  • LogisticRegression
  • LogisticRegressionCV

To be done:

  • Others classifiers
  • sparse input support (svm, gbm, etc)

aarshayj added 30 commits July 5, 2017 14:50

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it possible to see something more refactored and conventionalised, like:

def fit(X, y):
    self.classes_ = check_classes(self.classes, y, multioutput=False)

and sometimes

    self.classes_, label_encoders = check_classes(self.classes, y, multioutput=True, return_encoder=True)

?

(classes, ndim, str(classes_k),
classes_k.dtype.name))
if unq.size != np.asarray(classes_k).size:
raise ValueError("Classses parameter should contain all unique"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sss -> ss

Comment thread sklearn/ensemble/gradient_boosting.py Outdated
"""LAD updates terminal regions to median estimates. """
terminal_region = np.where(terminal_regions == leaf)[0]
sample_weight = sample_weight.take(terminal_region, axis=0)
diff = y.take(terminal_region, axis=0) - pred.take(terminal_region, axis=0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please please please do not litter your work with unrelated changes. This makes it so much harder to review, particularly when it's a big patch. It also introduces unnecessary merge conflicts into other PRs modifying this code. We know the repo contains non-PEP8 code. Once it's merged we leave it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually I had a chat with @amueller about this earlier and he suggested that its a good idea to fix the non-pep8 code first whenever I start modify a file.
But I see your point. I'm sorry but I can't really do anything about it now. I'll make these changes in separate PRs in future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be pretty straight-forward to undo....

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

should be pretty straight-forward to undo....

you mean manually inspect all the changes and then undo? or is there a better way?
i just thought i could better use time in doing other classifiers than undoing all pep8 fixes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can do git checkout filename master and use git gui or a diff tool to look at the diff and commit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but yeah, not that urgent.

permutation_test_score(clf, X_df, y_ser)


def test_cross_val_score_labels_paramter():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

*parameter

Comment thread sklearn/naive_bayes.py Outdated
# case when not set, infer from y
if self.classes is None:
# np.unique should not be used here becasue unique_labels performs
# some checkes as well

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

*checks

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Brief comments on estimator checks.

yield check_classes_parameter_sorted_unique

if hasattr(estimator, 'predict_proba'):
yield check_classes_parameter_predict_proba

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure why you want a separate test for predict_proba and decision_function. Can they be merged? You also need to check that the value of classes is copied to classes_ when present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

there are a couple of reasons why:

  1. in a binary classification case, predict_proba returns 2 vectors and decision function only 1.
  2. when passing extra classes, not present in the data, then probabilities for those classes should be 0, but what should be the decision function for those classes? it depends on the estimator I suppose so the checks are slightly different for both cases.

i can create if else structure in the check, but i thought it might be cleaner code to keep them separate.

i'll add the check you suggested. we do something similar in scorer but it should be here as well.

pred0 = clf.fit(X, y).predict_proba(X)

params['classes'] = [1, 2]
clf.set_params(**params)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This requires the estimator to have a classes attribute and in turn creates a backwards incompatibility for users of check_estimator. At least for a couple of versions, we should only warn if a classifier providing predict_proba, predict_log_proba or decision_function lacks a classes parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

right now, these checks are only for the classifiers which i've updated. but I think you're talking about user-defined estimators. would it work that we should just perform this check if classes is present as a parameter in the estimator? will we be specifying that classes parameter should be present in all user-defined classifiers?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm actually not sure if we should require that. It's a good question. I think for now it would be fine if we skip the test if classes is not a parameter.

@amueller

Copy link
Copy Markdown
Member

merge conflict and failing test. Do you want reviews?

@thismlguy

Copy link
Copy Markdown
Contributor Author

merge conflict and failing test. Do you want reviews?

the merge conflict came up today, probably because of some recent merge. failing test is just in gradientboosting.py which I'm trying to fix now. I wanted a review of the overall idea, I'm good with @jnothman's points now, I can work on them.

@amueller

Copy link
Copy Markdown
Member

given all the problems we run into with this, I'm not entirely sure any more if this is the right solution. Maybe stop work on it for now and let's step back and discuss in #8100.

@thismlguy

Copy link
Copy Markdown
Contributor Author

I've been trying to wrap my head around GBM. I suspect the error is because Binomial Deviance uses LogOddsEstimator for initialization which sets priors as 0 if 2 classes are equal in number. On the other hand, Multinomial Deviance uses PriorProbabilityEstimator which will set priors as 0.5 each if 2 classes are same.

So I don't think they will give the same residuals and final trees if we try to model 2 classes using Multinomial Deviance.

I even checked by having a y with 3 classes and then comparing the models without setting classes and another with classes set as [1,2,3,4]. Even then the results are not the same because the negative gradient is:
return y - np.nan_to_num(np.exp(pred[:, k] - logsumexp(pred, axis=1)))
The logsumexp will have an extra 1 added because of the extra 0 prior. So the results wont be the same.

@thismlguy

Copy link
Copy Markdown
Contributor Author

One way around could be that if y has only 2 classes but more are passed in classes argument, then we raise a warning and make estimators for only 1 class. Or we still use Multinomial Deviance but not expect the result of the 2 cases to be same.
@amueller , @jnothman - thoughts?

@adrinjalali

Copy link
Copy Markdown
Member

This would need a fresh start if there's interest, and probably better with a smaller PRs than a large one.

@adrinjalali adrinjalali closed this Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API Needs Decision Requires decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants