Skip to content

[WIP] Sample weight consistency #5515

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

Closed
wants to merge 17 commits into from
Closed

[WIP] Sample weight consistency #5515

wants to merge 17 commits into from

Conversation

ainafp
Copy link

@ainafp ainafp commented Oct 21, 2015

Supersedes #5461

Added a 0/1 sample weight test. Failing estimators are

AdaBoostRegressor, BaggingClassifier, BaggingRegressor, CalibratedClassifierCV, LogisticRegressionCV, Perceptron, RandomForestClassifier, RandomForestRegressor, RidgeCV, RidgeClassifierCV, SGDClassifier, SGDRegressor

For the previous test,

AdaBoostRegressor, BaggingRegressor, DecisionTreeRegressor, ExtraTreesRegressor, GradientBoostingRegressor, LogisticRegressionCV, Perceptron, RandomForestRegressor, 
RidgeCV, RidgeClassifierCV, SGDClassifier, SGDRegressor

The difference between the 2 are:

BaggingClassifier, CalibratedClassifierCV, DecisionTreeRegressor, ExtraTreesRegressor, GradientBoostingRegressor, RandomForestClassifier

How do I proceed? Do I create an exclusion list or an inclusion list (p.ex. linear models) for the previous test?

ping @eickenberg, @amueller, @glouppe, @arjoly, @agramfort, @GaelVaroquaux

set_random_state(estimator_aug, random_state=random_state)
estimator_aug.fit(X_aug_train, y_aug_train)

except ValueError:
Copy link
Contributor

Choose a reason for hiding this comment

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

@MechCoder what did you mean by the comment

a if name == check might be better.

that you put here?

Copy link
Member

Choose a reason for hiding this comment

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

I meant instead of try catch block, it would be safer to have a list of Estimators that have sample_weight in the fit param but might not support it. (which in this case is just LogisticRegression)

wdyt

@amueller
Copy link
Member

see if you can make the SGDClassifier and Perceptron working maybe? might be a convergence issue. Or we ask to high a precision on the coef.

@ainafp
Copy link
Author

ainafp commented Oct 22, 2015

I couldn't make SGDClassifier and Perceptron work, so I exclude them also. The excluded are
'AdaBoostRegressor', 'BaggingClassifier', 'BaggingRegressor', 'GradientBoostingRegressor', 'LogisticRegression', 'LogisticRegressionCV', 'LinearSVC', 'LinearSVC', 'MultinomialNB', 'CalibratedClassifierCV', 'SGDClassifier', 'SGDRegressor', 'Perceptron', 'RidgeClassifierCV', 'RidgeCV', 'RandomForestClassifier', 'RandomForestRegressor'
RidgeCV is being fixed in #4490
CalibratedClassifierCV is being discussed in #5518

Tests are now passing. Please take a look at the exclusion lists and see if you are OK with that

@eickenberg
Copy link
Contributor

Problems with Travis:

  File "/home/travis/build/scikit-learn/scikit-learn/sklearn/tests/test_common.py", line 232, in test_get_params_invariance

    yield check_transformer_n_iter, name, estimator

NameError: global name 'estimator' is not defined

======================================================================

FAIL: sklearn.tests.test_common.test_sample_weight_consistency(array([1, 1, 0, 1, 1]), array([1, 1, 0, 1, 0]), 6, 'AdaBoostClassifier prediction not equal')

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/home/travis/build/scikit-learn/scikit-learn/testvenv/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest

    self.test(*self.arg)

  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 800, in assert_array_almost_equal

    header=('Arrays are not almost equal to %d decimals' % decimal))

  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 636, in assert_array_compare

    raise AssertionError(msg)

AssertionError: 

Arrays are not almost equal to 6 decimals

AdaBoostClassifier prediction not equal

(mismatch 20.0%)

 x: array([1, 1, 0, 1, 1])

 y: array([1, 1, 0, 1, 0])

>>  raise AssertionError('\nArrays are not almost equal to 6 decimals\nAdaBoostClassifier prediction not equal\n(mismatch 20.0%)\n x: array([1, 1, 0, 1, 1])\n y: array([1, 1, 0, 1, 0])')



======================================================================

FAIL: sklearn.tests.test_common.test_sample_weight_consistency(array([  3.35668635,   2.73091015,  13.93340473, -16.09503755,  -2.30172005]), array([  3.35668635,   2.73091015,  13.93340473, -15.33777767,  -2.30172005]), 6, 'ExtraTreesRegressor prediction not equal')

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/home/travis/build/scikit-learn/scikit-learn/testvenv/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest

    self.test(*self.arg)

  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 800, in assert_array_almost_equal

    header=('Arrays are not almost equal to %d decimals' % decimal))

  File "/usr/lib/python2.7/dist-packages/numpy/testing/utils.py", line 636, in assert_array_compare

    raise AssertionError(msg)

AssertionError: 

Arrays are not almost equal to 6 decimals

ExtraTreesRegressor prediction not equal

(mismatch 20.0%)

 x: array([  3.35668635,   2.73091015,  13.93340473, -16.09503755,  -2.30172005])

 y: array([  3.35668635,   2.73091015,  13.93340473, -15.33777767,  -2.30172005])

