adapt.instance_based.IWC
- classadapt.instance_based.IWC(estimator=None,Xt=None,yt=None,classifier=None,cl_params=None,copy=True,verbose=1,random_state=None,**params)[source]
IWC: Importance Weighting Classifier
Importance weighting based on the output of a domain classifierwhich discriminates between source and target data.
The source importance weighting are given with the following formula:
\[w(x) = \frac{1}{P(x \in Source)} - 1\]- Parameters
- estimatorsklearn estimator or tensorflow Model (default=None)
Estimator used to learn the task. If estimator is
None, aLinearRegressioninstance is used as estimator.- Xtnumpy array (default=None)
Target input data.
- classifierobject (default=None)
Binary classifier trained to discriminatebetween source and target data.
- cl_paramsdict (default=None)
Dictionnary of parameters that willbe given in thefit and/orcompile methodsof the classifier.
- copyboolean (default=True)
Whether to make a copy of
estimatoror 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 theestimator or kernel parameters etc…Accepted parameters can be found by calling the method
_get_legal_params(params).
See also
References
- 1
[1] Steffen Bickel, Michael Bruckner, Tobias Scheffer. “Discriminative Learning for Differing Training and Test Distributions”. In ICML 2007
Examples
>>>fromsklearn.linear_modelimportRidgeClassifier>>>fromadapt.utilsimportmake_classification_da>>>fromadapt.instance_basedimportIWC>>>Xs,ys,Xt,yt=make_classification_da()>>>model=IWC(RidgeClassifier(0.),classifier=RidgeClassifier(0.),...Xt=Xt,random_state=0)>>>model.fit(Xs,ys);>>>model.score(Xt,yt)0.74
- Attributes
- classifier_object
Fitted classifier.
- estimator_object
Fitted estimator.
Methods
__init__([estimator, Xt, yt, classifier, ...])fit(X, y[, Xt, yt, domains])Fit Adapt Model.
fit_estimator(X, y[, sample_weight, ...])Fit estimator on X, y.
fit_weights(Xs, Xt[, warm_start])Fit importance weighting.
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.
predict_weights([X])Return fitted source weights
score(X, y[, sample_weight, domain])Return the estimator score.
set_fit_request(*[, domains])Request metadata passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_predict_request(*[, domain])Request metadata passed to the
predictmethod.set_score_request(*[, domain, sample_weight])Request metadata passed to the
scoremethod.unsupervised_score(Xs, Xt)Return unsupervised score.
- __init__(estimator=None,Xt=None,yt=None,classifier=None,cl_params=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 theinput features
XsandXtis first fitted. In a secondstage, theestimator_is fitted on the transformed features.For instance-based models, source importance weights arefirst learned based on
Xs,ysandXt. In a secondstage, theestimator_is fitted onXs,yswith the learnedimportance weights.- Parameters
- Xnumpy array
Source input data.
- ynumpy array
Source output data.
- Xtarray (default=None)
Target input data. If None, theXt argumentgiven ininit is used.
- ytarray (default=None)
Target input data. Only needed for supervisedand semi-supervised Adapt model.If None, theyt argument given ininit is used.
- domainsarray (default=None)
Vector giving the domain for each sourcedata. Can be used for multisource purpose.
- fit_paramskey, value arguments
Arguments given to the fit method ofthe 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 ona copy ofestimator. (Be sure to setcopy=Trueto usewarm_start=False)- fit_paramskey, value arguments
Arguments given to the fit method ofthe estimator and to the compile methodfor tensorflow estimator.
- Returns
- estimator_fitted estimator
- fit_weights(Xs,Xt,warm_start=False,**kwargs)[source]
Fit importance weighting.
- Parameters
- Xsarray
Input source data.
- Xtarray
Input target data.
- warm_startbool (default=False)
Weither to train the domain classifierfrom scratch or not.If False, the classifier is trained from scratch.
- kwargskey, value argument
Not used, present here for adapt consistency.
- Returns
- weights_sample weights
- get_metadata_routing()[source]
Get metadata routing of this object.
Please checkUser Guide on how the routingmechanism works.
- Returns
- routingMetadataRequest
A
MetadataRequestencapsulatingrouting 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 afteradaptation.
For feature-based method (object which implementsa
transformmethod), the input featureXare first transformed. Then thepredictmethodof the fitted estimatorestimator_is appliedon the transformedX.- Parameters
- Xarray
input data
- domainstr (default=None)
For antisymetric feature-based method,different transformation of the input Xare applied for different domains. The domainshould then be specified between “src” and “tgt”.If
Nonethe default transformation is thetarget 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.
- predict_weights(X=None)[source]
Return fitted source weights
If
None, the fitted source weights are returned.Else, sample weights are computing using the fittedclassifier_.- Parameters
- Xarray (default=None)
Input data.
- Returns
- weights_sample weights
- score(X,y,sample_weight=None,domain=None)[source]
Return the estimator score.
If the object has a
transformmethod, theestimator is applied on the transformedfeatures X. For antisymetric transformation,a parameter domain can be set to specifiedbetween source and target transformation.Callscore on sklearn estimator andevaluate on tensorflow Model.
- Parameters
- Xarray
input data
- yarray
output data
- sample_weightarray (default=None)
Sample weights
- domainstr (default=None)
This parameter specifies for antisymetricfeature-based method which transformationwill be applied between “source” and “target”.If
Nonethe transformation by default isthe target one.
- Returns
- scorefloat
estimator score.
- set_fit_request(*,domains:Union[bool,None,str]='$UNCHANGED$')→adapt.instance_based._iwc.IWC[source]
Request metadata passed to the
fitmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()).Please seeUser Guide on how the routingmechanism 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 theexisting request. This allows you to change the request for someparameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as asub-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
domainsparameter 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.instance_based._iwc.IWC[source]
Request metadata passed to the
predictmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()).Please seeUser Guide on how the routingmechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif 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 theexisting request. This allows you to change the request for someparameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as asub-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
domainparameter inpredict.
- Returns
- selfobject
The updated object.
- set_score_request(*,domain:Union[bool,None,str]='$UNCHANGED$',sample_weight:Union[bool,None,str]='$UNCHANGED$')→adapt.instance_based._iwc.IWC[source]
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()).Please seeUser Guide on how the routingmechanism 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 theexisting request. This allows you to change the request for someparameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as asub-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
domainparameter inscore.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns
- selfobject
The updated object.