Python 以数组为参数的 lambda 函数

Python lambda function with arrays as parameters

我正在尝试定义一个包含 n 个变量的函数以适应数据集。函数看起来像这样。

Kelly Function

然后我想使用 scipy.optimize.leastsq

找到适合我的数据集的最佳 ai 和 bj

到目前为止,这是我的代码。

from scipy.optimize import leastsq 
import numpy as np

def kellyFunc(a, b, x): #Function to fit.
  top = 0
  bot = 0
  a = [a]
  b = [b]
  for i in range(len(a)):
    top = top + a[i]*x**(2*i)
    bot = bot + b[i]*x**(2*i)
  return(top/bot)


def fitKelly(x, y, n):
  line = lambda params, x : kellyFunc(params[0,:], params[1,:], x) #Lambda Function to minimize
  error = lambda params, x, y : line(params, x) - y #Kelly - dataset

  paramsInit = [[1 for x in range(n)] for y in range(2)] #define all ai and bi = 1 for initial guess

  paramsFin, success = leastsq(error, paramsInit, args = (x,y)) #run leastsq optimization

  #line of best fit
  xx = np.linspace(x.min(), x.max(), 100)
  yy = line(paramsFin, xx)

  return(paramsFin, xx, yy)

目前它给我错误:
"IndexError: too many indices" 因为我用 params[0,:] 和 params[1,:] 定义初始 lambda 函数的方式。

你的方法有一些问题让我写了一个完整的答案。

至于你的具体问题:leastsq 并不真正期望多维数组作为参数输入。文档没有明确说明这一点,但参数输入在传递给 objective 函数时会被展平。您可以使用完整函数而不是 lambda 来验证这一点:

from scipy.optimize import leastsq           
import numpy as np

def kellyFunc(a, b, x): #Function to fit.
  top = 0
  bot = 0
  for i in range(len(a)):
    top = top + a[i]*x**(2*i)
    bot = bot + b[i]*x**(2*i)
  return(top/bot)

def line(params,x):
  print(repr(params)) # params is 1d!
  params = params.reshape(2,-1) # need to reshape back
  return kellyFunc(params[0,:], params[1,:], x)

def error(params,x,y):
  print(repr(params)) # params is 1d!
  return line(params, x) - y # pass it on, reshape in line()

def fitKelly(x, y, n):
  #paramsInit = [[1 for x in range(n)] for y in range(2)] #define all ai and bi = 1 for initial guess
  paramsInit = np.ones((n,2)) #better
  paramsFin, success = leastsq(error, paramsInit, args = (x,y)) #run leastsq optimization

  #line of best fit
  xx = np.linspace(x.min(), x.max(), 100)
  yy = line(paramsFin, xx)

  return(paramsFin, xx, yy)

现在,如您所见,params 数组的形状是 (2*n,) 而不是 (2,n)。通过自己进行重塑,您的代码(几乎)可以正常工作。当然 print 调用只是为了向您展示这一事实; 运行 的代码不需要它们(并且会在每次迭代中产生一堆不必要的输出)。

查看我的其他更改,与其他错误相关:您的 kellyFunc 中有 a=[a]b=[b],没有充分的理由。这将输入数组变成了包含数组的列表,这使得下一个循环执行的操作与您的预期非常不同。

最后,最偷偷摸摸的错误:你在fitKelly中输入了名为xy的变量,然后你使用xy是循环列表理解中的变量。请注意,这仅在 python 3 中按您预期的方式工作;在 python 2 中,列表理解的内部变量实际上泄漏到外部范围之外,覆盖了名为 xy.

的输入变量