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

ValueError: could not convert string to float #11013

Open
Checunmily opened this issue Nov 22, 2024 · 2 comments · May be fixed by #11255
Open

ValueError: could not convert string to float #11013

Checunmily opened this issue Nov 22, 2024 · 2 comments · May be fixed by #11255
Labels
cross-validation Issues related to cross validation implementation in XGBoost. type: bug

Comments

@Checunmily
Copy link

Hello, recently I tried xgb.cv and got an error when assigning save_best=True in xgb.callback.EarlyStopping().
I just used the demo privided at https://xgboost.readthedocs.io/en/latest/python/examples/cross_validation.html
Here is the full code I used:

import os

import numpy as np

import xgboost as xgb

# load data in do training
CURRENT_DIR = os.path.dirname(__file__)
dtrain = xgb.DMatrix(
    os.path.join(CURRENT_DIR, "../data/agaricus.txt.train?format=libsvm")
)
param = {"max_depth": 2, "eta": 1, "objective": "binary:logistic"}
num_round = 2

print("running cross validation, disable standard deviation display and save best")
# do cross validation, this will print result out as
# [iteration]  metric_name:mean_value
res = xgb.cv(
    param,
    dtrain,
    num_boost_round=10,
    nfold=5,
    metrics={"error"},
    seed=0,
    callbacks=[
        xgb.callback.EvaluationMonitor(show_stdv=False),
        xgb.callback.EarlyStopping(3, save_best=True),
    ],
)
print(res)

Finally I will got a

ValueError: could not convert string to float: '(0.001228171312096744, 0.0010411026299985337)'

When I tracing back, I found the function

def best_score(self) -> float:
    """Get best_score."""
    return float(cast(float, self.cvfolds[0].bst.attr("best_score")))

at

~/.conda/envs/sklearn-env/lib/python3.12/site-packages/xgboost/training.py", line 271

will return a float but

self.cvfolds[0].bst.attr("best_score")

just give a str like

(0.001228171312096744, 0.0010411026299985337)

When I trying to skip this step I got another error:

File "~/.conda/envs/sklearn-env/lib/python3.12/site-packages/xgboost/callback.py", line 464, in after_training
    model = model[: best_iteration + 1]
            ~~~~~^^^^^^^^^^^^^^^^^^^^^^
TypeError: '_PackedBooster' object is not subscriptable

Did I miss something? How can I fix it? Thanks in advance.

@trivialfis
Copy link
Member

trivialfis commented Dec 4, 2024

Thank you for raising the issue, will try to fix it. The CV function needs some update.

@trivialfis trivialfis added the cross-validation Issues related to cross validation implementation in XGBoost. label Dec 4, 2024
@trivialfis
Copy link
Member

The cv function doesn't return a model, hence the save_best has no effect. It should not be used with cv.

@trivialfis trivialfis linked a pull request Feb 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cross-validation Issues related to cross validation implementation in XGBoost. type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants