类型的无效值' numpy.float64'收到了' y'散布的特性
当我尝试使用plotly使用add_trace函数时,我会出现主题错误。我有2个自变量(x1和x2)和一个因变量(y)。我使用curve_fit进行数据拟合,并希望在图表中添加跟踪线。注意这是一个随机数据集,因此结果根本没有意义。
以下是代码和错误。任何建议或建议将不胜感激
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from scipy.optimize import curve_fit
np.random.seed(0)
x1 = np.random.randint(0, 100, 100)
x2 = np.random.randint(0, 100, 100)
y = np.random.randint(0, 100, 100)
def func(x, a, b, c, d, e, f, g):
return (a * x[0]**3 + b * x[0]**2 + c * x[0]) + (d * x[1]**3 + e * x[1]**2 + f * x[1]) + g
def inv(x1, x2, y):
(a, b, c, d, e, f, g), _ = curve_fit(func, np.stack([x1, x2]), y)
# create graph
x3 = [sum(i) for i in zip(x1, x2 )]
x = np.linspace(min(x3), max(y), 1000)
fig = go.Figure(go.Scatter(x=x3, y=y, name='data'))
fig.add_trace(go.Scatter(x=x, y=func(x, a, b, c, d, e, f, g), name='fit'))
fig.show()
return (a, b, c, d, e, f, g)
inv(x1, x2, y)
ValueError:
Invalid value of type 'numpy.float64' received for the 'y' property of scatter
Received value: 49.464592654826
The 'y' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series
I am having the subject error when I try to use add_trace function from plotly. I have 2 independent variables (x1 and x2) and a dependent variable(y). I used curve_fit for data fitting and want to add a trace line to the graph. Be noted this is a random dataset so the result doesn't make sense at all.
Below is the code and the error. Any advice or suggestion would be greatly appreciated
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from scipy.optimize import curve_fit
np.random.seed(0)
x1 = np.random.randint(0, 100, 100)
x2 = np.random.randint(0, 100, 100)
y = np.random.randint(0, 100, 100)
def func(x, a, b, c, d, e, f, g):
return (a * x[0]**3 + b * x[0]**2 + c * x[0]) + (d * x[1]**3 + e * x[1]**2 + f * x[1]) + g
def inv(x1, x2, y):
(a, b, c, d, e, f, g), _ = curve_fit(func, np.stack([x1, x2]), y)
# create graph
x3 = [sum(i) for i in zip(x1, x2 )]
x = np.linspace(min(x3), max(y), 1000)
fig = go.Figure(go.Scatter(x=x3, y=y, name='data'))
fig.add_trace(go.Scatter(x=x, y=func(x, a, b, c, d, e, f, g), name='fit'))
fig.show()
return (a, b, c, d, e, f, g)
inv(x1, x2, y)
ValueError:
Invalid value of type 'numpy.float64' received for the 'y' property of scatter
Received value: 49.464592654826
The 'y' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题将单个值作为参数
y
将单个值传递给绘图。由于版本3参见迁移指导plotly 。
因此,在您的情况下,简单的修复将是替换
fig.add_trace(go.scatter(x = x,y = func(x,a,a,b,c,c,d,e,f,g),name =' fit'))
带有图。Add_trace(go.scatter(x = x,y = [func(x,a,a,b,c,d,e,e,f,g)],name =' fit'))
。我最近有一个非常相似的问题,其中我的模型的预测可能是列表或数字。为此,我使用
np.Array(current_predictions)
作为y
的参数。我知道这不会再有所帮助,但也许其他人偶然发现了:)
The problem in you pass a single value as parameter
y
to plotly.This is not supported anymore since version 3 see migration-guide-plotly.
So in your case the simple fix would be to replace
fig.add_trace(go.Scatter(x=x, y=func(x, a, b, c, d, e, f, g), name='fit'))
withfig.add_trace(go.Scatter(x=x, y=[func(x, a, b, c, d, e, f, g)], name='fit'))
.I had a very similar problem lately where the predictions of my models could either be a list or a number. For this I used
np.array(current_predictions)
as parameter fory
.I know this won't help you anymore but maybe someone else stumbles upon it :)