Skip to content

Selectors

MultiClassClassifier

Bases: AbstractModelBasedSelector

MultiClassClassifier is a class that predicts the best algorithm for a given instance using a multi-class classification model.

Attributes:

Name Type Description
model_class

The class of the classification model to be used.

metadata

Metadata containing information about the algorithms.

classifier

The trained classification model.

Source code in asf/selectors/mutli_class.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
class MultiClassClassifier(AbstractModelBasedSelector):
    """
    MultiClassClassifier is a class that predicts the best algorithm for a given instance
    using a multi-class classification model.

    Attributes:
        model_class: The class of the classification model to be used.
        metadata: Metadata containing information about the algorithms.
        classifier: The trained classification model.
    """

    def __init__(
        self, model_class, metadata, hierarchical_generator=DummyFeatureGenerator()
    ):
        """
        Initializes the MultiClassClassifier with the given parameters.

        Args:
            model_class: The class of the classification model to be used.
            metadata: Metadata containing information about the algorithms.
            hierarchical_generator: Feature generator to be used.
        """
        AbstractModelBasedSelector.__init__(
            self, model_class, metadata, hierarchical_generator
        )
        self.classifier = None

    def _fit(self, features: pd.DataFrame, performance: pd.DataFrame):
        """
        Fits the classification model to the given feature and performance data.

        Args:
            features: DataFrame containing the feature data.
            performance: DataFrame containing the performance data.
        """
        self.classifier = self.model_class()
        self.classifier.fit(features, np.argmin(performance.values, axis=1))

    def _predict(self, features: pd.DataFrame):
        """
        Predicts the best algorithm for each instance in the given feature data.

        Args:
            features: DataFrame containing the feature data.

        Returns:
            A dictionary mapping instance names to the predicted best algorithm.
        """
        predictions = self.classifier.predict(features)

        return {
            instance_name: self.metadata.algorithms[predictions[i]]
            for i, instance_name in enumerate(features.index)
        }

__init__(model_class, metadata, hierarchical_generator=DummyFeatureGenerator())

Initializes the MultiClassClassifier with the given parameters.

Parameters:

Name Type Description Default
model_class

The class of the classification model to be used.

required
metadata

Metadata containing information about the algorithms.

required
hierarchical_generator

Feature generator to be used.

DummyFeatureGenerator()
Source code in asf/selectors/mutli_class.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def __init__(
    self, model_class, metadata, hierarchical_generator=DummyFeatureGenerator()
):
    """
    Initializes the MultiClassClassifier with the given parameters.

    Args:
        model_class: The class of the classification model to be used.
        metadata: Metadata containing information about the algorithms.
        hierarchical_generator: Feature generator to be used.
    """
    AbstractModelBasedSelector.__init__(
        self, model_class, metadata, hierarchical_generator
    )
    self.classifier = None

PairwiseClassifier

Bases: AbstractModelBasedSelector, AbstractFeatureGenerator

PairwiseClassifier is a selector that uses pairwise comparison of algorithms to predict the best algorithm for a given instance.

Attributes:

Name Type Description
model_class ClassifierMixin

The classifier model to be used for pairwise comparisons.

classifiers list[ClassifierMixin]

List of trained classifiers for pairwise comparisons.

Source code in asf/selectors/pairwise_classifier.py
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
class PairwiseClassifier(AbstractModelBasedSelector, AbstractFeatureGenerator):
    """
    PairwiseClassifier is a selector that uses pairwise comparison of algorithms
    to predict the best algorithm for a given instance.

    Attributes:
        model_class (ClassifierMixin): The classifier model to be used for pairwise comparisons.
        classifiers (list[ClassifierMixin]): List of trained classifiers for pairwise comparisons.
    """

    def __init__(
        self, model_class, metadata, hierarchical_generator=DummyFeatureGenerator()
    ):
        """
        Initializes the PairwiseClassifier with a given model class and hierarchical feature generator.

        Args:
            model_class (ClassifierMixin): The classifier model to be used for pairwise comparisons.
            hierarchical_generator (AbstractFeatureGenerator, optional): The feature generator to be used. Defaults to DummyFeatureGenerator.
        """
        AbstractModelBasedSelector.__init__(
            self, model_class, metadata, hierarchical_generator
        )
        AbstractFeatureGenerator.__init__(self)
        self.classifiers: list[AbstractPredictor] = []

    def _fit(self, features: pd.DataFrame, performance: pd.DataFrame):
        """
        Fits the pairwise classifiers using the provided features and performance data.

        Args:
            features (pd.DataFrame): The feature data for the instances.
            performance (pd.DataFrame): The performance data for the algorithms.
        """
        for i, algorithm in enumerate(self.metadata.algorithms):
            for other_algorithm in self.metadata.algorithms[i + 1 :]:
                algo1_times = performance[algorithm]
                algo2_times = performance[other_algorithm]

                diffs = algo1_times < algo2_times
                cur_model = self.model_class()
                cur_model.fit(features, diffs)
                self.classifiers.append(cur_model)

    def _predict(self, features: pd.DataFrame):
        """
        Predicts the best algorithm for each instance using the trained pairwise classifiers.

        Args:
            features (pd.DataFrame): The feature data for the instances.

        Returns:
            dict: A dictionary mapping instance names to the predicted best algorithm.
        """
        predictions_sum = self.generate_features(features)
        return {
            instance_name: [
                (
                    self.metadata.algorithms[np.argmax(predictions_sum[i])],
                    self.metadata.budget,
                )
            ]
            for i, instance_name in enumerate(features)
        }

    def generate_features(self, features: pd.DataFrame):
        """
        Generates features for the pairwise classifiers.

        Args:
            features (pd.DataFrame): The feature data for the instances.

        Returns:
            np.ndarray: An array of predictions for each instance and algorithm pair.
        """
        predictions_sum = np.zeros((features.shape[1], len(self.metadata.algorithms)))
        for i, algorithm in enumerate(self.metadata.algorithms):
            for j, other_algorithm in enumerate(self.metadata.algorithms[i + 1 :]):
                prediction = self.classifiers[i].predict(features)

                predictions_sum[prediction, i] += 1
                predictions_sum[~prediction, j] -= 1

        return predictions_sum

