Prompt48 commited on
Commit
09fc2b2
·
verified ·
1 Parent(s): 8f03048

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\sklearn\decomposition\tests\test_factor_analysis.py with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//sklearn//decomposition//tests//test_factor_analysis.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Authors: The scikit-learn developers
2
+ # SPDX-License-Identifier: BSD-3-Clause
3
+
4
+ from itertools import combinations
5
+
6
+ import numpy as np
7
+ import pytest
8
+
9
+ from sklearn.decomposition import FactorAnalysis
10
+ from sklearn.decomposition._factor_analysis import _ortho_rotation
11
+ from sklearn.exceptions import ConvergenceWarning
12
+ from sklearn.utils._testing import assert_almost_equal, assert_array_almost_equal
13
+
14
+
15
+ def test_factor_analysis(global_random_seed):
16
+ # Test FactorAnalysis ability to recover the data covariance structure
17
+ rng = np.random.RandomState(global_random_seed)
18
+ n_samples, n_features, n_components = 20, 5, 3
19
+
20
+ # Some random settings for the generative model
21
+ W = rng.randn(n_components, n_features)
22
+ # latent variable of dim 3, 20 of it
23
+ h = rng.randn(n_samples, n_components)
24
+ # using gamma to model different noise variance
25
+ # per component
26
+ noise = rng.gamma(1, size=n_features) * rng.randn(n_samples, n_features)
27
+
28
+ # generate observations
29
+ # wlog, mean is 0
30
+ X = np.dot(h, W) + noise
31
+
32
+ fas = []
33
+ for method in ["randomized", "lapack"]:
34
+ fa = FactorAnalysis(n_components=n_components, svd_method=method)
35
+ fa.fit(X)
36
+ fas.append(fa)
37
+
38
+ X_t = fa.transform(X)
39
+ assert X_t.shape == (n_samples, n_components)
40
+
41
+ assert_almost_equal(fa.loglike_[-1], fa.score_samples(X).sum())
42
+ assert_almost_equal(fa.score_samples(X).mean(), fa.score(X))
43
+
44
+ diff = np.all(np.diff(fa.loglike_))
45
+ assert diff > 0.0, "Log likelihood dif not increase"
46
+
47
+ # Sample Covariance
48
+ scov = np.cov(X, rowvar=0.0, bias=1.0)
49
+
50
+ # Model Covariance
51
+ mcov = fa.get_covariance()
52
+ diff = np.sum(np.abs(scov - mcov)) / W.size
53
+ assert diff < 0.2, "Mean absolute difference is %f" % diff
54
+ fa = FactorAnalysis(
55
+ n_components=n_components, noise_variance_init=np.ones(n_features)
56
+ )
57
+ with pytest.raises(ValueError):
58
+ fa.fit(X[:, :2])
59
+
60
+ def f(x, y):
61
+ return np.abs(getattr(x, y)) # sign will not be equal
62
+
63
+ fa1, fa2 = fas
64
+ for attr in ["loglike_", "components_", "noise_variance_"]:
65
+ assert_almost_equal(f(fa1, attr), f(fa2, attr))
66
+
67
+ fa1.max_iter = 1
68
+ fa1.verbose = True
69
+ with pytest.warns(ConvergenceWarning):
70
+ fa1.fit(X)
71
+
72
+ # Test get_covariance and get_precision with n_components == n_features
73
+ # with n_components < n_features and with n_components == 0
74
+ for n_components in [0, 2, X.shape[1]]:
75
+ fa.n_components = n_components
76
+ fa.fit(X)
77
+ cov = fa.get_covariance()
78
+ precision = fa.get_precision()
79
+ assert_array_almost_equal(np.dot(cov, precision), np.eye(X.shape[1]), 12)
80
+
81
+ # test rotation
82
+ n_components = 2
83
+
84
+ results, projections = {}, {}
85
+ for method in (None, "varimax", "quartimax"):
86
+ fa_var = FactorAnalysis(n_components=n_components, rotation=method)
87
+ results[method] = fa_var.fit_transform(X)
88
+ projections[method] = fa_var.get_covariance()
89
+ for rot1, rot2 in combinations([None, "varimax", "quartimax"], 2):
90
+ assert not np.allclose(results[rot1], results[rot2])
91
+ assert np.allclose(projections[rot1], projections[rot2], atol=3)
92
+
93
+ # test against R's psych::principal with rotate="varimax"
94
+ # (i.e., the values below stem from rotating the components in R)
95
+ # R's factor analysis returns quite different values; therefore, we only
96
+ # test the rotation itself
97
+ factors = np.array(
98
+ [
99
+ [0.89421016, -0.35854928, -0.27770122, 0.03773647],
100
+ [-0.45081822, -0.89132754, 0.0932195, -0.01787973],
101
+ [0.99500666, -0.02031465, 0.05426497, -0.11539407],
102
+ [0.96822861, -0.06299656, 0.24411001, 0.07540887],
103
+ ]
104
+ )
105
+ r_solution = np.array(
106
+ [[0.962, 0.052], [-0.141, 0.989], [0.949, -0.300], [0.937, -0.251]]
107
+ )
108
+ rotated = _ortho_rotation(factors[:, :n_components], method="varimax").T
109
+ assert_array_almost_equal(np.abs(rotated), np.abs(r_solution), decimal=3)