Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:数组([';循环';],dtype=object)不可JSON序列化_Python_Json_Django_Serialization_Machine Learning - Fatal编程技术网

Python TypeError:数组([';循环';],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(

您好,我制作了一个文本分类分类器,我在本文中使用了它,它返回了一个数组,我想返回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数组
转换为
列表
对象,这可以使用numpy数组对象上的方法轻松完成

示例

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

请提供答案,而不是回答什么是
pred
?它包含分类器的预测,即“'pred=['cycling']”。它是一个numpy数组对象吗?我认为该方法
svn\u model\u linear。predict
返回一个numpy数组对象。