__init__(model_class, metadata, hierarchical_generator=DummyFeatureGenerator())

Initializes the PairwiseClassifier with a given model class and hierarchical feature generator.

Parameters:

Name Type Description Default
model_class ClassifierMixin

The classifier model to be used for pairwise comparisons.

required
hierarchical_generator AbstractFeatureGenerator

The feature generator to be used. Defaults to DummyFeatureGenerator.

DummyFeatureGenerator()
Source code in asf/selectors/pairwise_classifier.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def __init__(
    self, model_class, metadata, hierarchical_generator=DummyFeatureGenerator()
):
    """
    Initializes the PairwiseClassifier with a given model class and hierarchical feature generator.

    Args:
        model_class (ClassifierMixin): The classifier model to be used for pairwise comparisons.
        hierarchical_generator (AbstractFeatureGenerator, optional): The feature generator to be used. Defaults to DummyFeatureGenerator.
    """
    AbstractModelBasedSelector.__init__(
        self, model_class, metadata, hierarchical_generator
    )
    AbstractFeatureGenerator.__init__(self)
    self.classifiers: list[AbstractPredictor] = []

generate_features(features)

Generates features for the pairwise classifiers.

Parameters:

Name Type Description Default
features DataFrame

The feature data for the instances.

required

Returns:

Type Description

np.ndarray: An array of predictions for each instance and algorithm pair.

Source code in asf/selectors/pairwise_classifier.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def generate_features(self, features: pd.DataFrame):
    """
    Generates features for the pairwise classifiers.

    Args:
        features (pd.DataFrame): The feature data for the instances.

    Returns:
        np.ndarray: An array of predictions for each instance and algorithm pair.
    """
    predictions_sum = np.zeros((features.shape[1], len(self.metadata.algorithms)))
    for i, algorithm in enumerate(self.metadata.algorithms):
        for j, other_algorithm in enumerate(self.metadata.algorithms[i + 1 :]):
            prediction = self.classifiers[i].predict(features)

            predictions_sum[prediction, i] += 1
            predictions_sum[~prediction, j] -= 1

    return predictions_sum

PairwiseRegressor

Bases: AbstractModelBasedSelector, AbstractFeatureGenerator

PairwiseRegressor is a selector that uses pairwise regression of algorithms to predict the best algorithm for a given instance.

Attributes:

Name Type Description
model_class

The regression model to be used for pairwise comparisons.

regressors

List of trained regressors for pairwise comparisons.

