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

get_dump(dump_format="json") produces invalid JSON for binary splits #5655

Closed
laudv opened this issue May 12, 2020 · 0 comments · Fixed by #5660
Closed

get_dump(dump_format="json") produces invalid JSON for binary splits #5655

laudv opened this issue May 12, 2020 · 0 comments · Fixed by #5660

Comments

@laudv
Copy link

laudv commented May 12, 2020

XGBoost version 1.0.2 installed using pip

Here's a code snippet that reproduces the error:

import xgboost as xgb
import numpy as np
import pandas as pd
import json

num_examples, num_features = 100, 5
X = np.random.randint(0, 2, size=(num_examples, num_features), dtype=bool)
pdX = pd.DataFrame(X)
y = pdX.sum(axis=1) > (num_features // 2)
clf = xgb.XGBClassifier(
        max_depth=1,
        learning_rate=1.0,
        n_estimators=1)
model = clf.fit(pdX, y)
dump = model.get_booster().get_dump(dump_format="json")
print(dump[0])
json.loads(dump[0]) # produces JSON syntax error

output:

  { "nodeid": 0, "depth": 0, "split": "2", "yes": 2, "no": 1} , "children": [
    { "nodeid": 1, "leaf": -0.980392158 },
    { "nodeid": 2, "leaf": 0.807017565 }
  ]}
Traceback (most recent call last):
  File "test.py", line 17, in <module>
    json.loads(dump[0]) # produces JSON syntax error
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 63 (char 62)

It's the closing brace here "no": 2 **}**

Note that this problem only occurs for binary splits.

trivialfis added a commit to trivialfis/xgboost that referenced this issue May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants