Python 尝试比较数字时出现奇怪的错误
考虑下一段代码-Python 尝试比较数字时出现奇怪的错误,python,numpy,Python,Numpy,考虑下一段代码- def findBestHypothesis(): bestOfBest = [] currentERMValue = 0 bestERMValue = 0 for polynom in bestOfHypothesisClasses: for j in range(0, len(training_set)): currentERMValue += (np.polyval(polynom, traini
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是数字而不是数组。但我明白了-
如果np.greater_等于(currentERMValue,bestERMValue):值错误:错误
包含多个元素的数组的真值不明确。使用
a、 any()或a.all()
我不确定,但是np.polyval返回几个值 返回:
值:ndarray或poly1d
如果x是poly1d实例,则结果是两个多项式的合成,即x在p中被“替换”,并返回简化结果。此外,x-array_like或poly1d-的类型控制输出的类型:x-array_like=>values-array_like,x-a poly1d对象=>values也是
polyval
数组返回一个数组,每个x
值对应一个元素:
>>> np.polyval([1,2,3],[4,5,6])
array([27, 38, 51])
因此,要么训练集是二维数组,要么x值索引或y值索引不是标量。为什么不在内部
中的任何位置使用j
进行循环?如果在=
比较之前添加打印(type(currentERMValue))
,输出是什么样的?@MarkDickinson我怀疑training\u set
是一个2d数组,OP想要training\u set[j,x\u value\u index]
而不是training\u set[x\u value\u index]
(同样的y\u value\u index
).training_set是一个2元组数组,我想计算training_set[x_value_index]中的多项式,它是一个scalar@AlexGoft:如果training\u set
是一个2元组数组,那么training\u set[某些索引]
不是一个2元组吗?我不明白你是如何从中得到标量的。在我将它修正到training\u set[j][x\u value\u index]
@AlexGoft:你是否也为表达式的y\u value\u index
部分添加了j
?