adapt.instance_based.KMM

class adapt.instance_based.KMM(estimator=None, Xt=None, kernel='rbf', B=1000, eps=None, max_size=1000, tol=None, max_iter=100, copy=True, verbose=1, random_state=None, **params)[source]

KMM: Kernel Mean Matching

KMM is a sample bias correction method for domain adaptation based on the minimization of the Maximum Mean Discrepancy (MMD) between source and target domains.

The algorithm corrects the difference between the input source and target distributions by reweighting the source instances such that the means of the source and target instances in a reproducing kernel Hilbert space (RKHS) are “close”.

This leads to solve the following quadratic optimization problem:

\[\min_{w} \frac{1}{2} w^T K w - \kappa^T w\]

Subject to:

\[w_i \in [0, B] \text{ and } |\sum_{i=1}^{n_S} w_i - n_S| \leq m \epsilon\]

Where:

  • \(K_{ij} = k(x_i, x_j)\) with \(x_i, x_j \in X_S\) and \(k\) a kernel.

  • \(\kappa_{i} = \frac{n_S}{n_T} \sum_{x_j \in X_T} k(x_i, x_j)\) with \(x_i \in X_S\).

  • \(w_i\) are the source instance weights.

  • \(X_S, X_T\) are respectively the input source and target dataset.

  • \(B, \epsilon\) are two KMM hyperparameters.

After solving the above OP, an estimator is fitted using the reweighted labeled source instances.

KMM method has been originally introduced for unsupervised DA but it could be widen to supervised by simply adding labeled target data to the training set.

Parameters
estimatorsklearn estimator or tensorflow Model (default=None)

Estimator used to learn the task. If estimator is None, a LinearRegression instance is used as estimator.

Xtnumpy array (default=None)

Target input data.

kernelstr (default=”rbf”)

Kernel metric. Possible values: [‘additive_chi2’, ‘chi2’, ‘linear’, ‘poly’, ‘polynomial’, ‘rbf’, ‘laplacian’, ‘sigmoid’, ‘cosine’]

B: float (default=1000)

Bounding weights parameter.

eps: float, optional (default=None)

Constraint parameter. If None, eps is set to:

eps = (np.sqrt(len(Xs)) - 1)/np.sqrt(len(Xs))

with Xs the source input dataset.

max_sizeint (default=1000)

Batch computation to speed up the fitting. If len(Xs) > max_size, KMM is applied successively on seperated batch of size lower than max_size.

tol: float (default=None)

Optimization threshold. If None default parameters from cvxopt are used.

max_iter: int (default=100)

Maximal iteration of the optimization.

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).

Yields
gammafloat

Kernel parameter gamma.

  • For kernel = chi2:

    k(x, y) = exp(-gamma Sum [(x - y)^2 / (x + y)])
    
  • For kernel = poly or polynomial:

    K(X, Y) = (gamma <X, Y> + coef0)^degree
    
  • For kernel = rbf:

    K(x, y) = exp(-gamma ||x-y||^2)
    
  • For kernel = laplacian:

    K(x, y) = exp(-gamma ||x-y||_1)
    
  • For kernel = sigmoid:

    K(X, Y) = tanh(gamma <X, Y> + coef0)
    
coef0floaf

Kernel parameter coef0. Used for ploynomial and sigmoid kernels. See gamma parameter above for the kernel formulas.

degreeint

Degree parameter for the polynomial kernel. (see formula in the gamma parameter description)

See also

KLIEP

References

1

[1] J. Huang, A. Gretton, K. Borgwardt, B. Schölkopf, and A. J. Smola. “Correcting sample selection bias by unlabeled data.” In NIPS, 2007.

Examples

>>> from sklearn.linear_model import RidgeClassifier
>>> from adapt.utils import make_classification_da
>>> from adapt.instance_based import KMM
>>> Xs, ys, Xt, yt = make_classification_da()
>>> model = KMM(RidgeClassifier(), Xt=Xt, kernel="rbf", gamma=1., verbose=0, random_state=0)
>>> model.fit(Xs, ys)
>>> model.score(Xt, yt)
0.76
Attributes
weights_numpy array

Training instance weights.

estimator_object

Estimator.

Methods

__init__([estimator, Xt, kernel, B, eps, ...])

fit(X, y[, Xt, yt, domains])

Fit Adapt Model.

fit_estimator(X, y[, sample_weight, ...])

Fit estimator on X, y.

fit_weights(Xs, Xt, **kwargs)

Fit importance weighting.

get_metadata_routing()

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()

Return fitted source weights

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.

unsupervised_score(Xs, Xt)

Return unsupervised score.

__init__(estimator=None, Xt=None, kernel='rbf', B=1000, eps=None, max_size=1000, tol=None, max_iter=100, 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 and Xt is first fitted. In a second stage, the estimator_ is fitted on the transformed features.

For instance-based models, source importance weights are first learned based on Xs, ys and Xt. In a second stage, the estimator_ is fitted on Xs, 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 of estimator. (Be sure to set copy=True to use warm_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_weights(Xs, Xt, **kwargs)[source]

Fit importance weighting.

Parameters
Xsarray

Input source data.

Xtarray

Input target data.

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 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 feature X are first transformed. Then the predict method of the fitted estimator estimator_ is applied on the transformed X.

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.

predict_weights()[source]

Return fitted source weights

Returns
weights_sample weights
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.instance_based._kmm.KMM[source]

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

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 in fit.

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._kmm.KMM[source]

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict 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 predict.

  • 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 in predict.

Returns
selfobject

The updated object.

set_score_request(*, domain: Union[bool, None, str] = '$UNCHANGED$', sample_weight: Union[bool, None, str] = '$UNCHANGED$') adapt.instance_based._kmm.KMM[source]

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

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 in score.

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.

unsupervised_score(Xs, Xt)[source]

Return unsupervised score.

The normalized discrepancy distance is computed between the reweighted/transformed source input data and the target input data.

Parameters
Xsarray

Source input data.

Xtarray

Source input data.

Returns
scorefloat

Unsupervised score.

Examples