PYTHON : IndexError: index 2 is out of bounds for axis 0 with size 2

PYTHON : IndexError: index 2 is out of bounds for axis 0 with size 2

这是我最初的一段代码:

此处 X 是维度为 (m x n) 的数据点数组,其中 m 是要预测的数据点数,n 是没有偏差项的特征数。

y是形状为(m,)的数据标签

lambda_ 是正则化项。

from scipy import optimize
def oneVsAll(X,y,num_labels,lambda_):
    #used to find the optimal parametrs theta for each label against the others
    #X (m,n)
    #y (m,)
    #num_labels : possible number of labels
    #lambda_ : regularization param
    #all_theta : trained param for logistic reg for each class
    #hence (k,n+1) where k is #labels and n+1 is #features with bias
    
    m,n = X.shape
    all_theta = np.array((num_labels,n+1))
    X = np.concatenate([np.ones((m,1)),X],axis = 1)
    for k in np.arange(num_labels):
        #y == k will generate a list with shape of y,but 1 only for index with value same as k and rest with 0
        initial_theta = np.zeros(n+1)
        options = {"maxiter" : 50}
        res = optimize.minimize(lrCostFunction,
                                initial_theta,args = (X,y==k,lambda_),
                                jac = True,method = 'CG',
                                options = options)
        all_theta[k] = res.x
    return all_theta

lambda_ = 0.1
all_theta = oneVsAll(X,y,num_labels,lambda_)

我得到的错误是:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-f9501694361e> in <module>()
      1 lambda_ = 0.1
----> 2 all_theta = oneVsAll(X,y,num_labels,lambda_)

<ipython-input-44-05a9b582ccaf> in oneVsAll(X, y, num_labels, lambda_)
     20                                 jac = True,method = 'CG',
     21                                 options = options)
---> 22         all_theta[k] = res.x
     23     return all_theta

ValueError: setting an array element with a sequence.

然后经过调试,我把代码改成了:

from scipy import optimize
def oneVsAll(X,y,num_labels,lambda_):
    #used to find the optimal parametrs theta for each label against the others
    #X (m,n)
    #y (m,)
    #num_labels : possible number of labels
    #lambda_ : regularization param
    #all_theta : trained param for logistic reg for each class
    #hence (k,n+1) where k is #labels and n+1 is #features with bias
    
    m,n = X.shape
    all_theta = np.array((num_labels,n+1),dtype = "object")
    X = np.concatenate([np.ones((m,1)),X],axis = 1)
    for k in np.arange(num_labels):
        #y == k will generate a list with shape of y,but 1 only for index with value same as k and rest with 0
        initial_theta = np.zeros(n+1)
        options = {"maxiter" : 50}
        res = optimize.minimize(lrCostFunction,
                                initial_theta,args = (X,y==k,lambda_),
                                jac = True,method = 'CG',
                                options = options)
        all_theta[k] = res.x
    return all_theta

现在我得到的错误是:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-47-f9501694361e> in <module>()
      1 lambda_ = 0.1
----> 2 all_theta = oneVsAll(X,y,num_labels,lambda_)

<ipython-input-46-383fc22e26cc> in oneVsAll(X, y, num_labels, lambda_)
     20                                 jac = True,method = 'CG',
     21                                 options = options)
---> 22         all_theta[k] = res.x
     23     return all_theta

IndexError: index 2 is out of bounds for axis 0 with size 2

我该如何纠正?

您创建 all_theta 运行:

all_theta = np.array((num_labels,n+1),dtype = "object")

这条指令实际上创建了一个仅包含 2 个元素的数组 (形状是 (2,)),包含两个传递值,而您可能 打算传递要创建的数组的形状

将此说明更改为:

all_theta = np.empty((num_labels,n+1))

dtype 的规范(在我看来)不是必需的。