collasso.multi_task module#
Multi-Task Learning.
- Main Class:
CoopLassoCV- cross-validated cooperative multi-task lasso regression
Examples#
>>> from sklearn.datasets import load_linnerud
>>> from collasso import CoopLassoCV
>>> x, y = load_linnerud(return_X_y=True)
>>> model = CoopLassoCV()
>>> 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.multi_task.CoopLassoCV(*, cv: int = 5, n_alphas: int = 100, l1_ratio: float = 0.5, exp_y: float = 1, exp_x: float = 1, random_state: int | None = None)#
Bases:
RegressorMixin,BaseEstimatorCross-Validated Cooperative Multi-Task Lasso Regression.
Implements cooperative multi-task lasso regression, optimising the regularisation parameters by cross-validation.
- Parameters:
- cvint, default=5
Number of cross-validation folds.
- n_alphasint, default=100
Number of candidate values for the regularisation parameter in the final regressions.
- l1_ratiofloat, default=0.5
Elastic net mixing parameter for the initial regressions, with 0<=l1_ratio<=1, where l1_ratio=0 leads to L2 (ridge) and l1_ratio=1 leads to L1 (lasso) penalisation.
- exp_yfloat, default=1.0
Non-negative number for exponentiating the target-target correlation coefficients.
- exp_xfloat, default=1.0
Non-negative number for exponentiating the feature-feature correlation coefficients.
- random_stateint or None, default=None
Random seed for generating reproducible cross-validation folds.
- 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
_CoopLasso.- alpha_list length q_targets of ndarrays
Sequence of regularisation parameters.
- mse_list of length q_targets of ndarrays
Cross-validated mean squared errors for each value of alpha.
- min_list of length q_targets of ndarrays
Indices of regularisation parameters corresponding to the lowest mean squared error.
- coef_ndarray of shape (q_targets, p_features)
Estimated effects (of the feature in the column on the target in the row).
See also
IndepLassoCVA convenience class using the same interface as
CoopLassoCV(similarly formatted inputs and outputs) without sharing information among targets or features._CoopLassoAn internal class without cross-validation returning the lasso solution path. This is repeatedly called by
CoopLassoCV(once in each cross-validation iteration and once for the full dataset).
Examples
>>> from sklearn.datasets import load_linnerud >>> from collasso import CoopLassoCV >>> x, y = load_linnerud(return_X_y=True) >>> model = CoopLassoCV() >>> 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) CoopLassoCV#
Fit cross-validated model.
Fits cross-validated cooperative multi-task lasso regression.
- Parameters:
- Xndarray of shape (n_samples, p_features) or (n_samples, p_features, q_targets)
Common feature matrix for all targets or a specific 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 or each target.
- Returns:
- selfCoopLassoCV
Fitted model.
- predict(X: ndarray) ndarray#
Make predictions.
Make predictions from a cross-validated model obtained by cooperative multi-task lasso regression.
- Parameters:
- Xndarray of shape (n_samples, p_features) or (n_samples, p_features, q_targets)
Common feature matrix for all targets, or a specific feature matrix for each target.
- Returns:
- y_hatndarray of shape (n_samples, q_targets)
Matrix of predicted values (of the target in the column for the sample in the row).
- set_fit_request(*, Z: bool | None | str = '$UNCHANGED$') CoopLassoCV#
Configure whether metadata should be requested to be passed to the
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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
Zparameter infit.
- Returns:
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') CoopLassoCV#
Configure whether metadata should be requested to be passed to the
scoremethod.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(seesklearn.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 toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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_weightparameter inscore.
- Returns:
- selfobject
The updated object.