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

Code before:
    # test predicting without fitting
    with pytest.raises(NotFittedError):
        ovr.predict([])

    # Fail on multioutput data
    msg = "Multioutput target data is not supported with label binarization"
    with pytest.raises(ValueError, match=msg):
        X = np.array([[1, 0], [0, 1]])
        y = np.array([[1, 2], [3, 1]])
        OneVsRestClassifier(MultinomialNB()).fit(X, y)

    with pytest.raises(ValueError, match=msg):
        X = np.array([[1, 0], [0, 1]])
        y = np.array([[1.5, 2.4], [3.1, 0.8]])
        OneVsRestClassifier(MultinomialNB()).fit(X, y)


def test_check_classification_targets():
    # Test that check_classification_target return correct type. #5782
    y = np.array([0.0, 1.1, 2.0, 3.0])
    msg = type_of_target(y)
    with pytest.raises(ValueError, match=msg):
        check_classification_targets(y)


def test_dir():
    # Test that __dir__ includes only relevant attributes. #28558
    svc = svm.SVC()
    assert "predict_proba" not in dir(svc)

    le = LabelEncoder()
    assert "set_output" not in dir(le)


def test_ovr_ties():
    """Check that ties-breaking matches np.argmax behavior

    Non-regression test for issue #14124
    """

    class Dummy(BaseEstimator):
        def fit(self, X, y):
            return self

        def decision_function(self, X):
            return np.zeros(len(X))

    X = np.array([[0], [0], [0], [0]])
    y = np.array([0, 1, 2, 3])
    clf = OneVsRestClassifier(Dummy()).fit(X, y)
    assert_array_equal(clf.predict(X), np.argmax(clf.decision_function(X), axis=1))

Review comment (suggestion):
A suggestion for a more explicit name that tries to capture what the test is meant to do, I don't love it but maybe it inspires a good name. I think `test_dir` is a bit too generic (the ideal would be that we don't need the comment on L87 because of the name. Might not be achievable.)
```suggestion
def test_conditional_attrs_not_in_dir():
```

Code after:
    # test predicting without fitting
    with pytest.raises(NotFittedError):
        ovr.predict([])

    # Fail on multioutput data
    msg = "Multioutput target data is not supported with label binarization"
    with pytest.raises(ValueError, match=msg):
        X = np.array([[1, 0], [0, 1]])
        y = np.array([[1, 2], [3, 1]])
        OneVsRestClassifier(MultinomialNB()).fit(X, y)

    with pytest.raises(ValueError, match=msg):
        X = np.array([[1, 0], [0, 1]])
        y = np.array([[1.5, 2.4], [3.1, 0.8]])
        OneVsRestClassifier(MultinomialNB()).fit(X, y)


def test_check_classification_targets():
    # Test that check_classification_target return correct type. #5782
    y = np.array([0.0, 1.1, 2.0, 3.0])
    msg = type_of_target(y)
    with pytest.raises(ValueError, match=msg):
        check_classification_targets(y)


def test_conditional_attrs_not_in_dir():
    # Test that __dir__ includes only relevant attributes. #28558

    encoder = LabelEncoder()
    assert "set_output" not in dir(encoder)

    scalar = StandardScaler()
    assert "set_output" in dir(scalar)

    svc = SVC(probability=False)
    assert "predict_proba" not in dir(svc)

    svc.probability = True
    assert "predict_proba" in dir(svc)


def test_ovr_ties():
    """Check that ties-breaking matches np.argmax behavior

    Non-regression test for issue #14124
    """

    class Dummy(BaseEstimator):
        def fit(self, X, y):
            return self