Repository: scikit-learn/scikit-learn
File: sklearn/linear_model/tests/test_base.py (Python)

Code before:
    assert_allclose(reg.coef_, coef_0, rtol=1e-5)
    if fit_intercept:
        assert_allclose(reg.intercept_, intercept_0)

    # 5) check that multiplying sample_weight by 2 is equivalent to repeating
    # corresponding samples twice
    if sparse_container is not None:
        X2 = sparse.vstack([X, X[: n_samples // 2]], format="csc")
    else:
        X2 = np.concatenate([X, X[: n_samples // 2]], axis=0)
    y2 = np.concatenate([y, y[: n_samples // 2]])
    sample_weight_1 = sample_weight.copy()
    sample_weight_1[: n_samples // 2] *= 2
    sample_weight_2 = np.concatenate(
        [sample_weight, sample_weight[: n_samples // 2]], axis=0
    )

    reg1 = LinearRegression(**params).fit(X, y, sample_weight=sample_weight_1)
    reg2 = LinearRegression(**params).fit(X2, y2, sample_weight=sample_weight_2)
    assert_allclose(reg1.coef_, reg2.coef_, rtol=1e-6)
    if fit_intercept:
        assert_allclose(reg1.intercept_, reg2.intercept_)


def test_predict_proba_lr_large_values():
    """Test that _predict_proba_lr of LinearClassifierMixin deals with large values.

    Note that exp(-1000) = 0.
    """

    class MockClassifier(LinearClassifierMixin, BaseEstimator):
        def __init__(self):
            pass

        def fit(self, X, y):
            self.__sklearn_is_fitted__ = True

        def decision_function(self, X):
            n_samples = X.shape[0]
            return np.tile([-1000.0] * 4, [n_samples, 1])

    clf = MockClassifier()
    clf.fit(X=None, y=None)

    proba = clf._predict_proba_lr(np.ones(5))
    assert_allclose(np.sum(proba, axis=1), 1)


Review comment (suggestion):
```suggestion
    """Test that _predict_proba_lr of LinearClassifierMixin deals with large
    negative values.
```

Code after:
    assert_allclose(reg.coef_, coef_0, rtol=1e-5)
    if fit_intercept:
        assert_allclose(reg.intercept_, intercept_0)

    # 5) check that multiplying sample_weight by 2 is equivalent to repeating
    # corresponding samples twice
    if sparse_container is not None:
        X2 = sparse.vstack([X, X[: n_samples // 2]], format="csc")
    else:
        X2 = np.concatenate([X, X[: n_samples // 2]], axis=0)
    y2 = np.concatenate([y, y[: n_samples // 2]])
    sample_weight_1 = sample_weight.copy()
    sample_weight_1[: n_samples // 2] *= 2
    sample_weight_2 = np.concatenate(
        [sample_weight, sample_weight[: n_samples // 2]], axis=0
    )

    reg1 = LinearRegression(**params).fit(X, y, sample_weight=sample_weight_1)
    reg2 = LinearRegression(**params).fit(X2, y2, sample_weight=sample_weight_2)
    assert_allclose(reg1.coef_, reg2.coef_, rtol=1e-6)
    if fit_intercept:
        assert_allclose(reg1.intercept_, reg2.intercept_)


def test_predict_proba_lr_large_values():
    """Test that _predict_proba_lr of LinearClassifierMixin deals with large
    negative values.

    Note that exp(-1000) = 0.
    """

    class MockClassifier(LinearClassifierMixin, BaseEstimator):
        def __init__(self):
            pass

        def fit(self, X, y):
            self.__sklearn_is_fitted__ = True

        def decision_function(self, X):
            n_samples = X.shape[0]
            return np.tile([-1000.0] * 4, [n_samples, 1])

    clf = MockClassifier()
    clf.fit(X=None, y=None)

    proba = clf._predict_proba_lr(np.ones(5))
    assert_allclose(np.sum(proba, axis=1), 1)