2
$\begingroup$

Many sklearn estimators support two weighting schemes:

  • Per-class weights: given when creating the estimator object (e.g. clf = DecisionTreeClassifier(class_weight={'Cat':0.4, 'Dog':0.6})

  • Per-sample weights: given when fitting a created object (e.g. clf.fit(X, y, sample_weight=[0.1, 0.1, 0.8])

For an already fitted estimator, I can get the per-class weights with clf.get_params()['class_weight'].

But what's the right way (if any) to get, given a fitted estimator, the per-sample weights that were used?

$\endgroup$
5
  • 5
    $\begingroup$ A quick search of the source code, e.g. RandomForestClassifier's, doesn't find anywhere that sample_weight gets saved as a class attribute. I suspect that was a conscious decision: the sample weights are directly tied to the dataset, which also doesn't get saved for later use; that's why sample_weight appears in the fit method rather than the class instantiation. $\endgroup$
    – Ben Reiniger
    Feb 10, 2020 at 12:51
  • 1
    $\begingroup$ Thanks @BenReiniger - would you like to re-post this as an answer? It's actually pretty good, and I'll accept it if no better idea comes up. $\endgroup$
    – OmerB
    Feb 10, 2020 at 13:00
  • 1
    $\begingroup$ Cross-posted at stats.stackexchange.com/q/448780/232706, stackoverflow.com/q/60099960/10495893 $\endgroup$
    – Ben Reiniger
    Feb 10, 2020 at 20:46
  • $\begingroup$ Update: I removed the two other cross-posts, you can clear the comments (links won't be active anymore). $\endgroup$
    – OmerB
    Feb 11, 2020 at 13:34
  • $\begingroup$ The links are operable for anyone with sufficient privileges. $\endgroup$
    – user45817
    Feb 11, 2020 at 13:45

1 Answer 1

1
$\begingroup$

A quick search of the source code, e.g. RandomForestClassifier's, doesn't find anywhere that sample_weight gets saved as a class attribute. I suspect that was a conscious decision: the sample weights are directly tied to the dataset, which also doesn't get saved for later use; that's why sample_weight appears in the fit method rather than the class instantiation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.