Python json error & #39; xxx is not JSON serializable & #39; solution, jsonserializable

Source: Internet
Author: User

The Processing Method for 'xxx is not json serializable' in Python JSON is jsonserializable.

 

 

1 predictions = self.model.predict(x_data, verbose=0)[0] 2 y_pred_idx = np.argmax(predictions)3 y_pred_prob = predictions[y_pred_idx]4 y_pred_label = self.le.inverse_transform(y_pred_idx) 5 res = OrderedDict()6 res['first'] = self.first_label2name[y_pred_label[0]]7 res['second'] = self.second_label2name[y_pred_label]8 res['probability'] = y_pred_prob9 out.write(json.dumps(res, ensure_ascii=False) + '\n')

Error: '0. 80454153 is not JSON serializable'

 

Type of the output y_pred_prob: <type 'numpy. float32'>

Refer to Region:

Ultimately, it looks likejsonIs telling you thatintIsn't serializable, but really, it's telling you that this particle np. int32 (or whatever type you actually have) isn't serializable. (No real surprise there -- No np. int32IsSerializable ).

 

Json Cannot serialize np. int32, np. float32, or other types. It can be solved through custom serializer or direct type conversion.

Therefore, you can explicitly convert data to the float type in Row 3: res ['bability '] = float (y_pred_prob ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.