python - 类型错误 : unsupported operand type(s) for -: 'float' and 'NoneType' python

标签 python

有谁知道为什么我会收到这样的错误?!如果你愿意,我将非常感激,我是新手,正在努力学习,但我真的陷入了 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

最佳答案

根据您提供的内容和错误,这是我的结论。

唯一使用 - 操作数的地方是在两个点中

sq_err = (rating- predicted_rating) * (rating - predicted_rating)

因为错误状态为 'float' 和 'NoneType' 我们可以得出结论 rating 是类型 floatpredicted_ratingNoneType

您将 predicted_rating 定义为:

predicted_rating = simple_nn(person_id, place_id, 5)

所以这意味着在函数 simple_nn 的代码中,您没有返回任何内容。也许如果您使用条件,则不会评估每个结束路径,并且函数只是返回。

例如...所有这些函数都返回 None 类型。

def example1():
    pass

def example2():
    return

def example3(a = True, b  = True):
    if not a:
        return True
    elif not b:
        return False

注意在最后一个例子中有一个路径,如果 case 不满足,.. 因此它可以返回 None

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

相关文章:

python - 约束二阶导数时,Scipy CubicSpline 外推如何工作?

Python super().__init__() 类型错误 : super() takes at least 1 argument (0 given)

python - SQL Server DATE 作为字符串检索到 pandas

python - 删除 pandas 数据框中的重复项后替换特定列值

python - 如何设置 tf.keras.layers.RandomFlip 的可能性?

python - 获取与静态类型检查器一起使用的 TypedDict 值类型的函数

python - Python 的重试装饰器的动态参数

python - 如果文件名更改则中止执行

python - 如何将包含列表列表的文件转换为字符串?

python - 页眉/页脚终端显示