问题描述
我正在尝试使用Cython在此处加快答案.我尝试编译代码(执行cygwinccompiler.py
hack解释),但出现fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated
错误.谁能告诉我我的代码是否有问题,或者Cython有点神秘?
I'm trying to speed up the answer here using Cython. I try to compile the code (after doing the cygwinccompiler.py
hack explained here), but get a fatal error: numpy/arrayobject.h: No such file or directory...compilation terminated
error. Can anyone tell me if it's a problem with my code, or some esoteric subtlety with Cython?
下面是我的代码.
import numpy as np
import scipy as sp
cimport numpy as np
cimport cython
cdef inline np.ndarray[np.int, ndim=1] fbincount(np.ndarray[np.int_t, ndim=1] x):
cdef int m = np.amax(x)+1
cdef int n = x.size
cdef unsigned int i
cdef np.ndarray[np.int_t, ndim=1] c = np.zeros(m, dtype=np.int)
for i in xrange(n):
c[<unsigned int>x[i]] += 1
return c
cdef packed struct Point:
np.float64_t f0, f1
@cython.boundscheck(False)
def sparsemaker(np.ndarray[np.float_t, ndim=2] X not None,
np.ndarray[np.float_t, ndim=2] Y not None,
np.ndarray[np.float_t, ndim=2] Z not None):
cdef np.ndarray[np.float64_t, ndim=1] counts, factor
cdef np.ndarray[np.int_t, ndim=1] row, col, repeats
cdef np.ndarray[Point] indices
cdef int x_, y_
_, row = np.unique(X, return_inverse=True); x_ = _.size
_, col = np.unique(Y, return_inverse=True); y_ = _.size
indices = np.rec.fromarrays([row,col])
_, repeats = np.unique(indices, return_inverse=True)
counts = 1. / fbincount(repeats)
Z.flat *= counts.take(repeats)
return sp.sparse.csr_matrix((Z.flat,(row,col)), shape=(x_, y_)).toarray()
推荐答案
在您的setup.py
中,Extension
应该具有参数include_dirs=[numpy.get_include()]
.
In your setup.py
, the Extension
should have the argument include_dirs=[numpy.get_include()]
.
而且,您的代码中缺少np.import_array()
.
Also, you are missing np.import_array()
in your code.
-
setup.py示例:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
ext_modules=[
Extension("my_module", ["my_module.c"],
include_dirs=[numpy.get_include()]),
],
)
# Or, if you use cythonize() to make the ext_modules list,
# include_dirs can be passed to setup()
setup(
ext_modules=cythonize("my_module.pyx"),
include_dirs=[numpy.get_include()]
)
这篇关于Cython:“致命错误:numpy/arrayobject.h:没有这样的文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!