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

Code before:
            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 = 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

Review comment (suggestion):
```suggestion
        centers=n_classes,
        cluster_std=[1.0, 3.0],
```

to make the test more interesting for a QDA model.

Code after:
    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(
            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}",
        )