Python Scipy - scipy.optimize.minimize(errors)

时间:2018-01-23 14:58:05

标签: python scipy

我正在做一些非线性最小二乘回归,我有两个错误。

这是代码:

import numpy as np
from numpy import array
import scipy

total = sqlDf2['Total'].values

def lsq(arg):
    a = arg[0]*100
    b = arg[1]*100
    p = 1-(1/(10**(a*(total**(b-1)))))
    print(p)
    return p

guesses = np.array([.05, 1])
res = scipy.optimize.minimize(lsq, guesses)

print(res.message)

print(res.x)

print([lsq(guesses), lsq(res.x)])

这是总数据:

array([ 1,  2,  1,  1,  1,  5,  5,  5,  1,  1,  3,  1,  1,  1,  2,  1, 16,
        5,  8,  1,  2,  1, 16,  2,  1,  1,  1,  2,  1,  4,  1,  5,  2,  3,
        1,  1,  5,  3,  1,  3, 26,  3,  1, 16,  4,  1,  3,  1,  1,  1,  5,
       22,  1,  7,  1,  2,  1,  5,  3,  1,  9,  1,  2,  2,  9,  2,  1, 16,
       13,  1,  1,  1,  1,  7,  1,  1,  1,  8,  1,  1,  2,  1,  1,  2,  1,
       28,  1,  2,  1,  1, 57, 10,  1,  1, 11,  2, 20,  2,  1, 21, 19,  1,
        2,  3,  6, 15, 10, 10, 23,  2,  1, 10, 18,  5,  3, 13,  1,  1,  1,
        1,  1,  2,  5,  3, 23,  6,  1,  7,  2, 15,  1, 19,  6,  7,  6,  1,
        8, 13,  1, 27,  4,  4, 15,  1,  1,  2,  1,  1,  2,  1,  1], dtype=int64)

这是错误:

ValueError: setting an array element with a sequence.

overflow encountered in power

我做了一些谷歌搜索,无法弄清楚我做错了什么。谢谢

这是完整的追溯:

   ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-105-70662b656272> in <module>()
     13 
     14 guesses = [.05, 1]
---> 15 res = scipy.optimize.minimize(lsq, guesses)
     16 
     17 print(res.message)

~\AppData\Local\Continuum\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,

~\AppData\Local\Continuum\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)

~\AppData\Local\Continuum\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

~\AppData\Local\Continuum\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 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\optimize\optimize.py in _approx_fprime_helper(xk, f, epsilon, args, f0)
    626         ei[k] = 1.0
    627         d = epsilon * ei
--> 628         grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]
    629         ei[k] = 0.0
    630     return grad

ValueError: setting an array element with a sequence.

0 个答案:

没有答案