我需要将函数适配到数据数组中,并获取该函数方程的最优系数。我使用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
执行代码后出现以下错误:
TypeError: 不支持的操作数类型,使用 ** 或 pow():'list' 和 'list'
但是,当我使用另一个更简单的函数而没有幂运算时,它可以正常工作。
def func(x, a, b):
return a*x + b
它必须试图将一个数的幂次应用于整个输入数据数组
怎么办?帮帮忙...