尝试绘制建模数据时出现以下错误:ValueError: 传递值的长度为 2,索引意味着 9。如何解决此问题?

发布于 2025-01-14 10:49:46 字数 1459 浏览 2 评论 0原文

我正在尝试绘制观察到的和建模的数据。这是数据框的示例,位于代码下方。 我收到此错误#### ValueError:传递值的长度为2,索引意味着9####。 我不知道如何绘制建模数据。

all = [treatment1, treatment2]

 [    x     y   intercept 
0     25  0.171144     1.6
1     50  1.859164     1.6
2    100  4.407867     1.6
      x     y       intercept 
14    25  0.997440     1.3
15    50  1.823077     1.3  
16   100  4.412220     1.3]



def model(x, slope):
 return  (slope*x) + intercept 

def func_fit(x):
 return model(x,  
            popt)


from scipy.optimize import curve_fit

for g in all: #this loop allows to fit the model for each treatment

popt = curve_fit(model, g['x'], g['y'])

new_row = {'treatment':treatment, 'slope': popt[0], 'intercept':intercept}

results=results.append(new_row, ignore_index=True)

plt.plot(g['x'],  g['y'], '.', label='data')

plt.plot(g['x'], func_fit(g['x']), 'r-', tuple(popt)) # this is the line that gives the error. 

#### ValueError: Length of passed values is 2, index implies 9####

#I tried to overcome the error but what I get with these lines below is no line
#t= np.linspace(0,200,1)
#a = model(t,results.iloc[0,1])
#plt.plot(t, a, 'r') # plotting t, a separately

这些是观察结果,我也想绘制建模数据:

观察

建模数据

I am trying to plot observed and modelled data. This is an example of dataframe and below the code.
I get this error #### ValueError: Length of passed values is 2, index implies 9####.
I don't know how else to plot the modelled data.

all = [treatment1, treatment2]

 [    x     y   intercept 
0     25  0.171144     1.6
1     50  1.859164     1.6
2    100  4.407867     1.6
      x     y       intercept 
14    25  0.997440     1.3
15    50  1.823077     1.3  
16   100  4.412220     1.3]



def model(x, slope):
 return  (slope*x) + intercept 

def func_fit(x):
 return model(x,  
            popt)


from scipy.optimize import curve_fit

for g in all: #this loop allows to fit the model for each treatment

popt = curve_fit(model, g['x'], g['y'])

new_row = {'treatment':treatment, 'slope': popt[0], 'intercept':intercept}

results=results.append(new_row, ignore_index=True)

plt.plot(g['x'],  g['y'], '.', label='data')

plt.plot(g['x'], func_fit(g['x']), 'r-', tuple(popt)) # this is the line that gives the error. 

#### ValueError: Length of passed values is 2, index implies 9####

#I tried to overcome the error but what I get with these lines below is no line
#t= np.linspace(0,200,1)
#a = model(t,results.iloc[0,1])
#plt.plot(t, a, 'r') # plotting t, a separately

These are the observations and I would like to plot the modelled data as well:

observations

modelled data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-01-21 10:49:46

我假设您想绘制模型学到的函数?
如果是这种情况,这里是使用 官方文档 scipy.optimize.curve_fit

data = [
    [25, 0.171144, 1.6],
    [50, 1.859164, 1.6],
    [100, 4.407867, 1.6]]

df = pd.DataFrame(data, columns=['x', 'y', 'intercept'])

def model(x, slope, intercept):
    return  (slope*x) + intercept 



xdata=df['x']
ydata=df['y']
popt, _ = curve_fit(model, xdata, ydata)

plt.plot(xdata, ydata, 'o', label='data')
plt.plot(xdata, model(xdata, *popt), 'r-',
         label='fit: slope=%5.3f, intercept=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

导致此结果:
结果

I assume you want to plot the function your model learned?
If this is the case here is a simple implementation using the code snippets from the Official docs scipy.optimize.curve_fit:

data = [
    [25, 0.171144, 1.6],
    [50, 1.859164, 1.6],
    [100, 4.407867, 1.6]]

df = pd.DataFrame(data, columns=['x', 'y', 'intercept'])

def model(x, slope, intercept):
    return  (slope*x) + intercept 



xdata=df['x']
ydata=df['y']
popt, _ = curve_fit(model, xdata, ydata)

plt.plot(xdata, ydata, 'o', label='data')
plt.plot(xdata, model(xdata, *popt), 'r-',
         label='fit: slope=%5.3f, intercept=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

Leads to this result:
Result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文