Passing an array where single value is expected?
<p>I am trying to implement simple optimization problem in Python. I am currently struggling with the following error:</p>

<p><strong>Value Error</strong>: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</p>

<p>As far as I understand, this means that I am somewhere trying to plug in an array where only single value can be accepted. Nevertheless, I haven't managed to come up with a solution, nor have I discovered where is the problem.</p>

<p>My code follows</p>

<pre><code>def validationCurve(X, y, Xval, yval):
    #[lmbda_vec, error_train, error_val] = 
    # VALIDATIONCURVE(X, y, Xval, yval) returns the train and 
    # validation errors (in error_train, error_val) for different
    # values of lmbda. Given the training set (X,y) and validation 
    # set (Xval, yval).

    lmbda_vec = [0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1];
    m = len(y);
    X = numpy.concatenate((numpy.ones((m,1)), X), axis = 1);
    n = len(Xval);
    Xval = numpy.concatenate((numpy.ones((n,1)), Xval), axis = 1);

    error_train = numpy.zeros((len(lmbda_vec), 1));
    error_val = numpy.zeros((len(lmbda_vec), 1));


    for i in range(0,len(lmbda_vec)):
        lmbda = lmbda_vec[i];
        theta = trainLinearReg(X, y, lmbda);
        error_train[i] = linearRegCostFunction(X, y, theta, lmbda);
        error_val[i] = linearRegCostFunction(Xval, yval, theta, lmbda);
    return lmbda_vec, error_train, error_val


def trainLinearReg(X, y, lmbda):
    #[theta] = TRAINLINEARREG (X, y, lmbda) trains linear 
    # regression usingthe dataset (X, y) and regularization 
    # parameter lmbda. Returns the trained parameters theta.

    alpha = 1 # learning rate
    num_iters = 200 # number of iterations
    initial_theta = (numpy.zeros((len(X[0,:]),1))) #initial guess

    #Create "short hand" for the cost function to be minimized
    costFunction = lambda t: linearRegCostFunction(X, y, t, lmbda);
    #Minimize using Conjugate Gradient
    theta = minimize(costFunction, initial_theta, method = 'Newton-CG',
    jac = True, options = {'maxiter': 200})
    return theta

def linearRegCostFunction(X, y, theta, lmbda):
    # [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lmbda)
    # computes the cost of using theta as the parameter for 
    # linear regression to fit the data points in X and y. 
    # Returns the cost in J and the gradient in grad.

    # Initialize some useful values
    m, n = X.shape; # number of training examples
    J = 0;
    grad = numpy.zeros((n ,1))

    J = numpy.dot((y- X @ theta).T, (y-X @ theta)) + 
    lmbda*(theta[1:].T @ theta[1:])
    J = J/m

    grad = (X.T @ (y - X @ theta))/m
    grad [1:] += (lmbda*theta[1:])/m


    grad = grad[:];
    return grad
</code></pre>

<p>I am trying to obtain an optimal regularization parameter by computing cost function and minimizing with respect to theta.
My input values are:</p>

<pre><code>X.shape = (100,25), y.shape = (100,1)
Xval.shape = (55,25), yval.shape = (55,1)
</code></pre>

<p>Outputted errors are:</p>

<pre><code>    --&gt; 129 lmbda_vec , error_train, error_val = validationCurve(Xtrain, ytrain, Xva
    lid, yvalid )
    ---&gt; 33 theta = trainLinearReg(X, y, lmbda);
    ---&gt; 49 theta = minimize(costFunction, initial_theta, 
                    method = 'Newton-CG', jac = True, options = {'maxiter': 200})
</code></pre>

<p>Later I won't to use the optimized model to predict y on new X.
<strong>Could you please advice me where is the problem in my code?</strong></p>

<p>Also, if you observe any points for improvement in my code, please let me know. I will be glad to hear and improve.</p>

<p>Thank you!</p>