这个问题可能与Python重复:ufunc';添加';不包含签名匹配类型为dtype(';S21';(dtype(#39;S21';(dttype(';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')
在我看来,这个例外实际上告诉了这里的实际情况。我尝试过SO的其他解决方案,但似乎都不起作用。
确切的误差线是:yield (sid, offset - mapper[hash])
。
sid
、offset
和mapper[hash]
的类型分别为<class 'int'>
、<class 'bytes'>
和<class 'numpy.int64'>
。
任何对这个问题的适当解释都会非常有帮助。
我可以用重现您的错误
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
数据类型为常见数据类型。
如果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