collasso.single_task module#

Single-Task Learning (convenience functions).

Class:

IndepLassoCV - a wrapper function for sklearn.linear_model.LassoCV to model multiple targets based on a common feature matrix or specific feature matrices (using the same interface as CoopLassoCV)

Examples#

>>> from sklearn.datasets import load_linnerud
>>> from collasso import CoopLassoCV
>>> x, y = load_linnerud(return_X_y=True)
>>> model = IndepLassoCV()
>>> model.fit(x, y) # n_samples x p_features, n_samples x q_targets
>>> model.coef_ # q_targets x p_features
>>> y_pred = model.predict(x) # n_samples x q_targets
class collasso.single_task.IndepLassoCV(*, cv: int = 5, alphas: int = 100)#

Bases: RegressorMixin, BaseEstimator

Single-Task Lasso Regression For Multiple Targets.

Fits single-task lasso regression separately to multiple targets, optimising the regularisation parameters by cross-validation.

This is a convencience class with the interface as CoopLassoCV (but without sharing information among targets or features). Note that auxiliary features are simply excluded from the model (i.e., learning without privileged information).

Parameters:
cvint, default=5

Number of cross-validation folds.

alphasint, default=100

Number of candidate values for the regularisation parameter.

Attributes:
n_int

Number of training samples.

p_int

Number of features.

q_int

Number of targets.

model_list of length q_targets

Fitted models from LassoCV (one for each target).

coef_ndarray of shape (q_targets, p_features)

Estimated coefficients (of the feature in the column on the target in the row).

See also

CoopLassoCV

The main class of this package. It uses the same interface as IndepLassoCV (similarly formatted inputs and outputs) but shares information among targets and features to improve selection and prediction.

Examples

>>> from sklearn.datasets import load_linnerud
>>> from collasso import IndepLassoCV
>>> x, y = load_linnerud(return_X_y=True)
>>> model = IndepLassoCV()
>>> model.fit(x, y) # n_samples x p_features, n_samples x q_targets
>>> model.coef_ # q_targets x p_features
>>> y_pred = model.predict(x) # n_samples x q_targets
fit(X: ndarray, y: ndarray, Z: ndarray | None = None) IndepLassoCV#

Fit IndepLassoCV.

Fit independent lasso regressions to multiple targets.

Parameters:
Xndarray of shape (n_samples, p_features) or (n_samples, p_features, q_targets)

Common feature matrix for all targets or a separate feature matrix for each target.

yndarray of shape (n_samples, q_targets)

Target matrix.

Zndarray of shape (p_features,) or (p_features, q_targets), or None

Logical vector or matrix indicating primary (1/True) and auxiliary features (0/False) for all targets together or each target separately (NB: auxiliary features are simply excluded).

Returns:
self: IndepLassoCV

Fitted models.

predict(X: ndarray) ndarray#

Make predictions.

Make prediction with models estimated by independent lasso regressions.

Parameters:
Xndarray of shape (n_samples, p_features) or (n_samples, p_features, q_targets)

Common feature matrix for all targets, or a separate feature matrix for each target.

Returns:
y_hatndarray of shape (n_samples, q_targets)

Matrix of predicted values.

set_fit_request(*, Z: bool | None | str = '$UNCHANGED$') IndepLassoCV#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
Zstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for Z parameter in fit.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') IndepLassoCV#

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.