how to fix ValueError: operands could not be broadcast together with shapes (50,) (10,)
how to fix ValueError: operands could not be broadcast together with shapes (50,) (10,)
这是数据框的样子,下面是带有方程和曲线拟合的代码:
all # this is the dataframe 'all'
[ y x Temp gamma Kc Ko
0 -0.501864 51.040405 26.575860 4.654191 32.342013 17.434281
1 3.317406 82.390735 26.608765 4.658023 32.457605 17.452507
2 10.207180 146.722313 26.645362 4.662289 32.586622 17.472796
3 16.055772 212.771464 26.698917 4.668535 32.776289 17.502520]
绘制模型数据时出现以下错误,我不明白为什么:
def model(x, Vcmax):
return Vcmax*((x- gamma))/((x+Kc)*(1+(O/Ko))) - Rd
def func_fit(x):
return model(x,
*popt)
from scipy.optimize import curve_fit
import math
for g in all:
O = 21
Rd = 1.60
Vcmax = 40 # initial guess
gamma=g.gamma # value from dataframe, function of temperature
Kc=g.Kc # value from dataframe, function of temperature
Ko=g.Ko # value from dataframe, function of temperature
# gamma = 34
# Kc = 89
# Ko = 80
popt, pcov = curve_fit(model, g['x'], g['y'], p0 = np.array([Vcmax]),
absolute_sigma=True, maxfev=1000000)
##################### new data frame
new_row = { 'Vcmax': popt[0]}
results=results.append(new_row, ignore_index=True)
x = np.linspace(0,300)
b = model(x, results.iloc[0,1],) # this is the line generating the error,
plt.plot(x, b, 'r') # plotting modelled data
plt.scatter(df.x, df.y, color='red') # plotting observations
ValueError: operands could not be broadcast together with shapes (50,) (10,)
如果我 'fix' 将 gamma、Ko 和 Kc 的值设为常量而不是当前情况,那么我不会得到相同的错误,但是拟合非常糟糕,见图
我没试过,但这应该行得通:
x = np.linspace(0,300, 10)
b = model(x, results.iloc[0,1],) # this is the line generating the error,
问题是 x
和 results.iloc[0,1]
的形状不一样,但它们本应如此。将参数 10
添加到 np.linespace 应将 x 数组的形状更改为与 results.iloc[0,1]
相同的形状
这是数据框的样子,下面是带有方程和曲线拟合的代码:
all # this is the dataframe 'all'
[ y x Temp gamma Kc Ko
0 -0.501864 51.040405 26.575860 4.654191 32.342013 17.434281
1 3.317406 82.390735 26.608765 4.658023 32.457605 17.452507
2 10.207180 146.722313 26.645362 4.662289 32.586622 17.472796
3 16.055772 212.771464 26.698917 4.668535 32.776289 17.502520]
绘制模型数据时出现以下错误,我不明白为什么:
def model(x, Vcmax):
return Vcmax*((x- gamma))/((x+Kc)*(1+(O/Ko))) - Rd
def func_fit(x):
return model(x,
*popt)
from scipy.optimize import curve_fit
import math
for g in all:
O = 21
Rd = 1.60
Vcmax = 40 # initial guess
gamma=g.gamma # value from dataframe, function of temperature
Kc=g.Kc # value from dataframe, function of temperature
Ko=g.Ko # value from dataframe, function of temperature
# gamma = 34
# Kc = 89
# Ko = 80
popt, pcov = curve_fit(model, g['x'], g['y'], p0 = np.array([Vcmax]),
absolute_sigma=True, maxfev=1000000)
##################### new data frame
new_row = { 'Vcmax': popt[0]}
results=results.append(new_row, ignore_index=True)
x = np.linspace(0,300)
b = model(x, results.iloc[0,1],) # this is the line generating the error,
plt.plot(x, b, 'r') # plotting modelled data
plt.scatter(df.x, df.y, color='red') # plotting observations
ValueError: operands could not be broadcast together with shapes (50,) (10,)
如果我 'fix' 将 gamma、Ko 和 Kc 的值设为常量而不是当前情况,那么我不会得到相同的错误,但是拟合非常糟糕,见图
我没试过,但这应该行得通:
x = np.linspace(0,300, 10)
b = model(x, results.iloc[0,1],) # this is the line generating the error,
问题是 x
和 results.iloc[0,1]
的形状不一样,但它们本应如此。将参数 10
添加到 np.linespace 应将 x 数组的形状更改为与 results.iloc[0,1]