Skip to content

Predictions

Predictions

Bases: dict

A dictionary-like class for storing predictions generated by a model on different data splits.

Inherits from the built-in dict class.

Methods: - create_from_model_dataset: Creates a Predictions object from a model and dataset.

Attributes: - pred: A dictionary that stores the predictions for each data split.

Source code in model_forge/data/predictions.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Predictions(dict):
    """
    A dictionary-like class for storing predictions generated by a model on different data splits.

    Inherits from the built-in `dict` class.

    Methods:
    - create_from_model_dataset: Creates a `Predictions` object from a model and dataset.

    Attributes:
    - pred: A dictionary that stores the predictions for each data split.

    """

    @classmethod
    def create_from_model_dataset(cls, model, dataset):
        """
        Creates a `Predictions` object from a model and dataset.

        Args:
        - model: The model object used for making predictions.
        - dataset: The dataset object containing the data splits.

        Returns:
        - A `Predictions` object containing the predictions for each data split.

        Raises:
        - AttributeError: If the model does not have a 'predict' attribute.

        """
        pred = {}
        for split, (X, _) in dataset:
            if hasattr(model, "predict") and hasattr(model, "predict_proba"):
                pred[split] = (model.predict(X), model.predict_proba(X))
            elif hasattr(model, "predict"):
                pred[split] = (model.predict(X), None)
            else:
                raise AttributeError("Model does not have 'predict' attribute.")
        return cls(pred)

create_from_model_dataset(model, dataset) classmethod

Creates a Predictions object from a model and dataset.

Args: - model: The model object used for making predictions. - dataset: The dataset object containing the data splits.

Returns: - A Predictions object containing the predictions for each data split.

Raises: - AttributeError: If the model does not have a 'predict' attribute.

Source code in model_forge/data/predictions.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@classmethod
def create_from_model_dataset(cls, model, dataset):
    """
    Creates a `Predictions` object from a model and dataset.

    Args:
    - model: The model object used for making predictions.
    - dataset: The dataset object containing the data splits.

    Returns:
    - A `Predictions` object containing the predictions for each data split.

    Raises:
    - AttributeError: If the model does not have a 'predict' attribute.

    """
    pred = {}
    for split, (X, _) in dataset:
        if hasattr(model, "predict") and hasattr(model, "predict_proba"):
            pred[split] = (model.predict(X), model.predict_proba(X))
        elif hasattr(model, "predict"):
            pred[split] = (model.predict(X), None)
        else:
            raise AttributeError("Model does not have 'predict' attribute.")
    return cls(pred)