您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

TypeError:只能使用NUMPY将长度为1的数组转换为Python标量

TypeError:只能使用NUMPY将长度为1的数组转换为Python标量

使用numpy.sqrt而不是math.sqrtnumpy.sqrt期望标量或数组作为输入,另一方面math.sqrt只能处理标量。

>>> import numpy as np
>>> import math
>>> a = np.arange(5)
>>> np.sqrt(a)
array([ 0.        ,  1.        ,  1.41421356,  1.73205081,  2.        ])
#error
>>> math.sqrt(a)
Traceback (most recent call last):
  File "<ipython-input-78-c7d50051514f>", line 1, in <module>
    math.sqrt(a)
TypeError: only length-1 arrays can be converted to Python scalars

>>>
python 2022/1/1 18:37:54 有249人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