我在破解此cython错误时遇到了一些麻烦:
---> 83 return RVKDE_loop(S, X, sigmas, n, m)
_cython_magic_e23143d4f8f8ff2526f1f1d1d47eacfb.pyx in _cython_magic_e23143d4f8f8ff2526f1f1d1d47eacfb.RVKDE_loop()
TypeError: unsupported operand type(s) for -: 'float' and '_cython_magic_e23143d4f8f8ff2526f1f1d1d47eacfb._memoryviewslice'
以下是产生错误的cython spaghetti代码:
%%cython
cimport cython
import numpy as np
from libc.stdio cimport printf
ctypedef fused dfloat:
cython.float
cython.double
int
def RVKDE_loop(dfloat[:] S, dfloat[:] X, dfloat[:] sigmas, int n, int m):
total = []
for i in range(n):
norms = -(abs(X[i]-S)**2)
exp_denom = (2*(sigmas[i]**2))
a_denom = ((sigmas[i]*np.sqrt(2*np.pi))**m)
a = (1/a_denom)
value = a*np.exp(norms/exp_denom)
total += [value]
return (1/n)*np.sum(total,axis=1)
以下是一些示例输入
S: np.array([-1.3015387, -0.07296862, 0.2387931, 0.38824359, 0.47182825, 0.75062962,
1.3190391, 1.86540763, 2.62434536, 2.74481176])
X: np.array([-5., -3.77777778, -2.55555556, -1.33333333, -0.11111111, 1.11111111,
2.33333333, 3.55555556, 4.77777778, 6. ])
sigmas: np.array([2.43158064, 0.59796393, 0.27334147, 0.14160981, 0.14160981, 0.53204325,
1.06717756, 1.06717756, 0.21537329, 0.21537329])
n: 10
m: 1
任何有关如何解决此问题的技巧都将非常有用。请注意,当它是常规python代码时,cython函数会执行我想要的操作;我喜欢普通的python有点慢。