Skip to content

Selectors

AbstractPredictor

Abstract base class for all predictors.

Methods

fit(X, Y) Fit the model to the data. predict(X) Predict using the model. save(file_path) Save the model to a file. load(file_path) Load the model from a file.

Source code in asf/predictors/abstract_predictor.py
 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
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
class AbstractPredictor:
    """
    Abstract base class for all predictors.

    Methods
    -------
    fit(X, Y)
        Fit the model to the data.
    predict(X)
        Predict using the model.
    save(file_path)
        Save the model to a file.
    load(file_path)
        Load the model from a file.
    """

    def __init__(self):
        """
        Initialize the predictor.
        """
        pass

    def fit(self, X: Any, Y: Any):
        """
        Fit the model to the data.

        Parameters
        ----------
        X : array-like
            Training data.
        Y : array-like
            Target values.
        """
        pass

    def predict(self, X: Any) -> Any:
        """
        Predict using the model.

        Parameters
        ----------
        X : array-like
            Data to predict on.

        Returns
        -------
        array-like
            Predicted values.
        """
        pass

    def save(self, file_path: str):
        """
        Save the model to a file.

        Parameters
        ----------
        file_path : str
            Path to the file where the model will be saved.
        """
        pass

    def load(self, file_path: str):
        """
        Load the model from a file.

        Parameters
        ----------
        file_path : str
            Path to the file from which the model will be loaded.
        """
        pass

__init__()

Initialize the predictor.

Source code in asf/predictors/abstract_predictor.py
20
21
22
23
24
def __init__(self):
    """
    Initialize the predictor.
    """
    pass

fit(X, Y)

Fit the model to the data.

Parameters

X : array-like Training data. Y : array-like Target values.

Source code in asf/predictors/abstract_predictor.py
26
27
28
29
30
31
32
33
34
35
36
37
def fit(self, X: Any, Y: Any):
    """
    Fit the model to the data.

    Parameters
    ----------
    X : array-like
        Training data.
    Y : array-like
        Target values.
    """
    pass

load(file_path)

Load the model from a file.

Parameters

file_path : str Path to the file from which the model will be loaded.

Source code in asf/predictors/abstract_predictor.py
66
67
68
69
70
71
72
73
74
75
def load(self, file_path: str):
    """
    Load the model from a file.

    Parameters
    ----------
    file_path : str
        Path to the file from which the model will be loaded.
    """
    pass

predict(X)

Predict using the model.

Parameters

X : array-like Data to predict on.

Returns

array-like Predicted values.

Source code in asf/predictors/abstract_predictor.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def predict(self, X: Any) -> Any:
    """
    Predict using the model.

    Parameters
    ----------
    X : array-like
        Data to predict on.

    Returns
    -------
    array-like
        Predicted values.
    """
    pass

save(file_path)

Save the model to a file.

Parameters

file_path : str Path to the file where the model will be saved.

Source code in asf/predictors/abstract_predictor.py
55
56
57
58
59
60
61
62
63
64
def save(self, file_path: str):
    """
    Save the model to a file.

    Parameters
    ----------
    file_path : str
        Path to the file where the model will be saved.
    """
    pass

EPMRandomForest

Bases: ForestRegressor, AbstractPredictor

Implementation of random forest as done in the paper "Algorithm runtime prediction: Methods & evaluation" by Hutter, Xu, Hoos, and Leyton-Brown (2014).

Methods

fit(X, Y) Fit the model to the data. predict(X) Predict using the model. save(file_path) Save the model to a file. load(file_path) Load the model from a file.

