嗨,我制作了一个文本分类分类器,我在其中使用了它,它返回一个数组,我想返回 jsonresponse,但最后一行代码给我错误 'array(['cycling'], dtype =object) 不可 JSON 序列化'
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
Chuyển đổi thànhlist
对象,这可以使用.tolist()轻松完成。 numpy 数组对象上的方法。
示例:
pred_list = pred.tolist()
return JsonResponse(pred_list, safe=False)
Tôi là một lập trình viên xuất sắc, rất giỏi!