比较数字时出现奇怪的错误

2024-09-27 00:15:10 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑下一段代码-

def findBestHypothesis():

    bestOfBest = []
    currentERMValue = 0
    bestERMValue = 0

    for polynom in bestOfHypothesisClasses:
        for j in range(0, len(training_set)):
            currentERMValue += (np.polyval(polynom, training_set[x_value_index]) -
                                training_set[y_value_index])**2

        if currentERMValue >= bestERMValue:
            bestERMValue = currentERMValue
            currentERMValue = 0
            bestOfBest = polynom

    return bestOfBest

如您所见,currentERMValue和bestERMValue是数字而不是数组。但我明白了-

if np.greater_equal(currentERMValue, bestERMValue): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


Tags: 代码inforindexifvaluedefnp
2条回答

数组上的polyval返回一个数组,每个x值对应一个元素:

>>> np.polyval([1,2,3],[4,5,6])
array([27, 38, 51])

所以要么训练集是二维数组,要么x值索引或y值索引不是标量。你知道吗

我不确定,但是np.polyval公司返回多个值。你知道吗

返回:
值:ndarray或poly1d
如果x是poly1d实例,则结果是两个多项式的合成,即x在p中被“替换”,并返回简化结果。此外,x-array-like或poly1d-的类型控制输出的类型:x array-like=>;values array-like,x a poly1d object=>;values也是。
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.polyval.html

相关问题 更多 >

    热门问题