python - scipy PchipInterpolator 错误 : 'array cannot be safely cast to required type'

标签 python numpy scipy interpolation type-conversion

我想对来自 pyaudio 的一些输入数据进行一些分段立方 hermite 插值:

#input comes as string and is converted to int16
numpyData = numpy.fromstring(inData, dtype=numpy.int16)

#x positions of my y data
x = numpy.arange(len(numpyData))

#as i always want to interpolate to 4096 these are my indices
interpolationIndices = numpy.linspace(0, len(numpyData), num=4096, endpoint=False)

#my interpolator and print for testing
f = interpolate.PchipInterpolator(x, numpyData)
print f(interpolationIndices[0])

错误日志现在显示:

TypeError: array cannot be safely cast to required type

scipy.interpolate.PchipInterpolator 的文档说它应该是一个实数值数组,但我知道 int16 应该足够真实,而且我的 x-pos 数组也是单调排序的。

附加信息:

print numpyData

结果:

[3153 2362 5361 ..., 3206 -849 3241]  

所以所有整数值都符合预期。

完整跟踪:

Traceback (most recent call last):  
File "/Users/myUser/Documents/workspace/myProject/AudioController.py", line 9, in callback  
return self.file.getAudio(frame_count, scaleMethod()), pyaudio.paContinue  
File "/Users/myUser/Documents/workspace/myProject/NewFileHandler.py", line 34, in getAudio
    data = self.resample(data)  
File "/Users/myUser/Documents/workspace/myProject/NewFileHandler.py", line 168, in resample
    f = interpolate.PchipInterpolator(x, numpyData)  
File "/Library/Python/2.7/site-packages/scipy/interpolate/polyint.py", line 932, in __init__
    data[:,1] = PchipInterpolator._find_derivatives(xp, yp)  
File "/Library/Python/2.7/site-packages/scipy/interpolate/polyint.py", line 982, in _find_derivatives
    PchipInterpolator._edge_case(mk[0],dk[1], dk[0])  
File "/Library/Python/2.7/site-packages/scipy/interpolate/polyint.py", line 947, in _edge_case
    out[mask] = 1.0/(1.0/m0[mask]+1.0/d1[mask])  
TypeError: array cannot be safely cast to required type

numpyData.dtype:

int16

numpy 版本:

print numpy.__version__
1.6.2

科学版:

print scipy.__version__
0.13.2

最佳答案

绝对是一个错误(见底部),并且可以在 numpy 1.6.2 中通过以下设置重现:

import scipy.interpolate
import numpy as np
y = np.array([0,1,2], dtype=np.int16)
x = np.arange(len(y))

然后引发异常:

>>> np.__version__
1.6.2
# code skipped
>>> scipy.interpolate.PchipInterpolator(x, y)
Traceback (most recent call last):
    ...
    out[mask] = 1.0/(1.0/m0[mask]+1.0/d1[mask])
TypeError: array cannot be safely cast to required type

虽然在 numpy 1.8.0 中按预期工作:

>>> np.__version__
1.8.0
# code skipped
>>> scipy.interpolate.PchipInterpolator(x, y)
<scipy.interpolate.polyint.PchipInterpolator object at 0x044371F0>

您的选择可能是升级 numpy。

附言这是一个 bool 索引错误,在跟踪器中没有找到,下面是一个简短的重现示例:

out = np.array([0])
out[np.array([True])] = np.array([.5])

关于python - scipy PchipInterpolator 错误 : 'array cannot be safely cast to required type' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800324/

相关文章:

python - 将 numpy 集成到 Meteorjs Web 应用程序中的最佳方法是什么?

python - 在python代码中访问在django中创建的表时出错

python - pandas dataframe中 `.value_counts()`的逆向操作是什么?

python - numpy.polyfit 是 1 度拟合,TLS 还是 OLS?

python - scipy.sparse.load_npz 属性错误

Python 代码在 Eclipse 中出错,但在终端中运行良好

python - Django:使用非模型数据时如何预填充表单?

python - 考虑到 GIL,asyncio 怎么可能不是线程安全的?

python - 遍历 NumPy 矩阵列表

python - 如何从 .csv 文件中提取数据并创建绘图?