scm2cpp-learn 0.19.0__tar.gz → 0.21.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/PKG-INFO +128 -5
  2. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/README.md +127 -4
  3. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/pyproject.toml +1 -1
  4. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/setup.py +29 -0
  5. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/__init__.py +701 -71
  6. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_compat.py +72 -2
  7. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader.py +6 -6
  8. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_accel_cublas.py +47 -35
  9. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_classifiers.py +52 -52
  10. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_derived_blas.py +2 -2
  11. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_derived_cublas.py +2 -2
  12. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_derived_loops.py +2 -2
  13. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_gpu_cuda.py +41 -41
  14. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/_loader_gramops_loops.py → scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/_loader_gramops_blas.py +206 -161
  15. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/_loader_gramops_blas.py → scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/_loader_gramops_loops.py +225 -142
  16. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_nmf_blas.py +3 -3
  17. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_nmf_cublas.py +3 -3
  18. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/_loader_nmf_loops.py +3 -3
  19. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/accel_cublas/accel.cpp +2 -0
  20. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/accel_cublas/accel.hpp +1250 -0
  21. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/accel_cublas/accel_capi.cpp +82 -0
  22. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/classifiers.hpp +768 -768
  23. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/classifiers_capi.cpp +115 -0
  24. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_blas/lasso_design.hpp +38 -38
  25. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_blas/lasso_design_capi.cpp +1 -1
  26. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_cublas/lasso_design.hpp +39 -39
  27. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_cublas/lasso_design_capi.cpp +1 -1
  28. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/derived_loops/lasso_design.hpp +179 -0
  29. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_loops/lasso_design_capi.cpp +1 -1
  30. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/gpu_cuda/gpu.hpp +675 -675
  31. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/gpu_cuda/gpu_capi.cpp +44 -44
  32. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/gramops_blas/gramops.cpp +8 -0
  33. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/gramops_blas/gramops.hpp +2109 -1677
  34. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/gramops_blas/gramops_capi.cpp +295 -0
  35. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/gramops_loops/gramops.cpp +8 -0
  36. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/gramops_loops/gramops.hpp +7711 -0
  37. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/gramops_loops/gramops_capi.cpp +301 -0
  38. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/lasso_cov.hpp +152 -0
  39. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/lasso_cov_capi.cpp +3 -3
  40. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/nmf_blas/nmf_cd.hpp +39 -39
  41. {scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/nmf_cublas → scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/nmf_blas}/nmf_cd_capi.cpp +2 -2
  42. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/nmf_cublas/nmf_cd.hpp +136 -0
  43. {scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/nmf_loops → scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/nmf_cublas}/nmf_cd_capi.cpp +2 -2
  44. scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/nmf_loops/nmf_cd.hpp +167 -0
  45. {scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/nmf_blas → scm2cpp_learn-0.21.0/src/scm2cpp_learn/_generated/nmf_loops}/nmf_cd_capi.cpp +2 -2
  46. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/scm2cpp-blas.hpp +18 -0
  47. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/scm2cpp.hpp +15 -1
  48. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/batch_capi.cu +7 -4
  49. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn.egg-info/PKG-INFO +128 -5
  50. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/accel_cublas/accel.hpp +0 -1178
  51. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/accel_cublas/accel_capi.cpp +0 -76
  52. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/classifiers_capi.cpp +0 -115
  53. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/derived_loops/lasso_design.hpp +0 -179
  54. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/gramops_blas/gramops_capi.cpp +0 -271
  55. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/gramops_loops/gramops.hpp +0 -7279
  56. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/gramops_loops/gramops_capi.cpp +0 -277
  57. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/lasso_cov.hpp +0 -152
  58. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/nmf_cublas/nmf_cd.hpp +0 -136
  59. scm2cpp_learn-0.19.0/src/scm2cpp_learn/_generated/nmf_loops/nmf_cd.hpp +0 -167
  60. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/setup.cfg +0 -0
  61. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/__init__.py +0 -0
  62. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/classifiers.cpp +0 -0
  63. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_blas/lasso_design.cpp +0 -0
  64. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_cublas/lasso_design.cpp +0 -0
  65. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/derived_loops/lasso_design.cpp +0 -0
  66. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/gpu_cuda/gpu.cpp +0 -0
  67. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/lasso_cov.cpp +0 -0
  68. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/nmf_blas/nmf_cd.cpp +0 -0
  69. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/nmf_cublas/nmf_cd.cpp +0 -0
  70. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/nmf_loops/nmf_cd.cpp +0 -0
  71. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/scm2cpp-cublas.hpp +0 -0
  72. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_generated/scm2cpp-cuda.hpp +0 -0
  73. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn/_libfind.py +0 -0
  74. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn.egg-info/SOURCES.txt +0 -0
  75. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn.egg-info/dependency_links.txt +0 -0
  76. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn.egg-info/requires.txt +0 -0
  77. {scm2cpp_learn-0.19.0 → scm2cpp_learn-0.21.0}/src/scm2cpp_learn.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scm2cpp-learn
3
- Version: 0.19.0
3
+ Version: 0.21.0
4
4
  Summary: scikit-learn estimators (lasso, elastic net, ridge, RidgeCV, ARD, SVM, SVR, logistic regression, GLMs, Huber, Theil-Sen, F-score selection, RFE, NMF, Gaussian mixture, FastICA, PCA, KernelPCA, LDA, TruncatedSVD, PLS, CCA, graphical lasso, RANSAC, SGD, OMP, LARS) translated from Scheme to C++ by scm2cpp; GPU batch solver and CUDA kernels included
5
5
  Author: Hirotaka Niitsuma
6
6
  License: MIT
@@ -206,9 +206,10 @@ package picks the fastest one present.
206
206
 
207
207
  | class | scikit-learn | Scheme kernel | builds | sklearnex accelerates |
208
208
  |---|---|---|---|---|
209
- | `GramLasso` | Lasso(precompute=True), fit(X, y) with the intercept | `lasso-cov.scm` | CPU | no |
210
- | `GramElasticNet` | ElasticNet(precompute=True), fit(X, y) with the intercept | `lasso-cov.scm` | CPU | no |
211
- | `GramRidge` | Ridge, fit(X, y) with the intercept, one or many targets | `gram.scm` (Cholesky; dposv) | loops / BLAS | Ridge |
209
+ | `GramLasso` | Lasso(precompute=True), fit(X, y) with the intercept, dense or sparse X | `lasso-cov.scm` | CPU | no |
210
+ | `GramElasticNet` | ElasticNet(precompute=True), fit(X, y) with the intercept, dense or sparse X | `lasso-cov.scm` | CPU | no |
211
+ | `GramRidge` | Ridge, fit(X, y) with the intercept, one or many targets, dense or sparse X | `gram.scm` (Cholesky; dposv) | loops / BLAS | Ridge |
212
+ | `GramLinearRegression` | LinearRegression, dense or sparse X, sample_weight | `linreg.scm` (Cholesky; dposv, dpocon) | loops / BLAS | no |
212
213
  | `GramRidgeCV` | RidgeCV (cv=None), leave-one-out over every alpha from one eigenproblem | `ridge-gcv.scm` | loops / BLAS | no |
213
214
  | `GramMultiTaskLasso` | MultiTaskLasso, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
214
215
  | `GramMultiTaskElasticNet` | MultiTaskElasticNet, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
@@ -256,8 +257,9 @@ package picks the fastest one present.
256
257
  | `CovPCA` | PCA(svd_solver='covariance_eigh') | `pca.scm` | loops / BLAS / cuBLAS | PCA |
257
258
  | `GramKernelPCA` | KernelPCA (no inverse_transform) | `kernel-pca.scm` | loops / BLAS | no |
258
259
  | `GramLDA` | LinearDiscriminantAnalysis (svd, lsqr, eigen) | `lda.scm` | loops / BLAS | no |
259
- | `GramTruncatedSVD` | TruncatedSVD on dense X (exact, as arpack) | `truncated-svd.scm` | loops / BLAS | no |
260
+ | `GramTruncatedSVD` | TruncatedSVD on dense or sparse X (exact, as arpack) | `truncated-svd.scm`, `sparse.scm` | loops / BLAS | no |
260
261
  | `GramPLSRegression`, `GramPLSCanonical`, `GramCCA`, `GramPLSSVD` | PLSRegression, PLSCanonical, CCA, PLSSVD | `pls.scm` | loops / BLAS | no |
262
+ | `GramFactorAnalysis` | FactorAnalysis (exact, as svd_method='lapack'; rotations) | `fa.scm` | loops / BLAS | no |
261
263
  | `f_regression, f_classif` | f_regression, f_classif | `fscore.scm` | loops / BLAS / cuBLAS | no |
262
264
  | `SelectKBest, SelectPercentile` | SelectKBest, SelectPercentile | `fscore.scm` | loops / BLAS / cuBLAS | no |
263
265
  | `GramRFE` | RFE(Ridge), RFE(LinearRegression) | `rfe.scm` | loops / BLAS / cuBLAS | inside: Ridge |
@@ -454,6 +456,32 @@ the pick is exact at n=5,000 and one grid step apart on a near-tie
454
456
  elsewhere (mean-MSE relative gap at most 2.6e-4), and sklearn at
455
457
  tol=1e-10 picks our alpha at every size.
456
458
 
459
+ `cv` may also be a splitter (`TimeSeriesSplit`, a shuffled `KFold`, ...)
460
+ or a list of (train, test) index pairs, and `fit_intercept=True` centres
461
+ each fold's training rows as `LassoCV` does (through the Gram, not a
462
+ centred copy); the default stays the fit through the origin. A
463
+ `TimeSeriesSplit` fold's training rows are a prefix, so its Gram is the
464
+ previous fold's plus the new rows' -- the five folds cost one pass over
465
+ the training rows, not five. Against `LassoCV` on the same grid the
466
+ validation errors agree to 1e-11 and the coefficients and intercept
467
+ to 1e-11.
468
+
469
+ `solver="lars"` (lasso only) solves each fold and the refit by the
470
+ translated least angle regression over the same Gram
471
+ (`examples/kernel-only/lars.scm`, method "lasso") and reads the
472
+ solutions at the grid off the recorded path, which is piecewise linear
473
+ in alpha: exact up to rounding, with no tolerance, and with a cost set
474
+ by the number of path steps rather than by conditioning. Columns equal
475
+ up to a factor are cut to the longest one first (two equal columns tie
476
+ in the path; scikit-learn's `lars_path` stops early there), and a grid
477
+ point whose solution misses the optimality conditions (1e-5 relative)
478
+ is finished by coordinate descent (`n_polished_`). Where the columns
479
+ are strongly correlated -- moving averages over overlapping windows --
480
+ coordinate descent needs thousands of sweeps and LARS is far faster: on
481
+ a stock-feature design with n=743 and p=180, 0.21 s against 145 s for
482
+ the descent on the same grid; at p=3495, 2.2 s, where the descent was
483
+ stopped after 30 minutes. The default stays `solver="cd"`.
484
+
457
485
  The elastic net cross-validates the same way: `CovLassoCV(l1_ratio=0.5)`
458
486
  against `ElasticNetCV(l1_ratio=0.5)` on the same folds and alpha grid
459
487
  (`bench/lasso-compare.py --cv-only --l1-ratio 0.5`, same conditions,
@@ -757,6 +785,101 @@ of three):
757
785
 
758
786
  PLSSVD is one SVD of X'Y on both sides, so only the product differs.
759
787
 
788
+ ## Factor analysis: `GramFactorAnalysis`
789
+
790
+ scikit-learn's `FactorAnalysis` over the covariance
791
+ (`examples/kernel-only/fa.scm`). Every iteration of scikit-learn's takes
792
+ the SVD of the n x p matrix Xc / (sqrt(psi) sqrt(n)); its squared
793
+ singular values and right singular vectors are the eigenpairs of
794
+ D^-1 C D^-1, C = Xc'Xc / n and D = diag(sqrt psi). So C is formed once
795
+ (dsyrk) and each iteration is the top `n_components` eigenpairs of a
796
+ p x p matrix (dsyevr), then scikit-learn's own updates of the
797
+ components, the log-likelihood and psi. The eigenproblem is exact: what
798
+ `svd_method="lapack"` computes, which the fit matches to 1e-13 with the
799
+ same iterations; scikit-learn's default `"randomized"` approximates it
800
+ (here to 1e-6 in psi). Rotations (varimax, quartimax), `transform`,
801
+ `score`, `get_covariance` and `get_precision` are scikit-learn's.
802
+
803
+ ```python
804
+ from scm2cpp_learn import GramFactorAnalysis
805
+ fa = GramFactorAnalysis(n_components=10).fit(X)
806
+ fa.components_, fa.noise_variance_, fa.transform(X)
807
+ ```
808
+
809
+ Measured (`bench/eigen-compare.py`, the MKL build, best of two; load
810
+ average 10 to 16 from other jobs):
811
+
812
+ | case | scikit-learn | package | ratio, one core | ratio, 20 cores |
813
+ |---|---|---|---|---|
814
+ | FactorAnalysis(randomized), 200,000 x 100, 10 factors | 7.06 s | 0.11 s | 62x | 56x |
815
+ | FactorAnalysis(lapack), 200,000 x 100, 10 factors | 28.4 s | 0.10 s | 286x | 219x |
816
+ | FactorAnalysis(randomized), 50,000 x 500, 20 factors | 4.37 s | 0.33 s | 13x | 28x |
817
+ | FactorAnalysis(lapack), 50,000 x 500, 20 factors | 22.0 s | 0.33 s | 67x | 106x |
818
+
819
+ ## Least squares: `GramLinearRegression`
820
+
821
+ scikit-learn's `LinearRegression` over the Gram matrix
822
+ (`examples/kernel-only/linreg.scm`): Xc'Xc and Xc'y in one pass, then
823
+ the normal equations of the column-scaled Gram by Cholesky (dposv),
824
+ where scikit-learn runs LAPACK's gelsd on the n x p design. The normal
825
+ equations square the condition number, so the answer is taken only
826
+ when the scaled Gram's condition (dpocon's estimate) is at most 1e6,
827
+ which bounds the relative error near 1e-10, and the design has full
828
+ rank at `tol`; otherwise, and for `positive=True`, the fit is
829
+ scikit-learn's own (gelsd, or nnls). `rank_` and `singular_` come from
830
+ the Gram's eigenvalues when first read. A sparse X is taken (below).
831
+
832
+ Measured (`bench/eigen-compare.py`, as above):
833
+
834
+ | case | scikit-learn | package | ratio, one core | ratio, 20 cores |
835
+ |---|---|---|---|---|
836
+ | LinearRegression, 500,000 x 100 | 7.25 s | 0.39 s | 19x | 19x |
837
+ | LinearRegression, 200,000 x 500 | 10.9 s | 1.15 s | 9.5x | 50x |
838
+ | LinearRegression, 200,000 x 100, 10 targets | 2.83 s | 0.17 s | 17x | 18x |
839
+ | LinearRegression, 20,000 x 2,000 | 12.3 s | 1.41 s | 8.7x | 123x |
840
+
841
+ The coefficients agree to 1e-13.
842
+
843
+ ## Sparse X
844
+
845
+ `GramLasso`, `GramElasticNet`, `GramRidge`, `GramLinearRegression` and
846
+ `GramTruncatedSVD` take a scipy.sparse X (any format; CSR reaches the
847
+ kernels). X'X is formed by `examples/kernel-only/sparse.scm`'s
848
+ `csr-gram` -- each row's outer product into the upper triangle, rows in
849
+ order, blocks of rows on threads into their own G's -- and X'y and X B
850
+ by `csr-mm`; the intercept is the n xm xm' correction, as scikit-learn's
851
+ sparse coordinate descent keeps X uncentred too. After X'X the fits are
852
+ the dense ones, so the answers equal the dense X's to 1e-15 and are
853
+ exact: scikit-learn's sparse Ridge and LinearRegression are iterative
854
+ (sparse_cg, lsqr) and agree with its own dense answers only to their
855
+ tolerance (1e-5 and 1e-7 here).
856
+
857
+ Measured (`bench/sparse-compare.py`, the MKL build, best of two; load
858
+ average 10 to 29):
859
+
860
+ | case | scikit-learn | package | ratio, one core | ratio, 20 cores |
861
+ |---|---|---|---|---|
862
+ | Lasso, 200,000 x 200, 5% dense | 0.16 s | 0.067 s | 2.4x | 2.6x |
863
+ | ElasticNet, 200,000 x 200, 5% dense | 0.16 s | 0.059 s | 2.6x | 3.4x |
864
+ | Ridge, 200,000 x 200, 5% dense | 0.056 s | 0.064 s | 0.9x | 0.9x |
865
+ | LinearRegression, 200,000 x 200, 5% dense | 0.074 s | 0.076 s | 1.0x | 1.3x |
866
+ | TruncatedSVD (randomized), 200,000 x 200, 5% dense | 0.86 s | 0.11 s | 8.1x | 6.2x |
867
+ | TruncatedSVD (arpack), 200,000 x 200, 5% dense | 1.54 s | 0.11 s | 15x | 13x |
868
+ | Lasso, 100,000 x 1,000, 1% dense | 0.14 s | 0.069 s | 2.1x | 3.0x |
869
+ | Ridge, 100,000 x 1,000, 1% dense | 0.043 s | 0.082 s | 0.5x | 0.6x |
870
+ | TruncatedSVD (arpack), 100,000 x 1,000, 1% dense | 1.41 s | 0.16 s | 8.7x | 8.7x |
871
+ | Lasso, 50,000 x 2,000, 0.5% dense | 0.11 s | 0.15 s | 0.7x | 1.2x |
872
+ | Ridge, 50,000 x 2,000, 0.5% dense | 0.024 s | 0.23 s | 0.1x | 0.2x |
873
+ | TruncatedSVD (randomized), 50,000 x 2,000, 0.5% dense | 0.16 s | 1.49 s | 0.1x | 0.6x |
874
+
875
+ The Gram route pays p^2 memory and p^3 for the solve or the
876
+ eigenproblem whatever the sparsity, where scikit-learn's iterative
877
+ sparse solvers pay a few passes over the nonzeros. So it wins for
878
+ moderate p (a few hundred) and for TruncatedSVD against arpack, and
879
+ loses for p in the thousands with few nonzeros per row: there, Ridge
880
+ and LinearRegression are better left to scikit-learn's sparse solvers
881
+ (at their tolerance), and the randomized TruncatedSVD to scikit-learn.
882
+
760
883
  ## Bayesian ridge
761
884
 
762
885
  `CovBayesianRidge` is scikit-learn's `BayesianRidge` -- the same model,
@@ -188,9 +188,10 @@ package picks the fastest one present.
188
188
 
189
189
  | class | scikit-learn | Scheme kernel | builds | sklearnex accelerates |
190
190
  |---|---|---|---|---|
191
- | `GramLasso` | Lasso(precompute=True), fit(X, y) with the intercept | `lasso-cov.scm` | CPU | no |
192
- | `GramElasticNet` | ElasticNet(precompute=True), fit(X, y) with the intercept | `lasso-cov.scm` | CPU | no |
193
- | `GramRidge` | Ridge, fit(X, y) with the intercept, one or many targets | `gram.scm` (Cholesky; dposv) | loops / BLAS | Ridge |
191
+ | `GramLasso` | Lasso(precompute=True), fit(X, y) with the intercept, dense or sparse X | `lasso-cov.scm` | CPU | no |
192
+ | `GramElasticNet` | ElasticNet(precompute=True), fit(X, y) with the intercept, dense or sparse X | `lasso-cov.scm` | CPU | no |
193
+ | `GramRidge` | Ridge, fit(X, y) with the intercept, one or many targets, dense or sparse X | `gram.scm` (Cholesky; dposv) | loops / BLAS | Ridge |
194
+ | `GramLinearRegression` | LinearRegression, dense or sparse X, sample_weight | `linreg.scm` (Cholesky; dposv, dpocon) | loops / BLAS | no |
194
195
  | `GramRidgeCV` | RidgeCV (cv=None), leave-one-out over every alpha from one eigenproblem | `ridge-gcv.scm` | loops / BLAS | no |
195
196
  | `GramMultiTaskLasso` | MultiTaskLasso, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
196
197
  | `GramMultiTaskElasticNet` | MultiTaskElasticNet, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
@@ -238,8 +239,9 @@ package picks the fastest one present.
238
239
  | `CovPCA` | PCA(svd_solver='covariance_eigh') | `pca.scm` | loops / BLAS / cuBLAS | PCA |
239
240
  | `GramKernelPCA` | KernelPCA (no inverse_transform) | `kernel-pca.scm` | loops / BLAS | no |
240
241
  | `GramLDA` | LinearDiscriminantAnalysis (svd, lsqr, eigen) | `lda.scm` | loops / BLAS | no |
241
- | `GramTruncatedSVD` | TruncatedSVD on dense X (exact, as arpack) | `truncated-svd.scm` | loops / BLAS | no |
242
+ | `GramTruncatedSVD` | TruncatedSVD on dense or sparse X (exact, as arpack) | `truncated-svd.scm`, `sparse.scm` | loops / BLAS | no |
242
243
  | `GramPLSRegression`, `GramPLSCanonical`, `GramCCA`, `GramPLSSVD` | PLSRegression, PLSCanonical, CCA, PLSSVD | `pls.scm` | loops / BLAS | no |
244
+ | `GramFactorAnalysis` | FactorAnalysis (exact, as svd_method='lapack'; rotations) | `fa.scm` | loops / BLAS | no |
243
245
  | `f_regression, f_classif` | f_regression, f_classif | `fscore.scm` | loops / BLAS / cuBLAS | no |
244
246
  | `SelectKBest, SelectPercentile` | SelectKBest, SelectPercentile | `fscore.scm` | loops / BLAS / cuBLAS | no |
245
247
  | `GramRFE` | RFE(Ridge), RFE(LinearRegression) | `rfe.scm` | loops / BLAS / cuBLAS | inside: Ridge |
@@ -436,6 +438,32 @@ the pick is exact at n=5,000 and one grid step apart on a near-tie
436
438
  elsewhere (mean-MSE relative gap at most 2.6e-4), and sklearn at
437
439
  tol=1e-10 picks our alpha at every size.
438
440
 
441
+ `cv` may also be a splitter (`TimeSeriesSplit`, a shuffled `KFold`, ...)
442
+ or a list of (train, test) index pairs, and `fit_intercept=True` centres
443
+ each fold's training rows as `LassoCV` does (through the Gram, not a
444
+ centred copy); the default stays the fit through the origin. A
445
+ `TimeSeriesSplit` fold's training rows are a prefix, so its Gram is the
446
+ previous fold's plus the new rows' -- the five folds cost one pass over
447
+ the training rows, not five. Against `LassoCV` on the same grid the
448
+ validation errors agree to 1e-11 and the coefficients and intercept
449
+ to 1e-11.
450
+
451
+ `solver="lars"` (lasso only) solves each fold and the refit by the
452
+ translated least angle regression over the same Gram
453
+ (`examples/kernel-only/lars.scm`, method "lasso") and reads the
454
+ solutions at the grid off the recorded path, which is piecewise linear
455
+ in alpha: exact up to rounding, with no tolerance, and with a cost set
456
+ by the number of path steps rather than by conditioning. Columns equal
457
+ up to a factor are cut to the longest one first (two equal columns tie
458
+ in the path; scikit-learn's `lars_path` stops early there), and a grid
459
+ point whose solution misses the optimality conditions (1e-5 relative)
460
+ is finished by coordinate descent (`n_polished_`). Where the columns
461
+ are strongly correlated -- moving averages over overlapping windows --
462
+ coordinate descent needs thousands of sweeps and LARS is far faster: on
463
+ a stock-feature design with n=743 and p=180, 0.21 s against 145 s for
464
+ the descent on the same grid; at p=3495, 2.2 s, where the descent was
465
+ stopped after 30 minutes. The default stays `solver="cd"`.
466
+
439
467
  The elastic net cross-validates the same way: `CovLassoCV(l1_ratio=0.5)`
440
468
  against `ElasticNetCV(l1_ratio=0.5)` on the same folds and alpha grid
441
469
  (`bench/lasso-compare.py --cv-only --l1-ratio 0.5`, same conditions,
@@ -739,6 +767,101 @@ of three):
739
767
 
740
768
  PLSSVD is one SVD of X'Y on both sides, so only the product differs.
741
769
 
770
+ ## Factor analysis: `GramFactorAnalysis`
771
+
772
+ scikit-learn's `FactorAnalysis` over the covariance
773
+ (`examples/kernel-only/fa.scm`). Every iteration of scikit-learn's takes
774
+ the SVD of the n x p matrix Xc / (sqrt(psi) sqrt(n)); its squared
775
+ singular values and right singular vectors are the eigenpairs of
776
+ D^-1 C D^-1, C = Xc'Xc / n and D = diag(sqrt psi). So C is formed once
777
+ (dsyrk) and each iteration is the top `n_components` eigenpairs of a
778
+ p x p matrix (dsyevr), then scikit-learn's own updates of the
779
+ components, the log-likelihood and psi. The eigenproblem is exact: what
780
+ `svd_method="lapack"` computes, which the fit matches to 1e-13 with the
781
+ same iterations; scikit-learn's default `"randomized"` approximates it
782
+ (here to 1e-6 in psi). Rotations (varimax, quartimax), `transform`,
783
+ `score`, `get_covariance` and `get_precision` are scikit-learn's.
784
+
785
+ ```python
786
+ from scm2cpp_learn import GramFactorAnalysis
787
+ fa = GramFactorAnalysis(n_components=10).fit(X)
788
+ fa.components_, fa.noise_variance_, fa.transform(X)
789
+ ```
790
+
791
+ Measured (`bench/eigen-compare.py`, the MKL build, best of two; load
792
+ average 10 to 16 from other jobs):
793
+
794
+ | case | scikit-learn | package | ratio, one core | ratio, 20 cores |
795
+ |---|---|---|---|---|
796
+ | FactorAnalysis(randomized), 200,000 x 100, 10 factors | 7.06 s | 0.11 s | 62x | 56x |
797
+ | FactorAnalysis(lapack), 200,000 x 100, 10 factors | 28.4 s | 0.10 s | 286x | 219x |
798
+ | FactorAnalysis(randomized), 50,000 x 500, 20 factors | 4.37 s | 0.33 s | 13x | 28x |
799
+ | FactorAnalysis(lapack), 50,000 x 500, 20 factors | 22.0 s | 0.33 s | 67x | 106x |
800
+
801
+ ## Least squares: `GramLinearRegression`
802
+
803
+ scikit-learn's `LinearRegression` over the Gram matrix
804
+ (`examples/kernel-only/linreg.scm`): Xc'Xc and Xc'y in one pass, then
805
+ the normal equations of the column-scaled Gram by Cholesky (dposv),
806
+ where scikit-learn runs LAPACK's gelsd on the n x p design. The normal
807
+ equations square the condition number, so the answer is taken only
808
+ when the scaled Gram's condition (dpocon's estimate) is at most 1e6,
809
+ which bounds the relative error near 1e-10, and the design has full
810
+ rank at `tol`; otherwise, and for `positive=True`, the fit is
811
+ scikit-learn's own (gelsd, or nnls). `rank_` and `singular_` come from
812
+ the Gram's eigenvalues when first read. A sparse X is taken (below).
813
+
814
+ Measured (`bench/eigen-compare.py`, as above):
815
+
816
+ | case | scikit-learn | package | ratio, one core | ratio, 20 cores |
817
+ |---|---|---|---|---|
818
+ | LinearRegression, 500,000 x 100 | 7.25 s | 0.39 s | 19x | 19x |
819
+ | LinearRegression, 200,000 x 500 | 10.9 s | 1.15 s | 9.5x | 50x |
820
+ | LinearRegression, 200,000 x 100, 10 targets | 2.83 s | 0.17 s | 17x | 18x |
821
+ | LinearRegression, 20,000 x 2,000 | 12.3 s | 1.41 s | 8.7x | 123x |
822
+
823
+ The coefficients agree to 1e-13.
824
+
825
+ ## Sparse X
826
+
827
+ `GramLasso`, `GramElasticNet`, `GramRidge`, `GramLinearRegression` and
828
+ `GramTruncatedSVD` take a scipy.sparse X (any format; CSR reaches the
829
+ kernels). X'X is formed by `examples/kernel-only/sparse.scm`'s
830
+ `csr-gram` -- each row's outer product into the upper triangle, rows in
831
+ order, blocks of rows on threads into their own G's -- and X'y and X B
832
+ by `csr-mm`; the intercept is the n xm xm' correction, as scikit-learn's
833
+ sparse coordinate descent keeps X uncentred too. After X'X the fits are
834
+ the dense ones, so the answers equal the dense X's to 1e-15 and are
835
+ exact: scikit-learn's sparse Ridge and LinearRegression are iterative
836
+ (sparse_cg, lsqr) and agree with its own dense answers only to their
837
+ tolerance (1e-5 and 1e-7 here).
838
+
839
+ Measured (`bench/sparse-compare.py`, the MKL build, best of two; load
840
+ average 10 to 29):
841
+
842
+ | case | scikit-learn | package | ratio, one core | ratio, 20 cores |
843
+ |---|---|---|---|---|
844
+ | Lasso, 200,000 x 200, 5% dense | 0.16 s | 0.067 s | 2.4x | 2.6x |
845
+ | ElasticNet, 200,000 x 200, 5% dense | 0.16 s | 0.059 s | 2.6x | 3.4x |
846
+ | Ridge, 200,000 x 200, 5% dense | 0.056 s | 0.064 s | 0.9x | 0.9x |
847
+ | LinearRegression, 200,000 x 200, 5% dense | 0.074 s | 0.076 s | 1.0x | 1.3x |
848
+ | TruncatedSVD (randomized), 200,000 x 200, 5% dense | 0.86 s | 0.11 s | 8.1x | 6.2x |
849
+ | TruncatedSVD (arpack), 200,000 x 200, 5% dense | 1.54 s | 0.11 s | 15x | 13x |
850
+ | Lasso, 100,000 x 1,000, 1% dense | 0.14 s | 0.069 s | 2.1x | 3.0x |
851
+ | Ridge, 100,000 x 1,000, 1% dense | 0.043 s | 0.082 s | 0.5x | 0.6x |
852
+ | TruncatedSVD (arpack), 100,000 x 1,000, 1% dense | 1.41 s | 0.16 s | 8.7x | 8.7x |
853
+ | Lasso, 50,000 x 2,000, 0.5% dense | 0.11 s | 0.15 s | 0.7x | 1.2x |
854
+ | Ridge, 50,000 x 2,000, 0.5% dense | 0.024 s | 0.23 s | 0.1x | 0.2x |
855
+ | TruncatedSVD (randomized), 50,000 x 2,000, 0.5% dense | 0.16 s | 1.49 s | 0.1x | 0.6x |
856
+
857
+ The Gram route pays p^2 memory and p^3 for the solve or the
858
+ eigenproblem whatever the sparsity, where scikit-learn's iterative
859
+ sparse solvers pay a few passes over the nonzeros. So it wins for
860
+ moderate p (a few hundred) and for TruncatedSVD against arpack, and
861
+ loses for p in the thousands with few nonzeros per row: there, Ridge
862
+ and LinearRegression are better left to scikit-learn's sparse solvers
863
+ (at their tolerance), and the randomized TruncatedSVD to scikit-learn.
864
+
742
865
  ## Bayesian ridge
