【问题标题】:TypeError: array([ 0.]) is not JSON serializableTypeError: array([ 0.]) 不是 JSON 可序列化的
【发布时间】:2017-12-10 13:29:51
【问题描述】:

我有一个 python 文件:analyze.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

变量“predicted”返回 [0.],它不是 JSON 可序列化的。我该如何纠正这个问题?

【问题讨论】:

    标签: python json tornado


    【解决方案1】:

    [0.] 是 json 可序列化的:

    >>> json.dumps([0.])
    '[0.0]'
    

    由于您标记了问题tornado(但在您的问题中没有提及),我猜您是在要求 tornado 为您进行 json 序列化并遇到 tornado 拒绝的事实将数组作为顶级 json 对象发送。这是由于担心跨站点安全漏洞documented here。推荐的解决方法是将数组包装在 dict 中,而不是在顶层发送。

    【讨论】:

      猜你喜欢
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2016-08-02
      • 2019-02-08
      • 2013-05-11
      • 2014-08-13
      • 2019-09-01
      相关资源
      最近更新 更多