Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\sklearn\decomposition\tests\test_truncated_svd.py with huggingface_hub
Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//sklearn//decomposition//tests//test_truncated_svd.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test truncated SVD transformer."""
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
import scipy.sparse as sp
|
| 6 |
+
|
| 7 |
+
from sklearn.decomposition import PCA, TruncatedSVD
|
| 8 |
+
from sklearn.utils import check_random_state
|
| 9 |
+
from sklearn.utils._testing import assert_allclose, assert_array_less
|
| 10 |
+
|
| 11 |
+
SVD_SOLVERS = ["arpack", "randomized"]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@pytest.fixture(scope="module")
|
| 15 |
+
def X_sparse():
|
| 16 |
+
# Make an X that looks somewhat like a small tf-idf matrix.
|
| 17 |
+
rng = check_random_state(42)
|
| 18 |
+
X = sp.random(60, 55, density=0.2, format="csr", random_state=rng)
|
| 19 |
+
X.data[:] = 1 + np.log(X.data)
|
| 20 |
+
return X
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@pytest.mark.parametrize("solver", ["randomized"])
|
| 24 |
+
@pytest.mark.parametrize("kind", ("dense", "sparse"))
|
| 25 |
+
def test_solvers(X_sparse, solver, kind):
|
| 26 |
+
X = X_sparse if kind == "sparse" else X_sparse.toarray()
|
| 27 |
+
svd_a = TruncatedSVD(30, algorithm="arpack")
|
| 28 |
+
svd = TruncatedSVD(30, algorithm=solver, random_state=42, n_oversamples=100)
|
| 29 |
+
|
| 30 |
+
Xa = svd_a.fit_transform(X)[:, :6]
|
| 31 |
+
Xr = svd.fit_transform(X)[:, :6]
|
| 32 |
+
assert_allclose(Xa, Xr, rtol=2e-3)
|
| 33 |
+
|
| 34 |
+
comp_a = np.abs(svd_a.components_)
|
| 35 |
+
comp = np.abs(svd.components_)
|
| 36 |
+
# All elements are equal, but some elements are more equal than others.
|
| 37 |
+
assert_allclose(comp_a[:9], comp[:9], rtol=1e-3)
|
| 38 |
+
assert_allclose(comp_a[9:], comp[9:], atol=1e-2)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@pytest.mark.parametrize("n_components", (10, 25, 41, 55))
|
| 42 |
+
def test_attributes(n_components, X_sparse):
|
| 43 |
+
n_features = X_sparse.shape[1]
|
| 44 |
+
tsvd = TruncatedSVD(n_components).fit(X_sparse)
|
| 45 |
+
assert tsvd.n_components == n_components
|
| 46 |
+
assert tsvd.components_.shape == (n_components, n_features)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@pytest.mark.parametrize(
|
| 50 |
+
"algorithm, n_components",
|
| 51 |
+
[
|
| 52 |
+
("arpack", 55),
|
| 53 |
+
("arpack", 56),
|
| 54 |
+
("randomized", 56),
|
| 55 |
+
],
|
| 56 |
+
)
|
| 57 |
+
def test_too_many_components(X_sparse, algorithm, n_components):
|
| 58 |
+
tsvd = TruncatedSVD(n_components=n_components, algorithm=algorithm)
|
| 59 |
+
with pytest.raises(ValueError):
|
| 60 |
+
tsvd.fit(X_sparse)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@pytest.mark.parametrize("fmt", ("array", "csr", "csc", "coo", "lil"))
|
| 64 |
+
def test_sparse_formats(fmt, X_sparse):
|
| 65 |
+
n_samples = X_sparse.shape[0]
|
| 66 |
+
Xfmt = X_sparse.toarray() if fmt == "dense" else getattr(X_sparse, "to" + fmt)()
|
| 67 |
+
tsvd = TruncatedSVD(n_components=11)
|
| 68 |
+
Xtrans = tsvd.fit_transform(Xfmt)
|
| 69 |
+
assert Xtrans.shape == (n_samples, 11)
|
| 70 |
+
Xtrans = tsvd.transform(Xfmt)
|
| 71 |
+
assert Xtrans.shape == (n_samples, 11)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@pytest.mark.parametrize("algo", SVD_SOLVERS)
|
| 75 |
+
def test_inverse_transform(algo, X_sparse):
|
| 76 |
+
# We need a lot of components for the reconstruction to be "almost
|
| 77 |
+
# equal" in all positions. XXX Test means or sums instead?
|
| 78 |
+
tsvd = TruncatedSVD(n_components=52, random_state=42, algorithm=algo)
|
| 79 |
+
Xt = tsvd.fit_transform(X_sparse)
|
| 80 |
+
Xinv = tsvd.inverse_transform(Xt)
|
| 81 |
+
assert_allclose(Xinv, X_sparse.toarray(), rtol=1e-1, atol=2e-1)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_integers(X_sparse):
|
| 85 |
+
n_samples = X_sparse.shape[0]
|
| 86 |
+
Xint = X_sparse.astype(np.int64)
|
| 87 |
+
tsvd = TruncatedSVD(n_components=6)
|
| 88 |
+
Xtrans = tsvd.fit_transform(Xint)
|
| 89 |
+
assert Xtrans.shape == (n_samples, tsvd.n_components)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@pytest.mark.parametrize("kind", ("dense", "sparse"))
|
| 93 |
+
@pytest.mark.parametrize("n_components", [10, 20])
|
| 94 |
+
@pytest.mark.parametrize("solver", SVD_SOLVERS)
|
| 95 |
+
def test_explained_variance(X_sparse, kind, n_components, solver):
|
| 96 |
+
X = X_sparse if kind == "sparse" else X_sparse.toarray()
|
| 97 |
+
svd = TruncatedSVD(n_components, algorithm=solver)
|
| 98 |
+
X_tr = svd.fit_transform(X)
|
| 99 |
+
# Assert that all the values are greater than 0
|
| 100 |
+
assert_array_less(0.0, svd.explained_variance_ratio_)
|
| 101 |
+
|
| 102 |
+
# Assert that total explained variance is less than 1
|
| 103 |
+
assert_array_less(svd.explained_variance_ratio_.sum(), 1.0)
|
| 104 |
+
|
| 105 |
+
# Test that explained_variance is correct
|
| 106 |
+
total_variance = np.var(X_sparse.toarray(), axis=0).sum()
|
| 107 |
+
variances = np.var(X_tr, axis=0)
|
| 108 |
+
true_explained_variance_ratio = variances / total_variance
|
| 109 |
+
|
| 110 |
+
assert_allclose(
|
| 111 |
+
svd.explained_variance_ratio_,
|
| 112 |
+
true_explained_variance_ratio,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
@pytest.mark.parametrize("kind", ("dense", "sparse"))
|
| 117 |
+
@pytest.mark.parametrize("solver", SVD_SOLVERS)
|
| 118 |
+
def test_explained_variance_components_10_20(X_sparse, kind, solver):
|
| 119 |
+
X = X_sparse if kind == "sparse" else X_sparse.toarray()
|
| 120 |
+
svd_10 = TruncatedSVD(10, algorithm=solver, n_iter=10).fit(X)
|
| 121 |
+
svd_20 = TruncatedSVD(20, algorithm=solver, n_iter=10).fit(X)
|
| 122 |
+
|
| 123 |
+
# Assert the 1st component is equal
|
| 124 |
+
assert_allclose(
|
| 125 |
+
svd_10.explained_variance_ratio_,
|
| 126 |
+
svd_20.explained_variance_ratio_[:10],
|
| 127 |
+
rtol=5e-3,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# Assert that 20 components has higher explained variance than 10
|
| 131 |
+
assert (
|
| 132 |
+
svd_20.explained_variance_ratio_.sum() > svd_10.explained_variance_ratio_.sum()
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
@pytest.mark.parametrize("solver", SVD_SOLVERS)
|
| 137 |
+
def test_singular_values_consistency(solver, global_random_seed):
|
| 138 |
+
# Check that the TruncatedSVD output has the correct singular values
|
| 139 |
+
rng = np.random.RandomState(global_random_seed)
|
| 140 |
+
n_samples, n_features = 100, 80
|
| 141 |
+
X = rng.randn(n_samples, n_features)
|
| 142 |
+
|
| 143 |
+
pca = TruncatedSVD(n_components=2, algorithm=solver, random_state=rng).fit(X)
|
| 144 |
+
|
| 145 |
+
# Compare to the Frobenius norm
|
| 146 |
+
X_pca = pca.transform(X)
|
| 147 |
+
assert_allclose(
|
| 148 |
+
np.sum(pca.singular_values_**2.0),
|
| 149 |
+
np.linalg.norm(X_pca, "fro") ** 2.0,
|
| 150 |
+
rtol=1e-2,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Compare to the 2-norms of the score vectors
|
| 154 |
+
assert_allclose(
|
| 155 |
+
pca.singular_values_, np.sqrt(np.sum(X_pca**2.0, axis=0)), rtol=1e-2
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
@pytest.mark.parametrize("solver", SVD_SOLVERS)
|
| 160 |
+
def test_singular_values_expected(solver, global_random_seed):
|
| 161 |
+
# Set the singular values and see what we get back
|
| 162 |
+
rng = np.random.RandomState(global_random_seed)
|
| 163 |
+
n_samples = 100
|
| 164 |
+
n_features = 110
|
| 165 |
+
|
| 166 |
+
X = rng.randn(n_samples, n_features)
|
| 167 |
+
|
| 168 |
+
pca = TruncatedSVD(n_components=3, algorithm=solver, random_state=rng)
|
| 169 |
+
X_pca = pca.fit_transform(X)
|
| 170 |
+
|
| 171 |
+
X_pca /= np.sqrt(np.sum(X_pca**2.0, axis=0))
|
| 172 |
+
X_pca[:, 0] *= 3.142
|
| 173 |
+
X_pca[:, 1] *= 2.718
|
| 174 |
+
|
| 175 |
+
X_hat_pca = np.dot(X_pca, pca.components_)
|
| 176 |
+
pca.fit(X_hat_pca)
|
| 177 |
+
assert_allclose(pca.singular_values_, [3.142, 2.718, 1.0], rtol=1e-14)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def test_truncated_svd_eq_pca(X_sparse):
|
| 181 |
+
# TruncatedSVD should be equal to PCA on centered data
|
| 182 |
+
|
| 183 |
+
X_dense = X_sparse.toarray()
|
| 184 |
+
|
| 185 |
+
X_c = X_dense - X_dense.mean(axis=0)
|
| 186 |
+
|
| 187 |
+
params = dict(n_components=10, random_state=42)
|
| 188 |
+
|
| 189 |
+
svd = TruncatedSVD(algorithm="arpack", **params)
|
| 190 |
+
pca = PCA(svd_solver="arpack", **params)
|
| 191 |
+
|
| 192 |
+
Xt_svd = svd.fit_transform(X_c)
|
| 193 |
+
Xt_pca = pca.fit_transform(X_c)
|
| 194 |
+
|
| 195 |
+
assert_allclose(Xt_svd, Xt_pca, rtol=1e-9)
|
| 196 |
+
assert_allclose(pca.mean_, 0, atol=1e-9)
|
| 197 |
+
assert_allclose(svd.components_, pca.components_)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
@pytest.mark.parametrize(
|
| 201 |
+
"algorithm, tol", [("randomized", 0.0), ("arpack", 1e-6), ("arpack", 0.0)]
|
| 202 |
+
)
|
| 203 |
+
@pytest.mark.parametrize("kind", ("dense", "sparse"))
|
| 204 |
+
def test_fit_transform(X_sparse, algorithm, tol, kind):
|
| 205 |
+
# fit_transform(X) should equal fit(X).transform(X)
|
| 206 |
+
X = X_sparse if kind == "sparse" else X_sparse.toarray()
|
| 207 |
+
svd = TruncatedSVD(
|
| 208 |
+
n_components=5, n_iter=7, random_state=42, algorithm=algorithm, tol=tol
|
| 209 |
+
)
|
| 210 |
+
X_transformed_1 = svd.fit_transform(X)
|
| 211 |
+
X_transformed_2 = svd.fit(X).transform(X)
|
| 212 |
+
assert_allclose(X_transformed_1, X_transformed_2)
|