这个问题似乎与:Python: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')重复 但是上面介绍的解决方案不起作用。
我目前正在https://github.com/executable16/audio-fingerprint-identifying-python上工作,因此避免在此处粘贴所有代码。我主要遇到错误:
Traceback (most recent call last):
File "recognize-from-microphone.py", line 139, in <module>
matches.extend(find_matches(channel))
File "recognize-from-microphone.py", line 132, in return_matches
yield (sid, offset - mapper[hash])
numpy.core._exceptions.UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('S21'), dtype('S21')) -> dtype('S21')
sqlite - connection has been closed
make: *** [Makefile:22: recognize-listen] Error 1
在我看来,该异常实际上很少说明这里的实际问题。我尝试了SO的其他解决方案,但它们似乎没有用。
确切的错误行是:yield (sid, offset - mapper[hash])
sid,offset和mapper [hash]的类型分别为<class 'int'> <class 'bytes'> and <class 'numpy.int64'>
。
对此问题进行任何适当解释的修补程序,将非常有帮助。
答案 0 :(得分:1)
我可以通过以下方式重现您的错误:
In [144]: type(b'123')
Out[144]: bytes
In [145]: type(np.int64(3))
Out[145]: numpy.int64
In [146]: b'123'-np.int64(3)
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
<ipython-input-146-bd8d8c3ec2cd> in <module>
----> 1 b'123'-np.int64(3)
UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('S21'), dtype('S21')) -> dtype('S21')
np.int64
变量已获得“控制权”,并将bytes
转换为数组,其中bytestring
dtype是常见的dtype。
如果mapper[hash]
产生一个Python编号,则会出现类似以下错误:
In [147]: b'123'-3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-147-04d1219bd464> in <module>
----> 1 b'123'-3
TypeError: unsupported operand type(s) for -: 'bytes' and 'int'
bytes
对象不支持减法。仅*和+:
In [149]: b'123'*3
Out[149]: b'123123123'
首先将bytes
转换为数字可能会解决您的问题:
In [150]: int(b'123')-np.int64(3)
Out[150]: 120