gpt4 book ai didi

python - 错误 "TypeError: type numpy.ndarray doesn' t 定义 __round__ 方法”

转载 作者:太空狗 更新时间:2023-10-29 20:12:05 30 4
gpt4 key购买 nike

import numpy

......

# Prediction
predictions = model.predict(X_test)
# round predictions
rounded = [round(x) for x in predictions]
print(rounded)

"predictions" is a list of decimals between [0,1] with sigmoid output.

为什么总是报这个错:

  File "/home/abigail/workspace/ml/src/network.py", line 41, in <listcomp>
rounded = [round(x) for x in predictions]
TypeError: type numpy.ndarray doesn't define __round__ method

如果我不使用“圆”,它会正确打印小数。这个“圆”应该是 Python 内置函数。为什么它与 numpy 有任何关系?

编辑:

for x in predictions:
print(x, end=' ')

输出是:

    [ 0.79361773] [ 0.10443521] [ 0.90862566] [ 0.10312044] [ 0.80714297] 
[ 0.23282401] [ 0.1730803] [ 0.55674052] [ 0.94095331] [ 0.11699325]
[ 0.1609294]

最佳答案

TypeError: type numpy.ndarray doesn't define round method

您尝试将轮次应用于 numpy.ndarray。显然,这不受支持。

试试这个,使用 numpy.round:

rounded = [numpy.round(x) for x in predictions]

x 是 numpy 数组。你也可以试试这个:

rounded = [round(y) for y in x for x in predictions]

关于python - 错误 "TypeError: type numpy.ndarray doesn' t 定义 __round__ 方法”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319292/

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