'numpy.ndarray'对象不是带有optimize.minimize的可调用错误

时间:2018-11-23 10:09:17

标签: python python-3.x numpy scipy minimize

我想使用简单的sin ^ 2函数拟合一组数据,并希望根据拟合的参数确定其最小值。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize

data = np.loadtxt('data.txt', usecols=(0,1))
x = data[:,0]*np.pi/180
y = data[:,1]

plt.scatter(x, y, c='red')

def sine(t,a,b,c):
    return a*(np.sin(b*(t-c)))**2

params, cov = optimize.curve_fit(sine, x, y, p0=[9500, 0.5, 0])
print(params)

t = np.linspace(0, 2*np.pi/3, 120) 
plt.plot(t, sine(t, *params), 'black')

plt.show()

optimize.minimize(sine(t, *params), x0=0)

除了minimize调用之外,其他所有东西都很好,因为出现以下错误(具有完整的追溯):

TypeError                                 Traceback (most recent call last)
~\Documents\CNR\Calibrazione_lamine_20181112\Fit.py in <module>()
     23 plt.show()
     24 
---> 25 optimize.minimize(sine(t, *params), x0=0)

~\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    442         return _minimize_cg(fun, x0, args, jac, callback, **options)
    443     elif meth == 'bfgs':
--> 444         return _minimize_bfgs(fun, x0, args, jac, callback, **options)
    445     elif meth == 'newton-cg':
    446         return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,

~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
    911     else:
    912         grad_calls, myfprime = wrap_function(fprime, args)
--> 913     gfk = myfprime(x0)
    914     k = 0
    915     N = len(x0)

~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in function_wrapper(*wrapper_args)
    290     def function_wrapper(*wrapper_args):
    291         ncalls[0] += 1
--> 292         return function(*(wrapper_args + args))
    293 
    294     return ncalls, function_wrapper

~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in approx_fprime(xk, f, epsilon, *args)
    686 
    687     """
--> 688     return _approx_fprime_helper(xk, f, epsilon, args=args)
    689 
    690 

~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in _approx_fprime_helper(xk, f, epsilon, args, f0)
    620     """
    621     if f0 is None:
--> 622         f0 = f(*((xk,) + args))
    623     grad = numpy.zeros((len(xk),), float)
    624     ei = numpy.zeros((len(xk),), float)

~\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in function_wrapper(*wrapper_args)
    290     def function_wrapper(*wrapper_args):
    291         ncalls[0] += 1
--> 292         return function(*(wrapper_args + args))
    293 
    294     return ncalls, function_wrapper

TypeError: 'numpy.ndarray' object is not callable.

我想念一些东西,但我不知道。


我正在按照建议添加数据文件以使该程序运行

0   405
5   20
10  350
15  1380
20  2900
25  4750
30  6450
35  8100
40  9100
45  9800
50  10100
55  10250
60  9400
65  8400
70  6430
75  4900
80  3030
85  1500
90  400
95  17
100 410
105 1550
110 3100
115 4850
120 6780

2 个答案:

答案 0 :(得分:1)

minimize期望将函数作为第一个参数,但是,您当前正在传递

sine(t, *params)

这是一个numpy数组。

您可以解决此问题并执行以下操作:

print(optimize.minimize(sine, x0=[0], args=tuple(params)))

这将打印

      fun: 2.4080485986582715e-12
 hess_inv: array([[1.15258817e-05]])
      jac: array([8.19961349e-09])
  message: 'Optimization terminated successfully.'
     nfev: 18
      nit: 4
     njev: 6
   status: 0
  success: True
        x: array([0.09203053])

答案 1 :(得分:0)

在scipy文档中,optimize.minimize函数将ndarrayshape(n)作为x,的输入,而不是整数。我认为错误是从那里引发的,因为在他们的错误跟踪中

--> 913     gfk = myfprime(x0)

此功能引发了错误。

文档link