Source code in asf/predictors/epm_random_forest.py
  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
 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class EPMRandomForest(ForestRegressor, AbstractPredictor):
    """
    Implementation of random forest as done in the paper
    "Algorithm runtime prediction: Methods & evaluation" by Hutter, Xu, Hoos, and Leyton-Brown (2014).

    Methods
    -------
    fit(X, Y)
        Fit the model to the data.
    predict(X)
        Predict using the model.
    save(file_path)
        Save the model to a file.
    load(file_path)
        Load the model from a file.
    """

    def __init__(
        self,
        n_estimators: int = 100,
        *,
        log=False,
        cross_trees_variance=False,
        criterion="squared_error",
        splitter="random",
        max_depth=None,
        min_samples_split=2,
        min_samples_leaf=1,
        min_weight_fraction_leaf=0.0,
        max_features=1.0,
        max_leaf_nodes=None,
        min_impurity_decrease=0.0,
        bootstrap: bool = False,
        oob_score: bool = False,
        n_jobs=None,
        random_state=None,
        verbose: int = 0,
        warm_start: bool = False,
        ccp_alpha=0.0,
        max_samples=None,
        monotonic_cst=None,
    ) -> None:
        super().__init__(
            DecisionTreeRegressor(),
            n_estimators,
            estimator_params=(
                "criterion",
                "max_depth",
                "min_samples_split",
                "min_samples_leaf",
                "min_weight_fraction_leaf",
                "max_features",
                "max_leaf_nodes",
                "min_impurity_decrease",
                "random_state",
                "ccp_alpha",
                "monotonic_cst",
            ),
            bootstrap=bootstrap,
            oob_score=oob_score,
            n_jobs=n_jobs,
            random_state=random_state,
            verbose=verbose,
            warm_start=warm_start,
            max_samples=max_samples,
        )
        self.criterion = criterion
        self.max_depth = max_depth
        self.min_samples_split = min_samples_split
        self.min_samples_leaf = min_samples_leaf
        self.min_weight_fraction_leaf = min_weight_fraction_leaf
        self.max_features = max_features
        self.max_leaf_nodes = max_leaf_nodes
        self.min_impurity_decrease = min_impurity_decrease
        self.ccp_alpha = ccp_alpha
        self.monotonic_cst = monotonic_cst
        self.splitter = splitter
        self.log = log
        self.cross_trees_variance = cross_trees_variance  # TODO use this

    def fit(self, X, y, sample_weight=None):
        """
        Fit the model to the data.

        Parameters
        ----------
        X : array-like
            Training data.
        y : array-like
            Target values.
        """
        assert sample_weight is None, "Sample weights are not supported"
        super().fit(X=X, y=y, sample_weight=sample_weight)

        self.trainX = X
        self.trainY = y
        if self.log:
            for tree, samples_idx in zip(self.estimators_, self.estimators_samples_):
                curX = X[samples_idx]
                curY = y[samples_idx]
                preds = tree.apply(curX)
                for k in np.unique(preds):
                    tree.tree_.value[k, 0, 0] = np.log(np.exp(curY[preds == k]).mean())

    def predict(self, X):
        """
        Predict using the model.

        Parameters
        ----------
        X : array-like
            Data to predict on.

        Returns
        -------
        array-like
            Predicted values.
        """
        preds = []
        for tree, samples_idx in zip(self.estimators_, self.estimators_samples_):
            preds.append(tree.predict(X))
        preds = np.array(preds).T

        means = preds.mean(axis=1)
        vars = preds.var(axis=1)

        return means.reshape(-1, 1), vars.reshape(-1, 1)

    def save(self, file_path: str):
        """
        Save the model to a file.

        Parameters
        ----------
        file_path : str
            Path to the file where the model will be saved.
        """
        import joblib

        joblib.dump(self, file_path)

    def load(self, file_path: str):
        """
        Load the model from a file.

        Parameters
        ----------
        file_path : str
            Path to the file from which the model will be loaded.

        Returns
        -------
        EPMRandomForest
            The loaded model.
        """
        import joblib

        return joblib.load(file_path)

fit(X, y, sample_weight=None)

Fit the model to the data.

Parameters

X : array-like Training data. y : array-like Target values.

Source code in asf/predictors/epm_random_forest.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def fit(self, X, y, sample_weight=None):
    """
    Fit the model to the data.

    Parameters
    ----------
    X : array-like
        Training data.
    y : array-like
        Target values.
    """
    assert sample_weight is None, "Sample weights are not supported"
    super().fit(X=X, y=y, sample_weight=sample_weight)

    self.trainX = X
    self.trainY = y
    if self.log:
        for tree, samples_idx in zip(self.estimators_, self.estimators_samples_):
            curX = X[samples_idx]
            curY = y[samples_idx]
            preds = tree.apply(curX)
            for k in np.unique(preds):
                tree.tree_.value[k, 0, 0] = np.log(np.exp(curY[preds == k]).mean())

load(file_path)

Load the model from a file.

Parameters

file_path : str Path to the file from which the model will be loaded.

Returns

EPMRandomForest The loaded model.

Source code in asf/predictors/epm_random_forest.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def load(self, file_path: str):
    """
    Load the model from a file.

    Parameters
    ----------
    file_path : str
        Path to the file from which the model will be loaded.

    Returns
    -------
    EPMRandomForest
        The loaded model.
    """
    import joblib

    return joblib.load(file_path)

predict(X)

Predict using the model.

Parameters

X : array-like Data to predict on.

Returns

array-like Predicted values.

Source code in asf/predictors/epm_random_forest.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def predict(self, X):
    """
    Predict using the model.

    Parameters
    ----------
    X : array-like
        Data to predict on.

    Returns
    -------
    array-like
        Predicted values.
    """
    preds = []
    for tree, samples_idx in zip(self.estimators_, self.estimators_samples_):
        preds.append(tree.predict(X))
    preds = np.array(preds).T

    means = preds.mean(axis=1)
    vars = preds.var(axis=1)

    return means.reshape(-1, 1), vars.reshape(-1, 1)

