• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 9394559170

26 Apr 2024 06:36AM UTC coverage: 94.656% (-0.02%) from 94.674%
9394559170

push

github

xmatthias
Loader should be passed as kwarg for clarity

20280 of 21425 relevant lines covered (94.66%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.97
/freqtrade/freqai/base_models/FreqaiMultiOutputClassifier.py
1
import numpy as np
1✔
2
from sklearn.base import is_classifier
1✔
3
from sklearn.multioutput import MultiOutputClassifier, _fit_estimator
1✔
4
from sklearn.utils.multiclass import check_classification_targets
1✔
5
from sklearn.utils.parallel import Parallel, delayed
1✔
6
from sklearn.utils.validation import has_fit_parameter
1✔
7

8
from freqtrade.exceptions import OperationalException
1✔
9

10

11
class FreqaiMultiOutputClassifier(MultiOutputClassifier):
1✔
12

13
    def fit(self, X, y, sample_weight=None, fit_params=None):
1✔
14
        """Fit the model to data, separately for each output variable.
15
        Parameters
16
        ----------
17
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
18
            The input data.
19
        y : {array-like, sparse matrix} of shape (n_samples, n_outputs)
20
            Multi-output targets. An indicator matrix turns on multilabel
21
            estimation.
22
        sample_weight : array-like of shape (n_samples,), default=None
23
            Sample weights. If `None`, then samples are equally weighted.
24
            Only supported if the underlying classifier supports sample
25
            weights.
26
        fit_params : A list of dicts for the fit_params
27
            Parameters passed to the ``estimator.fit`` method of each step.
28
            Each dict may contain same or different values (e.g. different
29
            eval_sets or init_models)
30
            .. versionadded:: 0.23
31
        Returns
32
        -------
33
        self : object
34
            Returns a fitted instance.
35
        """
36

37
        if not hasattr(self.estimator, "fit"):
1✔
38
            raise ValueError("The base estimator should implement a fit method")
×
39

40
        y = self._validate_data(X="no_validation", y=y, multi_output=True)
1✔
41

42
        if is_classifier(self):
1✔
43
            check_classification_targets(y)
1✔
44

45
        if y.ndim == 1:
1✔
46
            raise ValueError(
×
47
                "y must have at least two dimensions for "
48
                "multi-output regression but has only one."
49
            )
50

51
        if sample_weight is not None and not has_fit_parameter(
1✔
52
            self.estimator, "sample_weight"
53
        ):
54
            raise ValueError("Underlying estimator does not support sample weights.")
×
55

56
        if not fit_params:
1✔
57
            fit_params = [None] * y.shape[1]
×
58

59
        self.estimators_ = Parallel(n_jobs=self.n_jobs)(
1✔
60
            delayed(_fit_estimator)(
61
                self.estimator, X, y[:, i], sample_weight, **fit_params[i]
62
            )
63
            for i in range(y.shape[1])
64
        )
65

66
        self.classes_ = []
1✔
67
        for estimator in self.estimators_:
1✔
68
            self.classes_.extend(estimator.classes_)
1✔
69
        if len(set(self.classes_)) != len(self.classes_):
1✔
70
            raise OperationalException(f"Class labels must be unique across targets: "
×
71
                                       f"{self.classes_}")
72

73
        if hasattr(self.estimators_[0], "n_features_in_"):
1✔
74
            self.n_features_in_ = self.estimators_[0].n_features_in_
1✔
75
        if hasattr(self.estimators_[0], "feature_names_in_"):
1✔
76
            self.feature_names_in_ = self.estimators_[0].feature_names_in_
×
77

78
        return self
1✔
79

80
    def predict_proba(self, X):
1✔
81
        """
82
        Get predict_proba and stack arrays horizontally
83
        """
84
        results = np.hstack(super().predict_proba(X))
×
85
        return np.squeeze(results)
×
86

87
    def predict(self, X):
1✔
88
        """
89
        Get predict and squeeze into 2D array
90
        """
91
        results = super().predict(X)
×
92
        return np.squeeze(results)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc