Skip to content

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

Description

@shichaoya

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions