【问题标题】:PYTHON : IndexError: index 2 is out of bounds for axis 0 with size 2PYTHON:IndexError:索引 2 超出轴 0 的范围,大小为 2
【发布时间】:2020-11-22 17:44:44
【问题描述】:

这是我最初的一段代码:

这里 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

我该如何纠正这个问题?

【问题讨论】:

    标签: python numpy machine-learning scipy logistic-regression


    【解决方案1】:

    你创建 all_theta 运行:

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

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

    将此指令更改为:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      相关资源
      最近更新 更多