Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "_catboost.CatBoostError: util/generic/maybe.cpp:4: TMaybe is empty" #1920

Closed
AsciiShell opened this issue Nov 14, 2021 · 1 comment
Closed

Comments

@AsciiShell
Copy link

Problem: Catboost method "select_features" fails with custom metric
catboost version: 1.0.3
Operating System: 20.04.3 LTS (Focal Fossa)
CPU: Intel Xeon Processor (Icelake)
GPU: -

import sys
import numpy as np
import pandas as pd
import catboost
print(sys.version)
print('numpy', np.__version__)
print('pandas', pd.__version__)
print('catboost', catboost.__version__)

# from https://catboost.ai/en/docs/concepts/python-usages-examples#custom-loss-function-eval-metric
class LoglossMetric(object):
    def get_final_error(self, error, weight):
        return error / (weight + 1e-38)

    def is_max_optimal(self):
        return False

    def evaluate(self, approxes, target, weight):
        assert len(approxes) == 1
        assert len(target) == len(approxes[0])

        approx = approxes[0]

        error_sum = 0.0
        weight_sum = 0.0

        for i in range(len(approx)):
            e = np.exp(approx[i])
            p = e / (1 + e)
            w = 1.0 if weight is None else weight[i]
            weight_sum += w
            error_sum += -w * (target[i] * np.log(p) + (1 - target[i]) * np.log(1 - p))

        return error_sum, weight_sum

    
data  = pd.DataFrame({'A': [1, 2, 3, 4] * 100, 'B': [6, 7, 8, 9] * 100, 'target': [0, 1] * 200})
features = ['A', 'B']
train_pool = catboost.Pool(data[features], data['target'])

clf = catboost.CatBoostClassifier(iterations=2, eval_metric=LoglossMetric())
clf.fit(train_pool)

clf = catboost.CatBoostClassifier(iterations=2)
clf.select_features(train_pool, features_for_select=features, steps=1, num_features_to_select=1)

clf = catboost.CatBoostClassifier(iterations=2, eval_metric=LoglossMetric())
clf.select_features(train_pool, features_for_select=features, steps=1, num_features_to_select=1)
3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0]
numpy 1.20.3
pandas 1.3.4
catboost 1.0.3
Learning rate set to 0.5
0:      learn: 0.3432238        total: 379ms    remaining: 379ms
1:      learn: 0.1999481        total: 379ms    remaining: 0us
Learning rate set to 0.5
Step #1 out of 1
0:      learn: 0.3432126        total: 415us    remaining: 415us
1:      learn: 0.1999658        total: 746us    remaining: 0us
Feature #0 eliminated
Train final model
0:      learn: 0.3432126        total: 304us    remaining: 304us
1:      learn: 0.1999658        total: 511us    remaining: 0us
Learning rate set to 0.5
Step #1 out of 1
Traceback (most recent call last):
  File "cb_bug2.py", line 48, in <module>
    clf.select_features(train_pool, features_for_select=features, steps=1, num_features_to_select=1)
  File "/home/asciishell/project/venv/lib/python3.8/site-packages/catboost/core.py", line 4017, in select_features
    summary = self._object._select_features(train_pool, test_pool, params)
  File "_catboost.pyx", line 5001, in _catboost._CatBoost._select_features
  File "_catboost.pyx", line 5015, in _catboost._CatBoost._select_features
_catboost.CatBoostError: util/generic/maybe.cpp:4: TMaybe is empty
arcadia-devtools pushed a commit that referenced this issue Nov 16, 2021
ref:7162f0515040b67ecefded45f60d453c4d883b65
@LyzhinIvan
Copy link
Collaborator

Custom eval metric is supported now in select_features.

robot-piglet pushed a commit that referenced this issue Jan 16, 2023
ref:7162f0515040b67ecefded45f60d453c4d883b65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants