我有一个python文件:analyse.py:
def svm_analyze_AHE(file_name):
# obtain abp file
testdata = pd.read_csv(file_name, header=None)
#print testdata
testdata_to_transpose = testdata.iloc[:,1]
#print testdata_to_transpose
testdata = np.transpose(testdata_to_transpose)
testdata = testdata.reshape(1, -1)
#print testdata
# extract abp values
#print testdata.shape
leng = testdata.shape[1]-1
#print leng
#T = testdata.iloc[:,leng]
T = np.array(testdata[:,0:leng])
#print T
testdata = np.array(T)
# once model is stored, then retrieve it
model = joblib.load('svm-model-0.pkl')
# make predictions
predicted = model.predict(testdata)
return predicted
变量“predict”返回[0.],这不是JSON可序列化的。我该如何纠正这个问题?
答案 0 :(得分:2)
[0.]
是json serializable:
>>> json.dumps([0.])
'[0.0]'
由于你标记了问题tornado
(但在你的问题中没有提到它),我猜你要求龙卷风为你做json序列化并遇到这样的事实: tornado拒绝将数组作为顶级json对象发送。这是由于对跨站点安全漏洞的担忧documented here。建议的解决方法是将数组包装在dict中,而不是在顶层发送它。