gpt4 book ai didi

python - 数字号码 : inputs could not be coerced to any supported types according to the casting rule '' safe''

转载 作者:行者123 更新时间:2023-11-30 22:20:07 25 4
gpt4 key购买 nike

我最近一直在与 numba 作斗争。直接从 numba 文档复制此代码片段,它工作正常:

@guvectorize([(int64[:], int64, int64[:])], '(n),()->(n)')
def g(x, y, res):
for i in range(x.shape[0]):
res[i] = x[i] + y

a = np.arange(5)
g(a,2)

给 y 一个数组会产生一个网格。对 2 个数组求和是我经常做的事情,所以这是我通过修改代码片段得出的代码。

@guvectorize([(int64[:], int64[:], int64[:])], '(n),(n)->(n)')
def add_arr(x, y, res):
for i in range(x.shape[0]):
res[i] = x[i] + y[i]

p = np.ones(1000000)
q = np.ones(1000000)
r = np.zeros(1000000)

add_arr(p,q)

这给了我错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-75-074c0fd345aa> in <module>()
----> 1 add_arr(p,q)

TypeError: ufunc 'add_arr' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我之前曾多次遇到此错误,但我不知道它意味着什么或如何修复它。我怎样才能得到想要的结果?提前致谢。

最佳答案

您正在使用 numpy.ones 生成一个列表,并根据文档( https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html ):

dtype : data-type, optional

The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.

np.ones(1000000)numpy.float64 的列表。但是您的 add_arr 规范需要 int64 列表,因此 TypeError 会爆炸。

一个简单的修复:

p = np.ones(1000000, dtype=np.int64)
q = np.ones(1000000, dtype=np.int64)

关于python - 数字号码 : inputs could not be coerced to any supported types according to the casting rule '' safe'',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918928/

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