首页 > 解决方案 > 情节:TypeError:“复杂”类型的对象不是 JSON 可序列化的

问题描述

# Now plot
tot = sum(eig_vals)
var_exp = [(i / tot)*100 for i in sorted(eig_vals, reverse=True)]
cum_var_exp = np.cumsum(var_exp)

trace1 = Bar(
    x=['PC %s' %i for i in range(1,5)],
    y=var_exp,
    showlegend=False)

trace2 = Scatter(
    x=['PC %s' %i for i in range(1,5)], 
    y=cum_var_exp,
    name='cumulative explained variance')

data = Data([trace1, trace2])

layout=Layout(
    yaxis=YAxis(title='Explained variance in percent'),
    title='Explained variance by different principal components')

fig = Figure(data=data, layout=layout)
#display(data)

py.iplot(fig)

给我这个错误:---> 25 py.iplot(fig) TypeError: Object of type 'complex' is not JSON serializable

有关如何修复的任何帮助?

标签: pythonplotlylinear-algebra

解决方案


推荐阅读