Source code in asf/selectors/pairwise_regressor.py
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
class PairwiseRegressor(AbstractModelBasedSelector, AbstractFeatureGenerator):
    """
    PairwiseRegressor is a selector that uses pairwise regression of algorithms
    to predict the best algorithm for a given instance.

    Attributes:
        model_class: The regression model to be used for pairwise comparisons.
        regressors: List of trained regressors for pairwise comparisons.
    """

    def __init__(
        self, model_class, metadata, hierarchical_generator=DummyFeatureGenerator()
    ):
        """
        Initializes the PairwiseRegressor with a given model class and hierarchical feature generator.

        Args:
            model_class: The regression model to be used for pairwise comparisons.
            hierarchical_generator (AbstractFeatureGenerator, optional): The feature generator to be used. Defaults to DummyFeatureGenerator.
        """
        AbstractModelBasedSelector.__init__(
            self, model_class, metadata, hierarchical_generator
        )
        AbstractFeatureGenerator.__init__(self)
        self.regressors = []

    def _fit(self, features: pd.DataFrame, performance: pd.DataFrame):
        """
        Fits the pairwise regressors using the provided features and performance data.

        Args:
            features (pd.DataFrame): The feature data for the instances.
            performance (pd.DataFrame): The performance data for the algorithms.
        """
        for i, algorithm in enumerate(self.metadata.algorithms):
            for other_algorithm in self.metadata.algorithms[i + 1 :]:
                algo1_times = performance[algorithm]
                algo2_times = performance[other_algorithm]

                diffs = algo1_times - algo2_times
                cur_model = self.model_class()
                cur_model.fit(features, diffs)
                self.regressors.append(cur_model)

    def _predict(self, features: pd.DataFrame):
        """
        Predicts the best algorithm for each instance using the trained pairwise regressors.

        Args:
            features (pd.DataFrame): The feature data for the instances.

        Returns:
            dict: A dictionary mapping instance names to the predicted best algorithm.
        """
        predictions_sum = self.generate_features(features)
        return {
            instance_name: [
                (
                    self.metadata.algorithms[np.argmin(predictions_sum[i])],
                    self.metadata.budget,
                )
            ]
            for i, instance_name in enumerate(features.index)
        }

    def generate_features(self, features: pd.DataFrame):
        """
        Generates features for the pairwise regressors.

        Args:
            features (pd.DataFrame): The feature data for the instances.

        Returns:
            np.ndarray: An array of predictions for each instance and algorithm pair.
        """
        predictions_sum = np.zeros((features.shape[0], len(self.metadata.algorithms)))
        for i, algorithm in enumerate(self.metadata.algorithms):
            for j, other_algorithm in enumerate(self.metadata.algorithms[i + 1 :]):
                prediction = self.regressors[i].predict(features)

                predictions_sum[:, i] += prediction
                predictions_sum[:, j] -= prediction

        return predictions_sum

__init__(model_class, metadata, hierarchical_generator=DummyFeatureGenerator())

Initializes the PairwiseRegressor with a given model class and hierarchical feature generator.

Parameters:

Name Type Description Default
model_class

The regression model to be used for pairwise comparisons.

required
hierarchical_generator AbstractFeatureGenerator

The feature generator to be used. Defaults to DummyFeatureGenerator.

DummyFeatureGenerator()
Source code in asf/selectors/pairwise_regressor.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def __init__(
    self, model_class, metadata, hierarchical_generator=DummyFeatureGenerator()
):
    """
    Initializes the PairwiseRegressor with a given model class and hierarchical feature generator.

    Args:
        model_class: The regression model to be used for pairwise comparisons.
        hierarchical_generator (AbstractFeatureGenerator, optional): The feature generator to be used. Defaults to DummyFeatureGenerator.
    """
    AbstractModelBasedSelector.__init__(
        self, model_class, metadata, hierarchical_generator
    )
    AbstractFeatureGenerator.__init__(self)
    self.regressors = []

generate_features(features)

Generates features for the pairwise regressors.

Parameters:

Name Type Description Default
features DataFrame

The feature data for the instances.

required

Returns:

Type Description

np.ndarray: An array of predictions for each instance and algorithm pair.

Source code in asf/selectors/pairwise_regressor.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def generate_features(self, features: pd.DataFrame):
    """
    Generates features for the pairwise regressors.

    Args:
        features (pd.DataFrame): The feature data for the instances.

    Returns:
        np.ndarray: An array of predictions for each instance and algorithm pair.
    """
    predictions_sum = np.zeros((features.shape[0], len(self.metadata.algorithms)))
    for i, algorithm in enumerate(self.metadata.algorithms):
        for j, other_algorithm in enumerate(self.metadata.algorithms[i + 1 :]):
            prediction = self.regressors[i].predict(features)

            predictions_sum[:, i] += prediction
            predictions_sum[:, j] -= prediction

    return predictions_sum

PerformanceModel

Bases: AbstractModelBasedSelector, AbstractFeatureGenerator

PerformancePredictor is a class that predicts the performance of algorithms based on given features. It can handle both single-target and multi-target regression models.

Attributes:

Name Type Description
model_class

The class of the regression model to be used.

metadata

Metadata containing information about the algorithms.

use_multi_target

Boolean indicating whether to use multi-target regression.

normalize

Method to normalize the performance data.

regressors

List of trained regression models.

Source code in asf/selectors/performance_model.py
 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
class PerformanceModel(AbstractModelBasedSelector, AbstractFeatureGenerator):
    """
    PerformancePredictor is a class that predicts the performance of algorithms
    based on given features. It can handle both single-target and multi-target
    regression models.

    Attributes:
        model_class: The class of the regression model to be used.
        metadata: Metadata containing information about the algorithms.
        use_multi_target: Boolean indicating whether to use multi-target regression.
        normalize: Method to normalize the performance data.
        regressors: List of trained regression models.
    """

    def __init__(
        self,
        model_class,
        metadata,
        use_multi_target=False,
        normalize="log",
        hierarchical_generator=DummyFeatureGenerator(),
    ):
        """
        Initializes the PerformancePredictor with the given parameters.

        Args:
            model_class: The class of the regression model to be used.
            metadata: Metadata containing information about the algorithms.
            use_multi_target: Boolean indicating whether to use multi-target regression.
            normalize: Method to normalize the performance data.
            hierarchical_generator: Feature generator to be used.
        """
        AbstractModelBasedSelector.__init__(
            self, model_class, metadata, hierarchical_generator
        )
        AbstractFeatureGenerator.__init__(self)
        self.regressors = []
        self.use_multi_target = use_multi_target
        self.normalize = normalize

    def _fit(self, features: pd.DataFrame, performance: pd.DataFrame):
        """
        Fits the regression models to the given features and performance data.

        Args:
            features: DataFrame containing the feature data.
            performance: DataFrame containing the performance data.
        """
        if self.normalize == "log":
            performance = np.log(performance + 1e-8)

        if self.use_multi_target:
            self.regressors = self.model_class()
            self.regressors.fit(features, performance)
        else:
            for i, algorithm in enumerate(self.metadata.algorithms):
                algo_times = performance.iloc[:, i]

                cur_model = self.model_class()
                cur_model.fit(features, algo_times)
                self.regressors.append(cur_model)

    def _predict(self, features: pd.DataFrame):
        """
        Predicts the performance of algorithms for the given features.

        Args:
            features: DataFrame containing the feature data.

        Returns:
            A dictionary mapping instance names to the predicted best algorithm.
        """
        predictions = self.generate_features(features)

        return {
            instance_name: [
                (
                    self.metadata.algorithms[np.argmin(predictions[i])],
                    self.metadata.budget,
                )
            ]
            for i, instance_name in enumerate(features.index)
        }

    def generate_features(self, features: pd.DataFrame) -> pd.DataFrame:
        """
        Generates predictions for the given features using the trained models.

        Args:
            features: DataFrame containing the feature data.

        Returns:
            DataFrame containing the predictions for each algorithm.
        """
        if self.use_multi_target:
            predictions = self.regressors.predict(features)
        else:
            predictions = np.zeros((features.shape[0], len(self.metadata.algorithms)))
            for i, algorithm in enumerate(self.metadata.algorithms):
                prediction = self.regressors[i].predict(features)
                predictions[:, i] = prediction

        return predictions

__init__(model_class, metadata, use_multi_target=False, normalize='log', hierarchical_generator=DummyFeatureGenerator())

Initializes the PerformancePredictor with the given parameters.

Parameters:

Name Type Description Default
model_class

The class of the regression model to be used.

required
metadata

Metadata containing information about the algorithms.

required
use_multi_target

Boolean indicating whether to use multi-target regression.

False
normalize

Method to normalize the performance data.

'log'
hierarchical_generator

Feature generator to be used.

DummyFeatureGenerator()
Source code in asf/selectors/performance_model.py
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
def __init__(
    self,
    model_class,
    metadata,
    use_multi_target=False,
    normalize="log",
    hierarchical_generator=DummyFeatureGenerator(),
):
    """
    Initializes the PerformancePredictor with the given parameters.

    Args:
        model_class: The class of the regression model to be used.
        metadata: Metadata containing information about the algorithms.
        use_multi_target: Boolean indicating whether to use multi-target regression.
        normalize: Method to normalize the performance data.
        hierarchical_generator: Feature generator to be used.
    """
    AbstractModelBasedSelector.__init__(
        self, model_class, metadata, hierarchical_generator
    )
    AbstractFeatureGenerator.__init__(self)
    self.regressors = []
    self.use_multi_target = use_multi_target
    self.normalize = normalize

generate_features(features)

Generates predictions for the given features using the trained models.

Parameters:

Name Type Description Default
features DataFrame

DataFrame containing the feature data.

required

Returns:

Type Description
DataFrame

DataFrame containing the predictions for each algorithm.

Source code in asf/selectors/performance_model.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def generate_features(self, features: pd.DataFrame) -> pd.DataFrame:
    """
    Generates predictions for the given features using the trained models.

    Args:
        features: DataFrame containing the feature data.

    Returns:
        DataFrame containing the predictions for each algorithm.
    """
    if self.use_multi_target:
        predictions = self.regressors.predict(features)
    else:
        predictions = np.zeros((features.shape[0], len(self.metadata.algorithms)))
        for i, algorithm in enumerate(self.metadata.algorithms):
            prediction = self.regressors[i].predict(features)
            predictions[:, i] = prediction

    return predictions