>>  raise AssertionError('\nArrays are not almost equal to 6 decimals\nExtraTreesRegressor prediction not equal\n(mismatch 20.0%)\n x: array([  3.35668635,   2.73091015,  13.93340473, -16.09503755,  -2.30172005])\n y: array([  3.35668635,   2.73091015,  13.93340473, -15.33777767,  -2.30172005])')

different errors on different machines I guess.
Also somehow we are causing a problem in test_get_params_invariance

@ainafp
Copy link
Author

ainafp commented Oct 22, 2015

Do I exclude them too?

test_get_params_invariance has a typo

n_samples, n_features = 20, 5
rng = check_random_state(random_state)

sample_weight = (rng.permutation(n_samples) < (n_samples / 2.)) * 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you make this casting more explicit, maybe with astype(np.int) ?

@giorgiop
Copy link
Contributor

Is there any way to cover the case of float (positive) weights?

@eickenberg
Copy link
Contributor

I guess one could work with rationals and then use the common denominator.
But the int case is an easy correspondence point to sample count and should
already catch the biggest problems, right?

On Thu, Oct 22, 2015 at 3:07 PM, Giorgio Patrini notifications@github.com
wrote:

Is there any way to cover the case of float (positive) weights?


Reply to this email directly or view it on GitHub
#5515 (comment)
.

set_random_state(estimator_sw, random_state=random_state)
estimator_sw.fit(X[train], y[train], sample_weight=sample_weight[train])
X_aug_train, y_aug_train = aug((X[train], y[train]),
sample_weight[train])
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think a test for aug would make sense here? Something like

assert_equal(X_aug_train.shape[0], np.sum(sample_weight[train]))

@giorgiop
Copy link
Contributor

@eickenberg Right, but that would fall on an implementation with integer numbers again. I agree that the tests with integers will uncover the biggest issues. However, it's true that most if not all the algorithms can take positive real number as weights.

@ainafp ainafp changed the title Sample weight consistency [MRG] Sample weight consistency Oct 22, 2015
@ainafp ainafp changed the title [MRG] Sample weight consistency [WIP] Sample weight consistency Oct 22, 2015
n_samples, n_features = 20, 5
rng = check_random_state(random_state)

sample_weight = (rng.permutation(n_samples) < (n_samples / 2.)).astype(np.float)
Copy link
Contributor

Choose a reason for hiding this comment

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

I was not suggesting to do that. I think int makes more sense in this case.
My argument is that your tests will not cover the situation in which learning algorithms are weighted with float sample weights, as there is not a corresponding interpretation of "weight = number of sample copy". I simply do not think there is a way to cover this situation though.

Copy link
Member

Choose a reason for hiding this comment

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

well, you could have a dataset with some duplicate samples and use sample-weight 0.5 on it and compare against the data without duplicates? Or use 1.5 and compare it with data that has triples?

@ainafp
Copy link
Author

ainafp commented Oct 23, 2015

ok I didn't understand it then.

Would it make sense to multiply the weights by the number of decimals used and then do the same (augment data)? Is this what you mean? It doesn't change much though.

@giorgiop
Copy link
Contributor

I think solving weighting with rescaling (the multiplication by the weights) is only appropriate with linear models.

@ainafp
Copy link
Author

ainafp commented Oct 23, 2015

What do you suggest then?

@giorgiop
Copy link
Contributor

Your idea is the way to go with this test, I would not do anything else :)

@ainafp
Copy link
Author

ainafp commented Oct 26, 2015

@ogrisel do you know why travis doesn't pass with python 3 but it does with python 2?

@giorgiop
Copy link
Contributor

We were discussing in #5526 (comment) the addition of another test for sample_weight relative to linear models. The test is supposed to work under the conditions:

  • linear models fitted by square loss
  • regularization coefficient is 0 (if possibile) or very close to 0
  • X input matrix must describe an overdetermined linear system, i.e. X.shape[0] > X.shape[1]

The code should look like this one, looping over the appropriate regressors and testing both dense and sparse inputs.

@DiegoVergara
Copy link

How does it work sample weight in MultinomialNB ?, Is there any documentation or equation?

Someone can help me please, I appreciate it!

@amueller
Copy link
Member

I think the issue is the same I saw in #7618. There should be an AttributeError, not a ValueError. That's a bug in the SVC.

@amueller
Copy link
Member

Feel free to fix this here or do a separate PR.

@amueller amueller added the Bug label Dec 7, 2016
@amueller amueller added this to the 0.19 milestone Dec 7, 2016
@amueller
Copy link
Member

amueller commented Dec 7, 2016

fixes #5367.

@amueller
Copy link
Member

amueller commented Dec 7, 2016

Sorry I'm a bit out of the loop with this one. What's the status? Can you rebase?

@jnothman
Copy link
Member

Hmm... I'd forgotten this was here. Should this be closed given #11558, @sergulaydore? I've not yet checked if it's completely redundant.

1 similar comment
@jnothman
Copy link
Member

Hmm... I'd forgotten this was here. Should this be closed given #11558, @sergulaydore? I've not yet checked if it's completely redundant.

@sergulaydore
Copy link
Contributor

I think this is more relevant to #11598 which is not reviewed yet and also #9926.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants