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 converting prediction list to JSON array #8

Open
vpinnaka opened this issue May 13, 2018 · 2 comments
Open

Error converting prediction list to JSON array #8

vpinnaka opened this issue May 13, 2018 · 2 comments

Comments

@vpinnaka
Copy link

Good work Amir, I am trying to run your code but got the following error, I am using python3 and installed all the requirements.
Intial Analysis:
The error is because prediction list contains int64 type values, but jsonifying this list is throwing this error
return jsonify({'prediction': prediction})
Suggested fix
prediction_str = [str(i) for i in prediction] return jsonify({'prediction': prediction_str})
Converting the int64 list to string array solled the error.
Error:
{
"error": "Object of type 'int64' is not JSON serializable",
"trace": "Traceback (most recent call last):\n File "main.py", line 42, in predict\n return jsonify({'prediction': prediction})\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\site-packages\flask\json.py", line 263, in jsonify\n (dumps(data, indent=indent, separators=separators), '\n'),\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\site-packages\flask\json.py", line 123, in dumps\n rv = _json.dumps(obj, **kwargs)\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\init.py", line 238, in dumps\n **kw).encode(obj)\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\encoder.py", line 201, in encode\n chunks = list(chunks)\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\encoder.py", line 430, in _iterencode\n yield from _iterencode_dict(o, _current_indent_level)\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\encoder.py", line 404, in _iterencode_dict\n yield from chunks\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\encoder.py", line 325, in _iterencode_list\n yield from chunks\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\encoder.py", line 437, in _iterencode\n o = _default(o)\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\site-packages\flask\json.py", line 80, in default\n return _json.JSONEncoder.default(self, o)\n File "C:\Users\vinay\Anaconda3\envs\flask-ml\lib\json\encoder.py", line 180, in default\n o.class.name)\nTypeError: Object of type 'int64' is not JSON serializable\n"
}

@sumannitjsr06
Copy link

Thanks @vpinnaka , your fix worked for me.

@fsroque
Copy link

fsroque commented Feb 10, 2019

Replacing line 38 for this works fine:
prediction = clf.predict(query).tolist()

this uses the built-in numpy method instead.

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

3 participants