743
866
 
744
867
  `CovBayesianRidge` is scikit-learn's `BayesianRidge` -- the same model,
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scm2cpp-learn"
7
- version = "0.19.0"
7
+ version = "0.21.0"
8
8
  description = "scikit-learn estimators (lasso, elastic net, ridge, RidgeCV, ARD, SVM, SVR, logistic regression, GLMs, Huber, Theil-Sen, F-score selection, RFE, NMF, Gaussian mixture, FastICA, PCA, KernelPCA, LDA, TruncatedSVD, PLS, CCA, graphical lasso, RANSAC, SGD, OMP, LARS) translated from Scheme to C++ by scm2cpp; GPU batch solver and CUDA kernels included"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -12,6 +12,7 @@ device, or a failed nvcc all leave a working CPU package behind, and
12
12
  the batch path notices at import and falls back.
13
13
  """
14
14
  import os
15
+ import platform
15
16
  import re
16
17
  import shutil
17
18
  import subprocess
@@ -33,6 +34,33 @@ GEN = PKG / "_generated"
33
34
  MSVC = ["/O2", "/std:c++17", "/EHsc", "/bigobj"]
34
35
  CXXFLAGS = ["-O2", "-std=c++17"] if os.name != "nt" else MSVC
35
36
 
37
+
38
+ def align_flags():
39
+ """-Wa,-mbranches-within-32B-boundaries where the assembler takes it.
40
+
41
+ Skylake-family x86 cores slow a jump that crosses or ends on a 32-byte
42
+ boundary (the JCC erratum microcode), so where a hot loop's branch
43
+ lands depends on unrelated code around it: the linear SVM's sweep
44
+ moved 10-20% between builds that differed only elsewhere. The GNU
45
+ assembler pads such branches away. Other assemblers and other
46
+ machines get nothing.
47
+ """
48
+ if os.name == "nt" or platform.machine().lower() not in ("x86_64", "amd64"):
49
+ return []
50
+ cxx = os.environ.get("CXX", "c++")
51
+ flag = "-Wa,-mbranches-within-32B-boundaries"
52
+ try:
53
+ r = subprocess.run([cxx, flag, "-x", "c++", "-c", "-", "-o", os.devnull],
54
+ input="int f(int n){int s=0;for(int i=0;i<n;i++)s+=i;return s;}\n",
55
+ capture_output=True, text=True)
56
+ except OSError:
57
+ return []
58
+ return [flag] if r.returncode == 0 else []
59
+
60
+
61
+ _align = align_flags()
62
+ CXXFLAGS = CXXFLAGS + _align
63
+
36
64
  kernel = Extension(
37
65
  "scm2cpp_learn._lasso_cov",
38
66
  sources=[GEN_REL + "/lasso_cov_capi.cpp"],
@@ -252,6 +280,7 @@ def cuda_home():
252
280
  # the flat index i*p + j as an induction variable, and the linear SVM's
253
281
  # sweep runs 25% slower.
254
282
  _cls_flags = ["-O3", "-std=c++17", "-fstrict-overflow"] if os.name != "nt" else MSVC
283
+ _cls_flags = _cls_flags + _align
255
284
  classifiers = Extension(
256
285
  "scm2cpp_learn._classifiers",
257
286
  sources=[GEN_REL + "/classifiers_capi.cpp"],