TypeError: array([‘cycling’], dtype=object) is not JSON serializable

嗨,我做了一个文本分类器,我已经在使用它了,它返回给我一个数组,而我想返回一个JSON响应,但是代码的最后一行出现了错误 ‘array([‘cycling’], dtype=object) is not JSON serializable’

def classify_text(request):    if request.method == 'POST' and request.POST.get('text'):        test = []        text = request.POST.get('text')        text = re.sub('[^a-zA-Z]', ' ', text)        text = text.lower()        text = text.split()        ps = PorterStemmer()        text = [ps.stem(word) for word in text if not word in set(stopwords.words('english'))]        text = ' '.join(text)        test.append(text)        pred = cv.transform(test).toarray()        pred = svm_model_linear.predict(pred)        return JsonResponse(pred, safe=False)

回答:

你需要将numpy array转换为list对象,这可以通过在numpy数组对象上使用.tolist()方法轻松实现。

示例

pred_list = pred.tolist()return JsonResponse(pred_list, safe=False)

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注