• 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.22
/freqtrade/freqai/prediction_models/SKLearnRandomForestClassifier.py
1
import logging
1✔
2
from typing import Any, Dict, Tuple
1✔
3

4
import numpy as np
1✔
5
import numpy.typing as npt
1✔
6
from pandas import DataFrame
1✔
7
from sklearn.ensemble import RandomForestClassifier
1✔
8
from sklearn.preprocessing import LabelEncoder
1✔
9

10
from freqtrade.freqai.base_models.BaseClassifierModel import BaseClassifierModel
1✔
11
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
1✔
12

13

14
logger = logging.getLogger(__name__)
1✔
15

16

17
class SKLearnRandomForestClassifier(BaseClassifierModel):
1✔
18
    """
19
    User created prediction model. The class inherits IFreqaiModel, which
20
    means it has full access to all Frequency AI functionality. Typically,
21
    users would use this to override the common `fit()`, `train()`, or
22
    `predict()` methods to add their custom data handling tools or change
23
    various aspects of the training that cannot be configured via the
24
    top level config.json file.
25
    """
26

27
    def fit(self, data_dictionary: Dict, dk: FreqaiDataKitchen, **kwargs) -> Any:
1✔
28
        """
29
        User sets up the training and test data to fit their desired model here
30
        :param data_dictionary: the dictionary holding all data for train, test,
31
            labels, weights
32
        :param dk: The datakitchen object for the current coin/model
33
        """
34

35
        X = data_dictionary["train_features"].to_numpy()
1✔
36
        y = data_dictionary["train_labels"].to_numpy()[:, 0]
1✔
37

38
        if self.freqai_info.get('data_split_parameters', {}).get('test_size', 0.1) == 0:
1✔
39
            eval_set = None
×
40
        else:
41
            test_features = data_dictionary["test_features"].to_numpy()
1✔
42
            test_labels = data_dictionary["test_labels"].to_numpy()[:, 0]
1✔
43

44
            eval_set = (test_features, test_labels)
1✔
45

46
        if self.freqai_info.get("continual_learning", False):
1✔
47
            logger.warning("Continual learning is not supported for "
×
48
                           "SKLearnRandomForestClassifier, ignoring.")
49

50
        train_weights = data_dictionary["train_weights"]
1✔
51

52
        model = RandomForestClassifier(**self.model_training_parameters)
1✔
53

54
        model.fit(X=X, y=y, sample_weight=train_weights)
1✔
55
        if eval_set:
1✔
56
            logger.info("Score: %s", model.score(eval_set[0], eval_set[1]))
1✔
57

58
        return model
1✔
59

60
    def predict(
1✔
61
        self, unfiltered_df: DataFrame, dk: FreqaiDataKitchen, **kwargs
62
    ) -> Tuple[DataFrame, npt.NDArray[np.int_]]:
63
        """
64
        Filter the prediction features data and predict with it.
65
        :param  unfiltered_df: Full dataframe for the current backtest period.
66
        :return:
67
        :pred_df: dataframe containing the predictions
68
        :do_predict: np.array of 1s and 0s to indicate places where freqai needed to remove
69
        data (NaNs) or felt uncertain about data (PCA and DI index)
70
        """
71

72
        (pred_df, dk.do_predict) = super().predict(unfiltered_df, dk, **kwargs)
×
73

74
        le = LabelEncoder()
×
75
        label = dk.label_list[0]
×
76
        labels_before = list(dk.data['labels_std'].keys())
×
77
        labels_after = le.fit_transform(labels_before).tolist()
×
78
        pred_df[label] = le.inverse_transform(pred_df[label])
×
79
        pred_df = pred_df.rename(
×
80
            columns={labels_after[i]: labels_before[i] for i in range(len(labels_before))})
81

82
        return (pred_df, dk.do_predict)
×
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