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

Code before:
    assert_allclose(c1.covariance_, c2.covariance_)


def test_qda_coefs(global_random_seed):
    # Test if the coefficients of the solvers are approximately the same.
    n_features = 2
    n_classes = 2
    n_samples = 1000
    X, y = make_blobs(
        n_samples=n_samples,
        n_features=n_features,
        centers=n_classes,
        random_state=global_random_seed,
    )

    clf_svd = QuadraticDiscriminantAnalysis(solver="svd")
    clf_eigen = QuadraticDiscriminantAnalysis(solver="eigen")

    clf_svd.fit(X, y)
    clf_eigen.fit(X, y)

    for i in range(n_classes):
        assert_array_almost_equal(
            np.abs(clf_svd.rotations_[i]), np.abs(clf_eigen.rotations_[i]), 1
        )
        assert_array_almost_equal(clf_svd.scalings_[i], clf_eigen.scalings_[i], 1)


def test_qda_priors():
    clf = QuadraticDiscriminantAnalysis()
    y_pred = clf.fit(X6, y6).predict(X6)
    n_pos = np.sum(y_pred == 2)

    neg = 1e-10
    clf = QuadraticDiscriminantAnalysis(priors=np.array([neg, 1 - neg]))
    y_pred = clf.fit(X6, y6).predict(X6)
    n_pos2 = np.sum(y_pred == 2)

    assert n_pos2 > n_pos


@pytest.mark.parametrize("priors_type", ["list", "tuple", "array"])
def test_qda_prior_type(priors_type):
    """Check that priors accept array-like."""
    priors = [0.5, 0.5]
    clf = QuadraticDiscriminantAnalysis(
        priors=_convert_container([0.5, 0.5], priors_type)
    ).fit(X6, y6)
    assert isinstance(clf.priors_, np.ndarray)
    assert_array_equal(clf.priors_, priors)


Review comment (suggestion):
Let's use `assert_allclose` with explicit `rtol` / `atol` values for such new tests instead of using `1` decimal. `1` decimal seems much too loose for dataset of this shape.

Don't forget to push a commit `[all random seeds]` followed by the test name `test_qda_coefs` to check that the test still pass for all seeds on all platforms after the change.

Code after:
def test_qda_coefs(global_random_seed):
    # Test if the coefficients of the solvers are approximately the same.
    n_features = 2
    n_classes = 2
    n_samples = 3000
    X, y = make_blobs(
        n_samples=n_samples,
        n_features=n_features,
        centers=n_classes,
        cluster_std=[1.0, 3.0],
        random_state=global_random_seed,
    )

    clf_svd = QuadraticDiscriminantAnalysis(solver="svd")
    clf_eigen = QuadraticDiscriminantAnalysis(solver="eigen")

    clf_svd.fit(X, y)
    clf_eigen.fit(X, y)

    for class_idx in range(n_classes):
        assert_allclose(
            np.abs(clf_svd.rotations_[class_idx]),
            np.abs(clf_eigen.rotations_[class_idx]),
            rtol=1e-3,
            err_msg=f"SVD and Eigen rotations differ for class {class_idx}",
        )
        assert_allclose(
            clf_svd.scalings_[class_idx],
            clf_eigen.scalings_[class_idx],
            rtol=1e-3,
            err_msg=f"SVD and Eigen scalings differ for class {class_idx}",
        )


def test_qda_priors():
    clf = QuadraticDiscriminantAnalysis()
    y_pred = clf.fit(X6, y6).predict(X6)
    n_pos = np.sum(y_pred == 2)

    neg = 1e-10
    clf = QuadraticDiscriminantAnalysis(priors=np.array([neg, 1 - neg]))
    y_pred = clf.fit(X6, y6).predict(X6)
    n_pos2 = np.sum(y_pred == 2)

    assert n_pos2 > n_pos


@pytest.mark.parametrize("priors_type", ["list", "tuple", "array"])
def test_qda_prior_type(priors_type):
    """Check that priors accept array-like."""
    priors = [0.5, 0.5]