save(file_path)

Save the model to a file.

Parameters

file_path : str Path to the file where the model will be saved.

Source code in asf/predictors/epm_random_forest.py
135
136
137
138
139
140
141
142
143
144
145
146
def save(self, file_path: str):
    """
    Save the model to a file.

    Parameters
    ----------
    file_path : str
        Path to the file where the model will be saved.
    """
    import joblib

    joblib.dump(self, file_path)

SklearnWrapper

Bases: AbstractSelector

A generic wrapper for scikit-learn models.

This class allows scikit-learn models to be used with the ASF framework.

Methods

fit(X, Y) Fit the model to the data. predict(X) Predict using the model. save(file_path) Save the model to a file. load(file_path) Load the model from a file.

Source code in asf/predictors/sklearn_wrapper.py
 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
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
class SklearnWrapper(AbstractSelector):
    """
    A generic wrapper for scikit-learn models.

    This class allows scikit-learn models to be used with the ASF framework.

    Methods
    -------
    fit(X, Y)
        Fit the model to the data.
    predict(X)
        Predict using the model.
    save(file_path)
        Save the model to a file.
    load(file_path)
        Load the model from a file.
    """

    def __init__(self, model_class: ClassifierMixin):
        """
        Initialize the wrapper with a scikit-learn model.

        Parameters
        ----------
        model_class : ClassifierMixin
            An instance of a scikit-learn model.
        """
        self.model_class = model_class()

    def fit(self, X, Y):
        """
        Fit the model to the data.

        Parameters
        ----------
        X : array-like
            Training data.
        Y : array-like
            Target values.
        """
        self.model_class.fit(X, Y)

    def predict(self, X):
        """
        Predict using the model.

        Parameters
        ----------
        X : array-like
            Data to predict on.

        Returns
        -------
        array-like
            Predicted values.
        """
        return self.model_class.predict(X)

    def save(self, file_path: str):
        """
        Save the model to a file.

        Parameters
        ----------
        file_path : str
            Path to the file where the model will be saved.
        """
        import joblib

        joblib.dump(self, file_path)

    def load(self, file_path: str):
        """
        Load the model from a file.

        Parameters
        ----------
        file_path : str
            Path to the file from which the model will be loaded.

        Returns
        -------
        SklearnWrapper
            The loaded model.
        """
        import joblib

        return joblib.load(file_path)

__init__(model_class)

Initialize the wrapper with a scikit-learn model.

Parameters

model_class : ClassifierMixin An instance of a scikit-learn model.

Source code in asf/predictors/sklearn_wrapper.py
23
24
25
26
27
28
29
30
31
32
def __init__(self, model_class: ClassifierMixin):
    """
    Initialize the wrapper with a scikit-learn model.

    Parameters
    ----------
    model_class : ClassifierMixin
        An instance of a scikit-learn model.
    """
    self.model_class = model_class()

fit(X, Y)

Fit the model to the data.

Parameters

X : array-like Training data. Y : array-like Target values.

Source code in asf/predictors/sklearn_wrapper.py
34
35
36
37
38
39
40
41
42
43
44
45
def fit(self, X, Y):
    """
    Fit the model to the data.

    Parameters
    ----------
    X : array-like
        Training data.
    Y : array-like
        Target values.
    """
    self.model_class.fit(X, Y)

load(file_path)

Load the model from a file.

Parameters

file_path : str Path to the file from which the model will be loaded.

Returns

SklearnWrapper The loaded model.

Source code in asf/predictors/sklearn_wrapper.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def load(self, file_path: str):
    """
    Load the model from a file.

    Parameters
    ----------
    file_path : str
        Path to the file from which the model will be loaded.

    Returns
    -------
    SklearnWrapper
        The loaded model.
    """
    import joblib

    return joblib.load(file_path)

predict(X)

Predict using the model.

Parameters

X : array-like Data to predict on.

Returns

array-like Predicted values.

Source code in asf/predictors/sklearn_wrapper.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def predict(self, X):
    """
    Predict using the model.

    Parameters
    ----------
    X : array-like
        Data to predict on.

    Returns
    -------
    array-like
        Predicted values.
    """
    return self.model_class.predict(X)

save(file_path)

Save the model to a file.

Parameters

file_path : str Path to the file where the model will be saved.

Source code in asf/predictors/sklearn_wrapper.py
63
64
65
66
67
68
69
70
71
72
73
74
def save(self, file_path: str):
    """
    Save the model to a file.

    Parameters
    ----------
    file_path : str
        Path to the file where the model will be saved.
    """
    import joblib

    joblib.dump(self, file_path)