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

Code before:
    with pytest.raises(ValueError):
        clf.fit(X, y)


def test_qda_ledoitwolf():
    # When shrinkage="auto" current implementation uses ledoitwolf estimation
    # of covariance after standardizing the data. This checks that it is indeed
    # the case
    class StandardizedLedoitWolf:
        def fit(self, X):
            sc = StandardScaler()  # standardize features
            X_sc = sc.fit_transform(X)
            s = ledoit_wolf(X_sc)[0]
            # rescale
            s = sc.scale_[:, np.newaxis] * s * sc.scale_[np.newaxis, :]
            self.covariance_ = s

    rng = np.random.RandomState(0)
    X = rng.rand(100, 10)
    y = rng.randint(3, size=(100,))
    c1 = QuadraticDiscriminantAnalysis(
        store_covariance=True, shrinkage="auto", solver="eigen"
    )
    c2 = QuadraticDiscriminantAnalysis(
        store_covariance=True,
        covariance_estimator=StandardizedLedoitWolf(),
        solver="eigen",
    )
    c1.fit(X, y)
    c2.fit(X, y)
    assert_allclose(c1.means_, c2.means_)
    assert_allclose(c1.covariance_, c2.covariance_)


def test_qda_coefs():
    # 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=11
    )

    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(

Review comment (bug):
This is raising `RuntimeWarning: divide by zero encountered in dot`. You can investigate it with `pytest sklearn/tests/test_discriminant_analysis.py -k test_qda_ledoitwolf  -vl -Werror::RuntimeWarning`. It would be good to find why and either:

- update the test data to avoid triggering a warning,
- and/or open an issue about it and work on a fix in a dedicated PR if this is a bug fundamentally unrelated to the changes of this PR.

Code after:
def test_qda_ledoitwolf(global_random_seed):
    # When shrinkage="auto" current implementation uses ledoitwolf estimation
    # of covariance after standardizing the data. This checks that it is indeed
    # the case
    class StandardizedLedoitWolf:
        def fit(self, X):
            sc = StandardScaler()  # standardize features
            X_sc = sc.fit_transform(X)
            s = ledoit_wolf(X_sc)[0]
            # rescale
            s = sc.scale_[:, np.newaxis] * s * sc.scale_[np.newaxis, :]
            self.covariance_ = s

    rng = np.random.RandomState(global_random_seed)
    X = rng.rand(100, 10)
    y = rng.randint(3, size=(100,))
    c1 = QuadraticDiscriminantAnalysis(
        store_covariance=True, shrinkage="auto", solver="eigen"
    )
    c2 = QuadraticDiscriminantAnalysis(
        store_covariance=True,
        covariance_estimator=StandardizedLedoitWolf(),
        solver="eigen",
    )
    c1.fit(X, y)
    c2.fit(X, y)
    assert_allclose(c1.means_, c2.means_)
    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 = 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(