Python TypeError:数组([';循环';],dtype=object)不可JSON序列化
您好,我制作了一个文本分类分类器,我在本文中使用了它,它返回了一个数组,我想返回jsonresponse,但最后一行代码给我的错误是“array(['cycling'],dtype=object)不是JSON序列化的”Python TypeError:数组([';循环';],dtype=object)不可JSON序列化,python,json,django,serialization,machine-learning,Python,Json,Django,Serialization,Machine Learning,您好,我制作了一个文本分类分类器,我在本文中使用了它,它返回了一个数组,我想返回jsonresponse,但最后一行代码给我的错误是“array(['cycling'],dtype=object)不是JSON序列化的” def classify_text(request): if request.method == 'POST' and request.POST.get('text'): test = [] text = request.POST.get(
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数组
转换为列表
对象,这可以使用numpy数组对象上的方法轻松完成
示例:
pred_list = pred.tolist()
return JsonResponse(pred_list, safe=False)
请提供答案,而不是回答什么是
pred
?它包含分类器的预测,即“'pred=['cycling']”。它是一个numpy数组对象吗?我认为该方法svn\u model\u linear。predict
返回一个numpy数组对象。