Skip to content

Ml base

Metalearner_Base ¤

Bases: ABC

An abstract base class for a Metalearner class.

Metalearner are similar to Aggregate functions, however Metalearners are models which require fitting before usage.

Metalearners are utilized in Stacking pipelines.

A Metalearner act as a combiner algorithm which is trained to make a final prediction using predictions of other algorithms (NeuralNetwork) as inputs.

Assembled predictions encoded in a NumPy matrix with shape (N_models, N_classes).
Example: [[0.5, 0.4, 0.1],
          [0.4, 0.3, 0.3],
          [0.5, 0.2, 0.3]]
-> shape (3, 3)

Merged prediction encoded in a NumPy matrix with shape (1, N_classes).
Example: [[0.4, 0.3, 0.3]]
-> shape (1, 3)

Required Functions

Function Description
__init__() Object creation function.
training() Fit Metalearner model.
prediction() Merge multiple class predictions into a single prediction.
dump() Store Metalearner model to disk.
load() Load Metalearner model from disk.
Source code in aucmedi/ensemble/metalearner/ml_base.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Metalearner_Base(ABC):
    """ An abstract base class for a Metalearner class.

    Metalearner are similar to [Aggregate functions][aucmedi.ensemble.aggregate],
    however Metalearners are models which require fitting before usage.

    Metalearners are utilized in [Stacking][aucmedi.ensemble.stacking] pipelines.

    A Metalearner act as a combiner algorithm which is trained to make a final prediction
    using predictions of other algorithms (`NeuralNetwork`) as inputs.

    ```
    Assembled predictions encoded in a NumPy matrix with shape (N_models, N_classes).
    Example: [[0.5, 0.4, 0.1],
              [0.4, 0.3, 0.3],
              [0.5, 0.2, 0.3]]
    -> shape (3, 3)

    Merged prediction encoded in a NumPy matrix with shape (1, N_classes).
    Example: [[0.4, 0.3, 0.3]]
    -> shape (1, 3)
    ```

    !!! info "Required Functions"
        | Function            | Description                                                |
        | ------------------- | ---------------------------------------------------------- |
        | `__init__()`        | Object creation function.                                  |
        | `training()`        | Fit Metalearner model.                                     |
        | `prediction()`      | Merge multiple class predictions into a single prediction. |
        | `dump()`            | Store Metalearner model to disk.                           |
        | `load()`            | Load Metalearner model from disk.                          |
    """
    #---------------------------------------------#
    #                Initialization               #
    #---------------------------------------------#
    @abstractmethod
    def __init__(self):
        """ Initialization function which will be called during the Metalearner object creation.

        This function can be used to pass variables and options in the Metalearner instance.
        There are no mandatory parameters for the initialization.
        """
        pass

    #---------------------------------------------#
    #                  Training                   #
    #---------------------------------------------#
    @abstractmethod
    def train(self, x, y):
        """ Training function to fit the Metalearner model.

        Args:
            x (numpy.ndarray):          Assembled prediction dataset encoded in a NumPy matrix with shape (N_samples, N_classes*N_models).
            y (numpy.ndarray):          Classification list with One-Hot Encoding. Provided by
                                        [input_interface][aucmedi.data_processing.io_data.input_interface].
        """
        pass

    #---------------------------------------------#
    #                  Prediction                 #
    #---------------------------------------------#
    @abstractmethod
    def predict(self, data):
        """ Merge multiple predictions for a sample into a single prediction.

        It is required to return the merged predictions (as NumPy matrix).
        It is possible to pass configurations through the initialization function for this class.

        Args:
            data (numpy.ndarray):       Assembled predictions encoded in a NumPy matrix with shape (N_models, N_classes).
        Returns:
            pred (numpy.ndarray):       Merged prediction encoded in a NumPy matrix with shape (1, N_classes).
        """
        pass

    #---------------------------------------------#
    #              Dump Model to Disk             #
    #---------------------------------------------#
    @abstractmethod
    def dump(self, path):
        """ Store Metalearner model to disk.

        Args:
            path (str):                 Path to store the model on disk.
        """
        pass

    #---------------------------------------------#
    #             Load Model from Disk            #
    #---------------------------------------------#
    @abstractmethod
    def load(self, path):
        """ Load Metalearner model and its weights from a file.

        Args:
            path (str):                 Input path from which the model will be loaded.
        """
        pass

__init__() abstractmethod ¤

Initialization function which will be called during the Metalearner object creation.

This function can be used to pass variables and options in the Metalearner instance. There are no mandatory parameters for the initialization.

Source code in aucmedi/ensemble/metalearner/ml_base.py
63
64
65
66
67
68
69
70
@abstractmethod
def __init__(self):
    """ Initialization function which will be called during the Metalearner object creation.

    This function can be used to pass variables and options in the Metalearner instance.
    There are no mandatory parameters for the initialization.
    """
    pass

dump(path) abstractmethod ¤

Store Metalearner model to disk.

Parameters:

Name Type Description Default
path str

Path to store the model on disk.

required
Source code in aucmedi/ensemble/metalearner/ml_base.py
106
107
108
109
110
111
112
113
@abstractmethod
def dump(self, path):
    """ Store Metalearner model to disk.

    Args:
        path (str):                 Path to store the model on disk.
    """
    pass

load(path) abstractmethod ¤

Load Metalearner model and its weights from a file.

Parameters:

Name Type Description Default
path str

Input path from which the model will be loaded.

required
Source code in aucmedi/ensemble/metalearner/ml_base.py
118
119
120
121
122
123
124
125
@abstractmethod
def load(self, path):
    """ Load Metalearner model and its weights from a file.

    Args:
        path (str):                 Input path from which the model will be loaded.
    """
    pass

predict(data) abstractmethod ¤

Merge multiple predictions for a sample into a single prediction.

It is required to return the merged predictions (as NumPy matrix). It is possible to pass configurations through the initialization function for this class.

Parameters:

Name Type Description Default
data numpy.ndarray

Assembled predictions encoded in a NumPy matrix with shape (N_models, N_classes).

required

Returns:

Name Type Description
pred numpy.ndarray

Merged prediction encoded in a NumPy matrix with shape (1, N_classes).

Source code in aucmedi/ensemble/metalearner/ml_base.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@abstractmethod
def predict(self, data):
    """ Merge multiple predictions for a sample into a single prediction.

    It is required to return the merged predictions (as NumPy matrix).
    It is possible to pass configurations through the initialization function for this class.

    Args:
        data (numpy.ndarray):       Assembled predictions encoded in a NumPy matrix with shape (N_models, N_classes).
    Returns:
        pred (numpy.ndarray):       Merged prediction encoded in a NumPy matrix with shape (1, N_classes).
    """
    pass

train(x, y) abstractmethod ¤

Training function to fit the Metalearner model.

Parameters:

Name Type Description Default
x numpy.ndarray

Assembled prediction dataset encoded in a NumPy matrix with shape (N_samples, N_classes*N_models).

required
y numpy.ndarray

Classification list with One-Hot Encoding. Provided by input_interface.

required
Source code in aucmedi/ensemble/metalearner/ml_base.py
75
76
77
78
79
80
81
82
83
84
@abstractmethod
def train(self, x, y):
    """ Training function to fit the Metalearner model.

    Args:
        x (numpy.ndarray):          Assembled prediction dataset encoded in a NumPy matrix with shape (N_samples, N_classes*N_models).
        y (numpy.ndarray):          Classification list with One-Hot Encoding. Provided by
                                    [input_interface][aucmedi.data_processing.io_data.input_interface].
    """
    pass