TypeError: 不支持的操作数类型 -: 'float' 和 'NoneType' python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23037408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' python
提问by MB.
Does anyone know why I would be getting an error like this?! I would really appreciate it if you do, I am new to this and trying to learn but im getting really caught up in the nitty gritty of python! this is the error I am getting:
有谁知道为什么我会收到这样的错误?!如果你这样做,我真的很感激,我是新手,正在努力学习,但我真的陷入了 python 的本质!这是我得到的错误:
eError: unsupported operand type(s) for -: 'float' and 'NoneType'
for test in test_set:
person_id = test['person_id']
place_id = test['place_id']
rating = test['rating']
predicted_rating = simple_nn(person_id, place_id, 5)
#difference = (rating- predicted_rating)
sq_err = (rating- predicted_rating) * (rating - predicted_rating)
sq_err_sum = sq_err
sq_err_sum = sq_err_sum + sq_err
rmse = math.sqrt(sq_err_sum/5)
print rmse
回答by flakes
Judging by what you've provided, and the error this is my conclusion.
根据您提供的内容和错误判断,这是我的结论。
The only place you use the -
operand is in the two points
唯一使用-
操作数的地方是两点
sq_err = (rating- predicted_rating) * (rating - predicted_rating)
because the error states 'float' and 'NoneType'
we can conclude that rating
is type float
and predicted_rating
is NoneType
.
因为错误状态'float' and 'NoneType'
我们可以得出结论rating
是 typefloat
和predicted_rating
is NoneType
。
You defined predicted_rating
as:
你定义predicted_rating
为:
predicted_rating = simple_nn(person_id, place_id, 5)
So this means that somewhere in your code for the function simple_nn
you are not returning anything. Perhaps if you used conditions you didn't evaluate every end path and the function simply returned.
所以这意味着在你的函数代码中simple_nn
你没有返回任何东西。也许如果你使用了条件,你就没有评估每个结束路径,函数只是简单地返回。
for example... all of these functions return the None type.
例如...所有这些函数都返回 None 类型。
def example1():
pass
def example2():
return
def example3(a = True, b = True):
if not a:
return True
elif not b:
return False
Note in the last example there is a path where neither if case is satisfied,.. thus it could return None
请注意,在最后一个示例中,有一条路径既不满足 if case,.. 因此它可以返回 None
回答by Ch HaXam
you can also use import division
你也可以使用进口部门
from _ _ future _ _ import division
从_ _ 未来_ _ 进口司
it worked for me in same case
在同样的情况下它对我有用