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].

The diagnostic tool spadimo (SPArse DIrections of Maximal Outlyingness) identifies which variables contribute most to making an observation an outlier [4].

The crm (Cellwise Robust M-regression) method detects and handles cellwise outliers - individual contaminated cells in the data matrix rather than entire rows [5].

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)

spadimo — Sparse directions of maximal outlyingness

SPADIMO identifies which variables contribute most to making an observation an outlier. Given case weights from a robust estimator (e.g., rtb), it computes a sparse direction of maximal outlyingness and flags the contributing variables.

from twoblock import rtb, spadimo

# First, fit a robust model to get case weights
r = rtb(n_components_x=5, n_components_y=2,
        centre='l1median', scale='mad', fun='Hampel')
r.fit(X, Y)

# Find observations with low weights (potential outliers)
outlier_indices = np.where(r.caseweights_ < 0.5)[0]

# Analyze an outlier to find contributing variables
sp = spadimo(scale='Qn', stop_early=True)
sp.fit(X, r.caseweights_, obs=outlier_indices[0])

# Get the flagged variables
print(f"Outlying variables: {sp.outlvars_}")
print(f"Outlyingness before: {sp.outlyingness_before_:.2f}")
print(f"Outlyingness after removing flagged vars: {sp.outlyingness_after_:.2f}")

# With a DataFrame, get variable names directly
sp.fit(X_df, r.caseweights_, obs=outlier_indices[0])
print(f"Outlying variable names: {sp.get_outlying_variables(names=True)}")

# Print a summary
sp.summary()

Key parameters:

  • scale: Robust scale estimator ('Qn', 'mad', 'scaleTau2')
  • etas: Sparsity parameters (default: sequence from 0.9 to 0.1)
  • stop_early: Stop at first eta where observation becomes non-outlying
  • csq_critv: Chi-squared quantile for outlyingness threshold (default: 0.975)

crm — Cellwise Robust M-regression

CRM detects and handles cellwise outliers - individual contaminated cells rather than entire rows. It provides regression coefficients robust against both vertical outliers and leverage points, a map of cellwise outliers, and an imputed dataset with outlying cells replaced.

from twoblock import crm
import numpy as np

# Fit CRM model with casewise robust starting values (default)
model = crm(center='median', scale='Qn', fun='Hampel')
model.fit(X, y)

# Fit CRM with cellwise robust starting values via DDC
# (requires robpy: pip install robpy)
model_ddc = crm(start_cellwise=True, center='median', scale='Qn')
model_ddc.fit(X, y)

# Predictions
y_pred = model.predict(X_new)

# View cellwise outlier map
print(f"Cellwise outliers detected: {np.sum(model.cellwise_outliers_)}")
print(f"Casewise outliers: {model.get_casewise_outliers()}")

# Get imputed data (outlying cells replaced)
X_imputed = model.X_imputed_

# Inspect which cells are outliers for a specific row
row_outliers = model.get_cellwise_outliers(row=0)
print(f"Outlying variables in row 0: {row_outliers}")

# With a DataFrame, get variable names
model.fit(X_df, y)
print(model.get_cellwise_outliers(row=0, names=True))

# Print summary
model.summary()

Key parameters:

  • start_cellwise: If True, use DDC for cellwise robust starting values (default: False, requires robpy)
  • center: Centering method ('median', 'mean', 'l1median')
  • scale: Scale estimator ('Qn', 'mad', 'scaleTau2')
  • regtype: Initial regression type ('MM', 'LTS')
  • fun: M-estimation psi-function ('Hampel', 'Huber', 'Fair')
  • crit_cellwise: Chi-squared quantile for cellwise outlier detection (default: 0.99)
  • maxiter: Maximum IRLS iterations (default: 100)
  • tolerance: Convergence threshold (default: 0.01)

Key attributes:

  • coef_: Regression coefficients
  • cellwise_outliers_: Boolean matrix of cell outliers (n, p)
  • casewise_outliers_: Boolean array of row outliers (n,)
  • X_imputed_: Imputed X matrix with outliers replaced
  • caseweights_: Case weights from M-estimation
  • ddc_outliers_: Cellwise outliers from DDC initialization (if start_cellwise=True)

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
  • crm_simulation.ipynb — CRM simulation under cellwise contamination with normal and Cauchy noise

References

[1] R.D. Cook, L. Forzani, L. 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.

[4] M. Debruyne, S. Höppner, S. Serneels, T. Verdonck. "Outlyingness: which variables contribute most?" Statistics and Computing 29 (4), 707-723.

[5] P. Filzmoser, S. Höppner, I. Ortner, S. Serneels, T. Verdonck. "Cellwise Robust M regression." Computational Statistics & Data Analysis 147 (2020): 106944.

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.4.0.tar.gz (42.7 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.4.0-py3-none-any.whl (45.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for twoblock-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4eccd718cdcfae11c8b342b18dbacc9f34ef3e3c7741db4d94e0c82b95e0155c
MD5 d430c298461cc082c4e9e71381debf2c
BLAKE2b-256 b0da9e3c4dbf7ab16db28a8c97a9b0c609a97126c0eaa3886803d2d19fd7783a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twoblock-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 45.2 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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccae2eaef9fbb892d94a11ba7b0416c4554ed8cdfd1f15262ae92ece2129c3eb
MD5 2522856682a6a3cf8ac30eb629248c55
BLAKE2b-256 f9ff53a0efa256972e8307c5967711e37a042f3811e9958d35e5f4af5d362a3a

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