Scikit-learn compatible Thresholded Ridge regression for high-dimensional variable selection
Project description
threshold-ridge
Scikit-learn compatible Thresholded Ridge regression for high-dimensional variable selection.
Overview
threshold-ridge implements the Thresholded Ridge (TR) estimator — a two-step procedure for sparse regression in high-dimensional settings ($p > n$):
- Fit Ridge regression with regularisation parameter $h_n$.
- Apply hard thresholding: retain only coefficients with $|\hat{\theta}_j| > a_n$.
The hyperparameters $(h_n, a_n)$ are jointly selected by one of two data-driven criteria:
- BIC (
tuning='bic'): Bayesian Information Criterion. - LOOCV (
tuning='loocv'): Leave-One-Out CV proxy exploiting the Ridge hat-matrix diagonal.
The estimator is fully compatible with the scikit-learn API and can be used inside Pipeline, GridSearchCV, and cross_val_score.
Installation
pip install threshold-ridge
Install the development version from GitHub:
pip install git+https://github.com/Farmer122/Threshold-Ridge.git
Quick Start
import numpy as np
from threshold_ridge import ThresholdedRidge
# Simulate a sparse high-dimensional dataset
rng = np.random.default_rng(0)
X = rng.standard_normal((200, 500))
beta = np.zeros(500)
beta[:25] = 1.0
y = X @ beta + rng.standard_normal(200)
# Fit with BIC tuning (default)
tr = ThresholdedRidge(tuning='bic')
tr.fit(X, y)
print(tr.support_.sum()) # number of selected variables
print(tr.coef_) # sparse coefficient vector
print(tr.score(X, y)) # R-squared
# Use LOOCV tuning instead
tr_cv = ThresholdedRidge(tuning='loocv')
tr_cv.fit(X, y)
# Works inside a scikit-learn Pipeline
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
pipe = Pipeline([
('scaler', StandardScaler()),
('tr', ThresholdedRidge(tuning='bic')),
])
pipe.fit(X, y)
API Reference
ThresholdedRidge
ThresholdedRidge(
tuning='bic', # 'bic' or 'loocv'
h_n_grid=None, # Ridge penalty grid (default: logspace(-2, 2, 20))
a_n_grid=None, # Threshold grid (default: linspace(0.01, 0.6, 15))
fit_intercept=True,
)
Fitted attributes
| Attribute | Description |
|---|---|
coef_ |
Sparse coefficient vector of shape (n_features,) |
intercept_ |
Fitted intercept (0 if fit_intercept=False) |
support_ |
Boolean mask of selected features |
best_h_n_ |
Optimal Ridge regularisation parameter |
best_a_n_ |
Optimal hard-threshold value |
Development
git clone https://github.com/Farmer122/Threshold-Ridge.git
cd threshold-ridge
pip install -e ".[dev]"
pytest tests/ -v
To build and publish a release to PyPI:
python -m build
twine upload dist/*
References
- Hoerl, A. E. and Kennard, R. W. (1970). Ridge Regression: Biased Estimation for Nonorthogonal Problems. Technometrics, 12(1), 55--67.
- Schwarz, G. (1978). Estimating the Dimension of a Model. Annals of Statistics, 6(2), 461--464.
- Tibshirani, R. (1996). Regression Shrinkage and Selection via the Lasso. Journal of the Royal Statistical Society: Series B, 58(1), 267--288.
License
MIT License. See LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file threshold_ridge-0.1.0.tar.gz.
File metadata
- Download URL: threshold_ridge-0.1.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfc0bd855135e953eef111afaa245534215520c0f7616adac3ced507a7c661da
|
|
| MD5 |
2b4ed3f1642f7115734636d6242058ee
|
|
| BLAKE2b-256 |
73e6b1dc27cd322aaa0aa165392a1d21c6027ec4549876c533aa7f4b266fd79e
|
File details
Details for the file threshold_ridge-0.1.0-py3-none-any.whl.
File metadata
- Download URL: threshold_ridge-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ca01c946b8287acf25b2672dc254383a8ba2c5be80b21a18320b726dff0021b
|
|
| MD5 |
99f888009767af81379ca14a21e27f79
|
|
| BLAKE2b-256 |
6d8fc01d130a4c6580a2d73c20551c9cddd55fafbe06d7e5478d15d2fd080db7
|