adapt.feature_based.FA
- class adapt.feature_based.FA(estimator=None, Xt=None, yt=None, copy=True, verbose=1, random_state=None, **params)[source]
FA: Feature Augmentation.
FA consists in a feature augmentation method where each input feature vector is augmented as follow:
Source input feature vectors Xs are transformed into (Xs, 0, Xs).
Target input feature vectors Xt are transformed into (0, Xt, Xt).
Where 0 refers to the null vector of same size as Xs and Xt.
The goal of this feature augmentation is to separate features into the three following classes:
Specific source features (first part of the augmented vector) which gives the specific behaviour on source domain.
Specific target features (second part of the augmented vector) which gives the specific behaviour on target domain.
General features (third part of the augmented vector) which have the same behaviour with respect to the task on both source and target domains.
This feature-based method uses a few labeled target data and belongs to “supervised” domain adaptation methods.
As FA consists only in a preprocessing step, any kind of estimator can be used to learn the task. This method handles both regression and classification tasks.
- Parameters
- estimatorsklearn estimator or tensorflow Model (default=None)
Estimator used to learn the task. If estimator is
None
, aLinearRegression
instance is used as estimator.- Xtnumpy array (default=None)
Target input data.
- ytnumpy array (default=None)
Target output data.
- estimatorsklearn estimator or tensorflow Model (default=None)
Estimator used to learn the task. If estimator is
None
, aLinearRegression
instance is used as estimator.- copyboolean (default=True)
Whether to make a copy of
estimator
or not.- verboseint (default=1)
Verbosity level.
- random_stateint (default=None)
Seed of random generator.
- copyboolean (default=True)
Whether to make a copy of
estimator
or not.- verboseint (default=1)
Verbosity level.
- random_stateint (default=None)
Seed of random generator.
- paramskey, value arguments
Arguments given at the different level of the adapt object. It can be, for instance, compile or fit parameters of the estimator or kernel parameters etc… Accepted parameters can be found by calling the method
_get_legal_params(params)
.
See also
Notes
FA can be used for multi-source DA by using the
domains
argument in thefit
orfit_transform
method. An example is given [here]References
- 1
[1] Daume III, H. “Frustratingly easy domain adaptation”. In ACL, 2007.
Examples
>>> from sklearn.linear_model import RidgeClassifier >>> from adapt.utils import make_classification_da >>> from adapt.feature_based import FA >>> Xs, ys, Xt, yt = make_classification_da() >>> model = FA(RidgeClassifier(), Xt=Xt[:10], yt=yt[:10], random_state=0) >>> model.fit(Xs, ys) Fit transform... Previous shape: (100, 2) New shape: (110, 6) Fit Estimator... >>> model.score(Xt, yt) 0.92
- Attributes
- estimator_object
Fitted estimator.
- n_domains_int
Number of domains given in fit.
Methods
__init__
([estimator, Xt, yt, copy, verbose, ...])fit
(X, y[, Xt, yt, domains])Fit Adapt Model.
fit_estimator
(X, y[, sample_weight, ...])Fit estimator on X, y.
fit_transform
(Xs, Xt, ys, yt[, domains])Fit embeddings.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X[, domain])Return estimator predictions after adaptation.
predict_estimator
(X, **predict_params)Return estimator predictions for X.
score
(X, y[, sample_weight, domain])Return the estimator score.
set_fit_request
(*[, domains])Request metadata passed to the
fit
method.set_params
(**params)Set the parameters of this estimator.
set_predict_request
(*[, domain])Request metadata passed to the
predict
method.set_score_request
(*[, domain, sample_weight])Request metadata passed to the
score
method.set_transform_request
(*[, domain])Request metadata passed to the
transform
method.transform
(X[, domain])Return augmented features for X.
unsupervised_score
(Xs, Xt)Return unsupervised score.
- __init__(estimator=None, Xt=None, yt=None, copy=True, verbose=1, random_state=None, **params)[source]
- fit(X, y, Xt=None, yt=None, domains=None, **fit_params)[source]
Fit Adapt Model.
For feature-based models, the transformation of the input features
Xs
andXt
is first fitted. In a second stage, theestimator_
is fitted on the transformed features.For instance-based models, source importance weights are first learned based on
Xs, ys
andXt
. In a second stage, theestimator_
is fitted onXs, ys
with the learned importance weights.- Parameters
- Xnumpy array
Source input data.
- ynumpy array
Source output data.
- Xtarray (default=None)
Target input data. If None, the Xt argument given in init is used.
- ytarray (default=None)
Target input data. Only needed for supervised and semi-supervised Adapt model. If None, the yt argument given in init is used.
- domainsarray (default=None)
Vector giving the domain for each source data. Can be used for multisource purpose.
- fit_paramskey, value arguments
Arguments given to the fit method of the estimator.
- Returns
- selfreturns an instance of self
- fit_estimator(X, y, sample_weight=None, random_state=None, warm_start=True, **fit_params)[source]
Fit estimator on X, y.
- Parameters
- Xarray
Input data.
- yarray
Output data.
- sample_weightarray
Importance weighting.
- random_stateint (default=None)
Seed of the random generator
- warm_startbool (default=True)
If True, continue to fit
estimator_
, else, a new estimator is fitted based on a copy ofestimator
. (Be sure to setcopy=True
to usewarm_start=False
)- fit_paramskey, value arguments
Arguments given to the fit method of the estimator and to the compile method for tensorflow estimator.
- Returns
- estimator_fitted estimator
- fit_transform(Xs, Xt, ys, yt, domains=None, **kwargs)[source]
Fit embeddings.
- Parameters
- Xsarray
Source input data.
- Xtarray
Target input data.
- ysarray
Source output data.
- ytarray
Target output data.
- domainsarray (default=None)
Vector giving the domain for each source data. Can be used for multisource purpose.
- kwargskey, value argument
Not used, present here for adapt consistency.
- Returns
- X_emb, yembedded input and output data
- get_metadata_routing()[source]
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)[source]
Get parameters for this estimator.
- Parameters
- deepbool, default=True
Not used, here for scikit-learn compatibility.
- Returns
- paramsdict
Parameter names mapped to their values.
- predict(X, domain=None, **predict_params)[source]
Return estimator predictions after adaptation.
For feature-based method (object which implements a
transform
method), the input featureX
are first transformed. Then thepredict
method of the fitted estimatorestimator_
is applied on the transformedX
.- Parameters
- Xarray
input data
- domainstr (default=None)
For antisymetric feature-based method, different transformation of the input X are applied for different domains. The domain should then be specified between “src” and “tgt”. If
None
the default transformation is the target one.
- Returns
- y_predarray
prediction of the Adapt Model.
- predict_estimator(X, **predict_params)[source]
Return estimator predictions for X.
- Parameters
- Xarray
input data
- Returns
- y_predarray
prediction of estimator.
- score(X, y, sample_weight=None, domain=None)[source]
Return the estimator score.
If the object has a
transform
method, the estimator is applied on the transformed features X. For antisymetric transformation, a parameter domain can be set to specified between source and target transformation.Call score on sklearn estimator and evaluate on tensorflow Model.
- Parameters
- Xarray
input data
- yarray
output data
- sample_weightarray (default=None)
Sample weights
- domainstr (default=None)
This parameter specifies for antisymetric feature-based method which transformation will be applied between “source” and “target”. If
None
the transformation by default is the target one.
- Returns
- scorefloat
estimator score.
- set_fit_request(*, domains: Union[bool, None, str] = '$UNCHANGED$') adapt.feature_based._fa.FA [source]
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if 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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
- domainsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domains
parameter infit
.
- Returns
- selfobject
The updated object.
- set_params(**params)[source]
Set the parameters of this estimator.
- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.
- set_predict_request(*, domain: Union[bool, None, str] = '$UNCHANGED$') adapt.feature_based._fa.FA [source]
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
- domainstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domain
parameter inpredict
.
- Returns
- selfobject
The updated object.
- set_score_request(*, domain: Union[bool, None, str] = '$UNCHANGED$', sample_weight: Union[bool, None, str] = '$UNCHANGED$') adapt.feature_based._fa.FA [source]
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if 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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
- domainstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domain
parameter inscore
.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns
- selfobject
The updated object.
- set_transform_request(*, domain: Union[bool, None, str] = '$UNCHANGED$') adapt.feature_based._fa.FA [source]
Request metadata passed to the
transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed totransform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it totransform
.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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
- domainstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domain
parameter intransform
.
- Returns
- selfobject
The updated object.
- transform(X, domain='tgt')[source]
Return augmented features for X.
In single source:
If
domain="src"
, the method returns the array (X, 0, X).If
domain="tgt"
, the method returns the array (0, X, X).
With 0 the array of same shape as X with zeros everywhere.
In single Multi-source:
If
domain="src_%i"%i
, the method returns the array (X, [X]*i, 0, [X]*(n_sources-i)).If
domain="tgt"
, the method returns the array (0, [X]*(n_sources+1)).
- Parameters
- Xarray
Input data.
- domainstr (default=”tgt”)
Choose between
"source", "src"
and"target", "tgt"
feature augmentation, or “src_0”, “src_1”, … in multisource setting.
- Returns
- X_embarray
Embeddings of X.
Notes
As FA is an anti-symetric feature-based method, one should indicates the domain of
X
in order to apply the appropriate feature transformation.