Skip to content

Models

EnsembleClassifier

Bases: BaseEstimator, ClassifierMixin

Ensemble model for MLClassifiers.

iterable

A list of model objects.

list (default: None)

If None, the majority rule voting will be applied to the predicted class labels. If a list of weights (float or int) is provided, the averaged raw probabilities (via predict_proba) will be used to determine the most confident class label.

Source code in autorad/models/classifier.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
class EnsembleClassifier(BaseEstimator, ClassifierMixin):
    """
    Ensemble model for MLClassifiers.

    Parameters:
    clf : `iterable`
      A list of model objects.
    weights : `list` (default: `None`)
      If `None`, the majority rule voting will be applied to the predicted
        class labels. If a list of weights (`float` or `int`) is provided,
        the averaged raw probabilities (via `predict_proba`) will be used
        to determine the most confident class label.
    """

    def __init__(self, model_list, weights=None):
        self.model_list = model_list
        self.weights = weights

    def fit(self, X, y, **fit_params):
        """
        Fit the scikit-learn estimators.

        Parameters:
        X : numpy array, shape = [n_samples, n_features]
            Training data
        y : list or numpy array, shape = [n_samples]
            Class labels
        """
        for clf in self.model_list:
            clf.fit(X, y, **fit_params)

    def predict(self, X):
        """
        Parameters:
        X : numpy array, shape = [n_samples, n_features]

        Returns:
        maj : list or numpy array, shape = [n_samples]
            Predicted class labels by majority rule
        """
        self.classes_ = np.asarray([clf.predict(X) for clf in self.model_list])
        # if self.weights:
        #    avg = self.predict_proba(X)
        #         maj = np.apply_along_axis(lambda x: max(enumerate(x),
        #               key=operator.itemgetter(1))[0], axis=1, arr=avg)
        # else:
        #     maj = np.asarray(
        #         [
        #             np.argmax(np.bincount(self.classes_[:, c]))
        #             for c in range(self.classes_.shape[1])
        #         ]
        #     )

        # return maj

    def predict_proba(self, X):
        """
        Parameters:
        X : numpy array, shape = [n_samples, n_features]

        Returns:
        avg : list or numpy array, shape = [n_samples, n_probabilities]
            Weighted average probability for each class per sample.
        """
        self.probas_ = [clf.predict_proba(X) for clf in self.model_list]
        avg = np.average(self.probas_, axis=0, weights=self.weights)

        return avg

    def get_model_list(self):
        return self.model_list

fit(X, y, **fit_params)

Fit the scikit-learn estimators.

numpy array, shape = [n_samples, n_features]

Training data

list or numpy array, shape = [n_samples]

Class labels

Source code in autorad/models/classifier.py
192
193
194
195
196
197
198
199
200
201
202
203
def fit(self, X, y, **fit_params):
    """
    Fit the scikit-learn estimators.

    Parameters:
    X : numpy array, shape = [n_samples, n_features]
        Training data
    y : list or numpy array, shape = [n_samples]
        Class labels
    """
    for clf in self.model_list:
        clf.fit(X, y, **fit_params)

predict(X)

X : numpy array, shape = [n_samples, n_features]

list or numpy array, shape = [n_samples]

Predicted class labels by majority rule

Source code in autorad/models/classifier.py
205
206
207
208
209
210
211
212
213
214
def predict(self, X):
    """
    Parameters:
    X : numpy array, shape = [n_samples, n_features]

    Returns:
    maj : list or numpy array, shape = [n_samples]
        Predicted class labels by majority rule
    """
    self.classes_ = np.asarray([clf.predict(X) for clf in self.model_list])

predict_proba(X)

X : numpy array, shape = [n_samples, n_features]

list or numpy array, shape = [n_samples, n_probabilities]

Weighted average probability for each class per sample.

Source code in autorad/models/classifier.py
229
230
231
232
233
234
235
236
237
238
239
240
241
def predict_proba(self, X):
    """
    Parameters:
    X : numpy array, shape = [n_samples, n_features]

    Returns:
    avg : list or numpy array, shape = [n_samples, n_probabilities]
        Weighted average probability for each class per sample.
    """
    self.probas_ = [clf.predict_proba(X) for clf in self.model_list]
    avg = np.average(self.probas_, axis=0, weights=self.weights)

    return avg

MLClassifier

Bases: ClassifierMixin

Class to provide a homogenous interface for all models.

Source code in autorad/models/classifier.py
 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
165
166
167
168
169
170
171
class MLClassifier(ClassifierMixin):
    """
    Class to provide a homogenous interface for all models.
    """

    def __init__(
        self,
        model,
        name: str,
        params: dict = {},
    ):
        self.model = model
        self.name = name
        self.params = params
        self._param_fn = None
        self.available_models = [
            "Random Forest",
            "AdaBoost",
            "Logistic Regression",
            "SVM",
            "Gaussian Process Classifier",
            "XGBoost",
        ]

    def __repr__(self):
        return f"{self.name}"

    @classmethod
    def from_sklearn(cls, name: str, params: dict = {}):
        if name == "Random Forest":
            model = RandomForestClassifier(**params)
        elif name == "AdaBoost":
            model = AdaBoostClassifier(**params)
        elif name == "Logistic Regression":
            model = LogisticRegression(
                max_iter=1000,
                **params,
            )
        elif name == "SVM":
            model = SVC(
                probability=True,
                max_iter=1000,
                **params,
            )
        elif name == "Gaussian Process Classifier":
            model = GaussianProcessClassifier(**params)
        elif name == "XGBoost":
            model = XGBClassifier(
                verbosity=0,
                silent=True,
                use_label_encoder=False,
                **params,
            )
        else:
            raise ValueError("Classifier name not recognized.")

        return cls(model, name, params)

    @classmethod
    def from_keras(cls, model, name, **params):
        """
        Args:
            keras_model (scikeras.KerasClassifier):
                keras model, wrapped for sklearn
            name (str): name, used to reference the model
        """
        return cls(model, name, **params)

    @classmethod
    def initialize_default_sklearn_models(cls, seed: int = 42):
        """
        Initialize a list of all available models.
        """
        models = []
        for model_name in config.AVAILABLE_CLASSIFIERS:
            model = cls.from_sklearn(model_name, params={"random_state": seed})
            models.append(model)
        return models

    def fit(self, X, y, **params):
        self.model.fit(X, y, **params)

    def predict(self, X):
        return self.model.predict(X)

    def predict_proba(self, X, **params):
        return self.model.predict_proba(X, **params)

    def predict_proba_binary(self, X, **params):
        return self.model.predict_proba(X, **params)[:, 1]

    def predict_label_and_proba(self, X):
        y_pred = self.model.predict(X)
        y_pred_proba = self.model.predict_proba_binary(X)

        return y_pred, y_pred_proba

    def score(self, X, y):
        return self.model.score(X, y)

    def get_params(self, deep):
        return self.model.get_params(deep)

    def set_params(self, **params):
        if self.name == "Logistic Regression":
            if params["penalty"] == "l1":
                params["solver"] = "saga"
        self.model.set_params(**params)
        self.params = params
        return self

    @property
    def param_fn(self):
        if self._param_fn is None:
            self._param_fn = optuna_params.get_param_fn(self.name)
        return self._param_fn

    # @param_fn.setter
    # def param_fn(self, param_fn: Optional[Callable] = None):
    #     if param_fn is None:
    #     self._param_fn = param_fn

    def feature_importance(self):
        if self.name == "Logistic Regression":
            importance = self.model.coef_[0]
        elif self.name in ["AdaBoost", "Random Forest", "XGBoost"]:
            importance = self.model.feature_importances_
        else:
            raise ValueError(
                f"For model {self.name} feature \
                               importance could not be calculated."
            )
        return importance

    def save_to_mlflow(self):
        if self.name == "XGBoost":
            mlflow.xgboost.log_model(self.model, "model")
        else:
            try:
                mlflow.sklearn.log_model(self.model, "model")
            except Exception:
                print("Could not save model to mlflow.")

    @classmethod
    def load_from_mlflow(cls, model_uri):
        try:
            model = mlflow.sklearn.load_model(model_uri)
        except Exception:
            try:
                model = mlflow.xgboost.load_model(model_uri)
            except Exception:
                log.error(f"Could not load model from mlflow at {model_uri}")
                return None
        return cls(model, name=model_uri.split("/")[-1])

from_keras(model, name, **params) classmethod

Parameters:

Name Type Description Default
keras_model scikeras.KerasClassifier

keras model, wrapped for sklearn

required
name str

name, used to reference the model

required
Source code in autorad/models/classifier.py
76
77
78
79
80
81
82
83
84
@classmethod
def from_keras(cls, model, name, **params):
    """
    Args:
        keras_model (scikeras.KerasClassifier):
            keras model, wrapped for sklearn
        name (str): name, used to reference the model
    """
    return cls(model, name, **params)

initialize_default_sklearn_models(seed=42) classmethod

Initialize a list of all available models.

Source code in autorad/models/classifier.py
86
87
88
89
90
91
92
93
94
95
@classmethod
def initialize_default_sklearn_models(cls, seed: int = 42):
    """
    Initialize a list of all available models.
    """
    models = []
    for model_name in config.AVAILABLE_CLASSIFIERS:
        model = cls.from_sklearn(model_name, params={"random_state": seed})
        models.append(model)
    return models