我需要将函数拟合到数据数组并获得该函数方程的最佳系数.我使用scipy库中的curve_fit方法.它基于最小二乘法.
import numpy as np
from scipy.optimize import curve_fit
#This is my function from which i need to get optimal coefficients 'a' and 'b'
def func(x, a, b):
return a*x**(b*x)
#the arrays of input data
x = [1,2,3,4,5]
y =[6,7,8,9,10]
#default (guess) coefficients
p0 = [1, 1]
popt, pcov = curve_fit(func, x, y, p0)
print popt
Run Code Online (Sandbox Code Playgroud)
它返回以下错误
TypeError:**或pow()不支持的操作数类型:'list'和'list'
但是,当我使用另一个更简单的功能而没有电源操作时,它可以工作
def func(x, a, b):
return a*x + b
Run Code Online (Sandbox Code Playgroud)
它必须尝试将数字增加到整个输入数据阵列的幂
该怎么办?请帮忙...
看起来你是在追逐元素的力量?
喜欢a*x[i]**(b*x[i])每个我?
在这种情况下,您必须使用以下np.power功能:
def func(x,a,b):
return a*np.power(x,b*x)
Run Code Online (Sandbox Code Playgroud)
然后它工作.
(顺便说一句,它可能是值得的转换x和y从列表来numpy的数组:np.array(x)).
| 归档时间: |
|
| 查看次数: |
24371 次 |
| 最近记录: |