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

TypeError: Object of type 'ndarray' is not JSON serializable #14

Open
shichaoya opened this issue Mar 10, 2024 · 0 comments
Open

TypeError: Object of type 'ndarray' is not JSON serializable #14

shichaoya opened this issue Mar 10, 2024 · 0 comments

Comments

@shichaoya
Copy link

How to convert the fitted gradient boosting regression model into JSON
My code is as follows

from sklearn.ensemble import GradientBoostingRegressor
import sklearn_json as skljson
import pandas as pd
file_path = 'data.csv'
data = pd.read_csv(file_path)

# 从数据中提取预测值和实际体重

X = data[['Actual_Length', 'Actual_Diagonal', 'Actual_Height']]
y = data['Weight']

创建并拟合梯度提升回归模型

gbr_model = GradientBoostingRegressor()
gbr_model.fit(X, y)

But he will report an error

/Users/yang/opt/anaconda3/envs/tojson/bin/python /Users/yang/PycharmProjects/tojson/weight3.py
Traceback (most recent call last):
File "/Users/yang/PycharmProjects/tojson/weight3.py", line 28, in
json_model = skljson.to_json(gbr_model,'model')
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/site-packages/sklearn_json/init.py", line 122, in to_json
json.dump(serialize_model(model), model_json)
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/json/init.py", line 179, in dump
for chunk in iterable:
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/json/encoder.py", line 430, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
yield from chunks
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/json/encoder.py", line 437, in _iterencode
o = _default(o)
File "/Users/yang/opt/anaconda3/envs/tojson/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'ndarray' is not JSON serializable

Process finished with exit code 1

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

1 participant