gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for +: 'int' and 'str' (Logistic regression)

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:40 25 4
gpt4 key购买 nike

当我在 juypter notebook 中运行这个预测代码时,它运行完美,我通过 predict([1,2,1,0,1], [.4,1,3,.01,. 1]) 在单独的文件中,我得到了 0.995929862284 的正确答案,但是当使用单元测试时,我收到以下错误。

def dot(X, Y):
if len(X) != len(Y):
return 0

return sum(i[0] * i[1] for i in zip(X, Y))

def predict(features, weights):
x = dot(features, weights)
return logistic(x)

def test_predict(self):
model = [1,2,1,0,1]
point = {'features':[.4,1,3,.01,.1], 'label': 1}
p = predict(model, point)
self.assertAlmostEqual(p, 0.995929862284)

错误:

enter image description here

最佳答案

当您调用predict 时,您传递一个dict 作为第二个参数。但是,predict 将该参数按原样传递给 dot,它需要一个数字列表。因此,dot 迭代字典的键,而不是该字典的 features 值的值。 predict 应该被称为 predict(model, point['features'])。 (或者可能是 predict(point['features'], model),给定参数名称。)

关于python - 类型错误 : unsupported operand type(s) for +: 'int' and 'str' (Logistic regression),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49498324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com