Skip to main content

A Scikit-Learn Compatible Library for Simultaneous Two-Block Sufficient Dimension Reduction Methods

Project description

twoblock

Two-block dense and sparse simultaneous dimension reduction

The dense version is a scikit-learn compatible implementation of simultaneous two-block dimension reduction, as proposed in [1].

The sparse version is a scikit-learn compatible implementation of sparse twoblock dimension reduction, recently published by the author [2].

The robust version (rtb) extends twoblock with iterative M-estimation reweighting, providing resistance to outliers in both X and Y blocks [3].

Installation

pip install twoblock

Or install from source:

git clone https://github.com/SvenSerneels/twoblock.git
cd twoblock
pip install -e .

Dependencies

  • numpy >= 1.22.0
  • scikit-learn >= 1.3.0
  • pandas >= 1.4.0
  • scipy >= 1.8.0

Usage

twoblock — Dense and sparse two-block dimension reduction

from twoblock import twoblock
from sklearn.model_selection import GridSearchCV

# Dense twoblock
tb = twoblock(n_components_x=5, n_components_y=2, scale='std')
tb.fit(X_train, Y_train)
Y_pred = tb.predict(X_test)

# Sparse twoblock (variable selection via soft-thresholding)
tb_sparse = twoblock(n_components_x=5, n_components_y=2,
                     sparse=True, eta_x=0.7, eta_y=0, scale='std')
tb_sparse.fit(X_train, Y_train)
Y_pred = tb_sparse.predict(X_test)

# Cross-validation with scikit-learn
gcv = GridSearchCV(twoblock(),
                   {'n_components_x': range(1, 10),
                    'n_components_y': range(1, 3),
                    'scale': ['std', 'None']},
                   scoring='r2', cv=5)
gcv.fit(X_train, Y_train)

rtb — Robust twoblock with iterative reweighting

from twoblock import rtb

# Dense robust twoblock (Hampel weighting, robust centering/scaling)
r = rtb(n_components_x=5, n_components_y=2,
        centre='l1median', scale='mad',
        fun='Hampel', probp1=0.95, probp2=0.975, probp3=0.999)
r.fit(X_train, Y_train)
Y_pred = r.predict(X_test)

# Sparse robust twoblock
r_sparse = rtb(n_components_x=5, n_components_y=2,
               sparse=True, eta_x=0.5, eta_y=0,
               centre='l1median', scale='mad',
               fun='Hampel', probp1=0.95, probp2=0.975, probp3=0.999)
r_sparse.fit(X_train, Y_train)

# Inspect case weights (outliers receive low weights)
print(r_sparse.caseweights_)

# Cross-validation
gcv = GridSearchCV(rtb(verbose=False),
                   {'n_components_x': range(1, 10),
                    'n_components_y': [1, 2],
                    'scale': ['mad', 'kstepLTS'],
                    'probp1': [0.75, 0.95]},
                   scoring='r2', cv=5)
gcv.fit(X_train, Y_train)

Examples

Example notebooks are provided in the examples/ folder:

  • cookie_example.ipynb — Cookie dough NIR spectroscopy
  • gas_turbine_example.ipynb — Gas turbine CO/NOx emissions
  • simulation_rtb.ipynb — Simulation study comparing twoblock, sparse twoblock, rtb, and sparse rtb

References

[1] Cook, R. Dennis, Liliana Forzani, and Lan Liu. "Partial least squares for simultaneous reduction of response and predictor vectors in regression." Journal of Multivariate Analysis 196 (2023): 105163.

[2] S. Serneels. "Sparse Twoblock Dimension Reduction: A Versatile Alternative to Sparse PLS2 and CCA." Journal of Chemometrics, 39 (2025): e70051.

[3] S. Serneels. ["Robust Twoblock Dimension Reduction."] (https://arxiv.org/pdf/2603.24820) (2025, submitted). Preprint available at arXiv.org, arXiv: 2603.24820.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twoblock-0.3.0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twoblock-0.3.0-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file twoblock-0.3.0.tar.gz.

File metadata

  • Download URL: twoblock-0.3.0.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for twoblock-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ea991bb077805ba056cb64c9925a510676fe5bed65d16546155cfa14e47a5b92
MD5 9f00001c381e29e44a09d2d805b17be8
BLAKE2b-256 80c369ed593bf6248dacd4005b098087e3c22da562dea3db237900c1adb8c2f8

See more details on using hashes here.

File details

Details for the file twoblock-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: twoblock-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for twoblock-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3c0f09a6a0cfc9250ae8234adc78b7512602fa36f377621496f2ccef11e72bf
MD5 b93f3dd56ef6f43db71cce789bef7631
BLAKE2b-256 d6834776ec94d2a056bc1f75001688fca6ee31a6719bb6789b4c576f4a974ef4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page