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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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