Skip to content

Commit 037d836

Browse files
Update PCA doctests for Nx 0.13's eigh fix
Verified against sklearn on the iris dataset (fit, incremental_fit, partial_fit, transform) and against a numpy SVD reference (fit_transform, n=6). The n=6 case is the clearest evidence this is a real precision fix, not just reformatting: the old second-component value was off by ~0.045 from the numpy reference, the new one matches to ~1e-6. Small-sample PCA is exactly the ill-conditioned regime the batched eigh bug (found while working on QDA, #349) would be expected to hurt most.
1 parent 2342a84 commit 037d836

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

  • lib/scholar/decomposition

lib/scholar/decomposition/pca.ex

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ defmodule Scholar.Decomposition.PCA do
100100
iex> pca.components
101101
Nx.tensor(
102102
[
103-
[0.36182016134262085, -0.08202514797449112, 0.8565111756324768, 0.3588128685951233],
104-
[0.6585038900375366, 0.7275884747505188, -0.17632202804088593, -0.07679986208677292]
103+
[0.36158934, -0.082268484, 0.8565728, 0.35884288],
104+
[0.65654594, 0.72970587, -0.17576799, -0.074712664]
105105
]
106106
)
107107
iex> pca.singular_values
108-
Nx.tensor([25.089859008789062, 6.007821559906006])
108+
Nx.tensor([25.089865, 6.007852])
109109
"""
110110
deftransform fit(x, opts \\ []) do
111111
opts = NimbleOptions.validate!(opts, @opts_schema)
@@ -210,12 +210,12 @@ defmodule Scholar.Decomposition.PCA do
210210
iex> pca.components
211211
Nx.tensor(
212212
[
213-
[-0.33354005217552185, 0.1048964187502861, -0.8618107080105579, -0.3674643635749817],
214-
[-0.5862125754356384, -0.7916879057884216, 0.15874788165092468, -0.06621300429105759]
213+
[-0.3331483, 0.105326004, -0.86190677, -0.36747244],
214+
[-0.58288467, -0.79399943, 0.15797591, -0.06967129]
215215
]
216216
)
217217
iex> pca.singular_values
218-
Nx.tensor([77.05782028025969, 10.137848854064941])
218+
Nx.tensor([77.05691, 10.158366])
219219
"""
220220
deftransform incremental_fit(batches, opts) do
221221
opts = NimbleOptions.validate!(opts, @opts_schema)
@@ -242,12 +242,12 @@ defmodule Scholar.Decomposition.PCA do
242242
iex> pca.components
243243
Nx.tensor(
244244
[
245-
[-0.3229745328426361, 0.09587063640356064, -0.8628664612770081, -0.37677285075187683],
246-
[-0.6786625981330872, -0.7167785167694092, 0.14237160980701447, 0.07332050055265427]
245+
[-0.32289746, 0.095940694, -0.8628819, -0.37678596],
246+
[-0.67544055, -0.7201917, 0.141901, 0.070487075]
247247
]
248248
)
249249
iex> pca.singular_values
250-
Nx.tensor([166.141845703125, 6.078948020935059])
250+
Nx.tensor([166.14182, 6.078579])
251251
"""
252252
deftransform partial_fit(model, x) do
253253
if Nx.rank(x) != 2 do
@@ -373,9 +373,9 @@ defmodule Scholar.Decomposition.PCA do
373373
iex> Scholar.Decomposition.PCA.transform(pca, x_transform)
374374
Nx.tensor(
375375
[
376-
[-1.4739344120025635, -0.48932668566703796],
377-
[0.28113049268722534, 0.2337251454591751],
378-
[2.0361955165863037, 0.9567767977714539]
376+
[-1.4737695, -0.49078038],
377+
[0.28105056, 0.23384926],
378+
[2.0358706, 0.9584787]
379379
]
380380
)
381381
"""
@@ -442,13 +442,12 @@ defmodule Scholar.Decomposition.PCA do
442442
iex> Scholar.Decomposition.PCA.fit_transform(x, num_components: 2)
443443
Nx.tensor(
444444
[
445-
[0.16441848874092102, 0.028548287227749825],
446-
[-0.32804328203201294, 0.20709986984729767],
447-
[-0.3284338414669037, -0.08318747580051422],
448-
[-0.42237386107444763, -0.0735677033662796],
449-
[0.17480169236660004, -0.11189625412225723],
450-
[0.7396301627159119, 0.03300142288208008
451-
]
445+
[0.16341472, -0.017230444],
446+
[-0.33249766, 0.1893504],
447+
[-0.32686582, -0.10110473],
448+
[-0.42023695, -0.005525116],
449+
[0.1768687, -0.14014886],
450+
[0.7393165, 0.074657336]
452451
]
453452
)
454453
"""

0 commit comments

Comments
 (0)