Search code examples
pythonnumpymachine-learningscipylogistic-regression

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


This was my piece of code initially :

Here X is the array of data points with dimensions (m x n) where m is number of data points to predict, and n is number of features without the bias term.

y is the data labels with shape (m,)

lambda_ is the regularization term.

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_)

The error I got was :

---------------------------------------------------------------------------
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.

Then after debugging, I changed the code to :

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

Now the error I am getting is :

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

How can I correct this?


Solution

  • You create all_theta running:

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

    This instruction actually creates an array containig just 2 elements (the shape is (2,)), containing two passed values, whereas you probably intend to pass the shape of the array to be created.

    Change this instruction to:

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

    Specification of dtype (in my opinion) is not necessary.