简体   繁体   English

optimize.fmin 错误:IndexError:数组索引过多

[英]optimize.fmin error: IndexError: too many indices for array

I am trying to optimize a function in python, using optimize.fmin from scipy.我正在尝试使用 scipy 中的 optimize.fmin 优化 python 中的函数。 The function should optimize a vector of parameters, given initial conditions and arguments.在给定初始条件和参数的情况下,该函数应该优化参数向量。 However, I keep receiving the following error when I try to run the optimization, while running the function itself works:但是,当我尝试运行优化时,我不断收到以下错误,而运行该函数本身是有效的:

IndexError: too many indices for array, line 1, in parametrization IndexError:参数化中数组第 1 行的索引过多

In brief, my code is like:简而言之,我的代码是这样的:

import numpy as np # import numpy library
import pandas as pd # import pandas library
from scipy import optimize # import optimize from scipy library
from KF_GATSM import KF_GATSM # import script with Kalman filter

yields=pd.read_excel('data.xlsx',index_col=None,header=None) # Import observed yields
Omega0=pd.read_excel('parameters.xlsx') # Import initial parameters

# Function to optimize
def GATSM(Omega,yields,N):

     # recover parameters
     Omega=np.matrix(Omega)
     muQ,muP=parametrization(N,Omega) # run parametrization

Y=muQ+muP # or any other function

return Y

# Parametrization of the function
def parametrization(nstate,N,Omega):

     muQ=np.matrix([[Omega[0,0],0,0]]).T # intercept risk-neutral world
     muP=np.matrix([[Omega[1,0],Omega[2,0],Omega[3,0]]]).T # intercept physical world

     return muQ,muP

# Run optimization
def MLE(data,Omega0):

    # extract number of observations and yields maturities
    N=np.shape(yields)[1]

    # local optimization
     omega_opt=optimize.fmin(GATSM,np.array(Omega0)[:,0],args=(yields,N)) 

    return Y

I solved the issue.我解决了这个问题。 It seems that I cannot select the element of an array as follows in Scipy (although it works in Numpy):似乎我无法在 Scipy 中按如下方式选择数组的元素(尽管它在 Numpy 中有效):

Omega[0,0]
Omega[0]

The trick is to use:诀窍是使用:

Omega.item(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM