传递一个预期单值的数组?

时间:2016-10-12 19:16:11

标签: python numpy machine-learning scipy regression

我正在尝试在Python中实现简单的优化问题。我目前正在努力解决以下错误:

值错误:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()

据我所知,这意味着我在某处试图插入一个只能接受单个值的数组。尽管如此,我还没有设法找到解决方案,也没有发现问题出在哪里。

我的代码如下

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

我试图通过计算成本函数并相对于θ最小化来获得最优正则化参数。 我的输入值是:

X.shape = (100,25), y.shape = (100,1)
Xval.shape = (55,25), yval.shape = (55,1)

输出的错误是:

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

稍后我将不会使用优化模型来预测新X上的y。 您能否告诉我代码中的问题在哪里?

此外,如果您在我的代码中发现任何需要改进的地方,请告知我们。我很乐意听到并改进。

谢谢!

1 个答案:

答案 0 :(得分:0)

Nimitz14得出了一般解释:你已经提供了一个需要标量的数组。这导致运行时错误。问题是如何从这里解决它。

首先,尝试在问题上使用您最喜欢的调试器,这样您就可以在有用的位置停止程序,并确切地找出导致问题的 数组。这应该可以帮助您确定它的来源。

如果失败,请在调用路径上放置一些策略性打印语句,在函数调用之前打印每个参数。然后检查每个函数的签名(调用序列),并查看给定数组代替标量的位置。我不明白,但是......

您是否有可能以某种方式将 True 重新定义为数组?