Skip to content

Documentation for Branch

A substream of pipeline elements that is encapsulated, e.g. for parallelization.

Examples:

1
2
3
4
5
6
7
8
from photonai.base import Branch
from photonai.optimization import IntegerRange

tree_qua_branch = Branch('tree_branch')
tree_qua_branch += PipelineElement('QuantileTransformer', n_quantiles=100)
tree_qua_branch += PipelineElement('DecisionTreeClassifier',
                                   {'min_samples_split': IntegerRange(2, 4)},
                                   criterion='gini')

__iadd__(self, pipe_element) special

Add an element to the sub pipeline.

Parameters:

Name Type Description Default
pipe_element PipelineElement

The PipelineElement to add, being either a transformer or an estimator.

required
Source code in photonai/base/photon_elements.py
def __iadd__(self, pipe_element: PipelineElement):
    """
    Add an element to the sub pipeline.

    Parameters:
        pipe_element:
            The PipelineElement to add, being either a transformer or an estimator.

    """
    super(Branch, self).__iadd__(pipe_element)
    self._prepare_pipeline()
    return self

__init__(self, name, elements=None) special

Initialize the object.

Parameters:

Name Type Description Default
name str

Name of the encapsulated item and/or summary of the encapsulated element`s functions.

required
elements List[photonai.base.photon_elements.PipelineElement]

List of PipelineElements added one after another to the Branch.

None
Source code in photonai/base/photon_elements.py
def __init__(self, name: str, elements: List[PipelineElement] = None):
    """
    Initialize the object.

    Parameters:
        name:
            Name of the encapsulated item and/or
            summary of the encapsulated element`s functions.

        elements:
            List of PipelineElements added one after another to the Branch.

    """
    super().__init__(name, {}, test_disabled=False, disabled=False, base_element=True)

    # in case any of the children needs y or covariates we need to request them
    self.needs_y = True
    self.needs_covariates = True
    self.elements = []
    self.has_hyperparameters = True
    self.skip_caching = True
    self.identifier = "BRANCH:"

    # needed for caching on individual level
    self.fix_fold_id = False
    self.do_not_delete_cache_folder = False

    # add elements
    if elements:
        for element in elements:
            self.add(element)

add(self, pipe_element)

Add an element to the sub pipeline.

Parameters:

Name Type Description Default
pipe_element PipelineElement

The PipelineElement to add, being either a transformer or an estimator.

required
Source code in photonai/base/photon_elements.py
def add(self, pipe_element: PipelineElement):
    """
    Add an element to the sub pipeline.

    Parameters:
        pipe_element:
            The PipelineElement to add, being either a transformer or an estimator.

    """
    self.__iadd__(pipe_element)

fit(self, X, y=None, **kwargs)

Calls the fit function on all underlying base elements.

Parameters:

Name Type Description Default
X ndarray

The array-like input with shape=[N, D], where N is the number of samples and D is the number of features.

required
y ndarray

The truth array-like values with shape=[N], where N is the number of samples.

None
**kwargs

Keyword arguments, passed to base_elements fit.

{}

Returns:

Type Description

Fitted self.

Source code in photonai/base/photon_elements.py
def fit(self, X: np.ndarray, y: np.ndarray = None, **kwargs):
    """
    Calls the fit function on all underlying base elements.

    Parameters:
        X:
            The array-like input with shape=[N, D], where N is
            the number of samples and D is the number of features.

        y:
            The truth array-like values with shape=[N],
            where N is the number of samples.

        **kwargs:
            Keyword arguments, passed to base_elements fit.

    Returns:
        Fitted self.

    """
    self.base_element = Branch.sanity_check_pipeline(self.base_element)
    return super().fit(X, y, **kwargs)

predict(self, X, **kwargs)

Calls the predict function on underlying base elements.

Parameters:

Name Type Description Default
X ndarray

The array-like data with shape=[N, D], where N is the number of samples and D is the number of features.

required
**kwargs

Keyword arguments, passed to base_elements predict method.

{}

Returns:

Type Description
ndarray

Prediction values.

Source code in photonai/base/photon_elements.py
def predict(self, X: np.ndarray, **kwargs) -> np.ndarray:
    """
    Calls the predict function on underlying base elements.

    Parameters:
        X:
            The array-like data with shape=[N, D], where N is the
            number of samples and D is the number of features.

        **kwargs:
            Keyword arguments, passed to base_elements predict method.

    Returns:
        Prediction values.

    """
    return super().predict(X, **kwargs)

transform(self, X, y=None, **kwargs)

Calls the transform function on all underlying base elements. If _estimator_type is in ['classifier', 'regressor'], predict is called instead.

Parameters:

Name Type Description Default
X ndarray

The array-like data with shape=[N, D], where N is the number of samples and D is the number of features.

required
y ndarray

The truth array-like values with shape=[N], where N is the number of samples.

None
**kwargs

Keyword arguments, passed to base_elements predict/transform.

{}

Returns:

Type Description
(<class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'dict'>)

Transformed/Predicted data.

Source code in photonai/base/photon_elements.py
def transform(self, X: np.ndarray, y: np.ndarray = None, **kwargs) -> (np.ndarray, np.ndarray, dict):
    """
    Calls the transform function on all underlying base elements.
    If _estimator_type is in ['classifier', 'regressor'], predict is called instead.

    Parameters:
        X:
            The array-like data with shape=[N, D], where N is the
            number of samples and D is the number of features.

        y:
            The truth array-like values with shape=[N],
            where N is the number of samples.

        **kwargs:
            Keyword arguments, passed to base_elements predict/transform.

    Returns:
        Transformed/Predicted data.

    """
    if self._estimator_type == 'classifier' or self._estimator_type == 'regressor':
        return super().predict(X), y, kwargs
    return super().transform(X, y, **kwargs)