adapt.parameter_based.TransferTreeClassifier

class adapt.parameter_based.TransferTreeClassifier(estimator=None, Xt=None, yt=None, algo='', copy=True, verbose=1, random_state=None, **params)[source]

TransferTreeClassifier: Modify a source Decision tree on a target dataset.

Parameters
estimatorsklearn DecsionTreeClassifier (default=None)

Source decision tree classifier.

Xtnumpy array (default=None)

Target input data.

ytnumpy array (default=None)

Target output data.

algostr or callable (default=””)

Leaves relabeling if “” or “relab”. “ser” and “strut” for SER and STRUT algorithms

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.

References

1

[1] Segev, Noam and Harel, Maayan Mannor, Shie and Crammer, Koby and El-Yaniv, Ran “Learn on Source, Refine on Target: A Model Transfer Learning Framework with Random Forests”. In IEEE TPAMI, 2017.

2

[2] Minvielle, Ludovic and Atiq, Mounir Peignier, Sergio and Mougeot, Mathilde “Transfer Learning on Decision Tree with Class Imbalance”. In IEEE ICTAI, 2019.

Examples

>>> from adapt.utils import make_classification_da
>>> from sklearn.tree import DecisionTreeClassifier
>>> from adapt.parameter_based import TransferTreeClassifier
>>> Xs, ys, Xt, yt = make_classification_da()
>>> src_model = DecisionTreeClassifier().fit(Xs, ys)
>>> src_model.score(Xt, yt)
0.62
>>> tgt_model = TransferTreeClassifier(src_model)
>>> tgt_model.fit(Xt[[1, -1]], yt[[1, -1]])
>>> tgt_model.score(Xt, yt)
0.92
Attributes
estimator_sklearn DecsionTreeClassifier

Transferred decision tree classifier using target data.

parentsnumpy array of int.
bool_parents_lrnumpy array of {-1,0,1} values.
pathsnumpy array of int arrays.
rulesnumpy array of 3-tuple arrays.
depthsnumpy array of int.

Methods

__init__([estimator, Xt, yt, algo, copy, ...])

extend(node, subtree)

Extend the underlying decision tree estimator by a sub-tree at a given node.

fit([Xt, yt])

Fit TransferTreeClassifier.

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

Fit estimator on X, y.

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.

prune(node[, include_node, lr, leaf_value])

Pruning the corresponding sub-tree at a given node.

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.

swap_subtrees(node1, node2)

Swap respective sub-trees between two given nodes.

unsupervised_score(Xs, Xt)

Return unsupervised score.

updateSplit(node, feature, threshold)

Update the (feature,threshold) split for a given node.

updateValue(node, values)

Update class values for a given node.

__init__(estimator=None, Xt=None, yt=None, algo='', copy=True, verbose=1, random_state=None, **params)[source]
extend(node, subtree)[source]

Extend the underlying decision tree estimator by a sub-tree at a given node.

Parameters
nodeint

Node to update.

subtreeDecisionTreeClassifier.
fit(Xt=None, yt=None, **fit_params)[source]

Fit TransferTreeClassifier.

Parameters
Xtnumpy array (default=None)

Target input data.

ytnumpy array (default=None)

Target output data.

fit_paramskey, value arguments

Not used. Here for sklearn compatibility.

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

prune(node, include_node=False, lr=0, leaf_value=None)[source]

Pruning the corresponding sub-tree at a given node.

If include_node is False, replaces the node by a leaf (with values leaf_values if provided). If include_node is True, prunes the left (lr=-1) or (lr=1) child sub-tree and replaces the given node by the other sub-tree.

Parameters
nodeint

Node to prune.

include_nodeboolean (default=False)

Type of pruning to apply.

lrfloat

Direction of pruning if include_node is True. Must be either -1 (left) or 1 (right) in this case.

leaf_valuenumpy array of float (default=None)

If include_node is False, affects these values to the created leaf.

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.parameter_based._transfer_tree.TransferTreeClassifier[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.parameter_based._transfer_tree.TransferTreeClassifier[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.parameter_based._transfer_tree.TransferTreeClassifier[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.

swap_subtrees(node1, node2)[source]

Swap respective sub-trees between two given nodes.

Each node must not be a sub-node of the other. Update the (feature,threshold) split for a given node.

Parameters
node1int

Node to swap.

node2int

Node to swap.

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.

updateSplit(node, feature, threshold)[source]

Update the (feature,threshold) split for a given node.

Parameters
nodeint

Node to update.

featureint

New node feature.

thresholdfloat

New node threshold.

updateValue(node, values)[source]

Update class values for a given node.

Parameters
nodeint

Node to update.

valuesnumpy array of float

Class values to affect at node.

Examples