GVKun编程网logo

Cython:“严重错误:numpy / arrayobject.h:没有此类文件或目录”(numpy has no attribute linespace)

2

如果您对Cython:“严重错误:numpy/arrayobject.h:没有此类文件或目录”和numpyhasnoattributelinespace感兴趣,那么这篇文章一定是您不可错过的。我们将详

如果您对Cython:“严重错误:numpy / arrayobject.h:没有此类文件或目录”numpy has no attribute linespace感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Cython:“严重错误:numpy / arrayobject.h:没有此类文件或目录”的各种细节,并对numpy has no attribute linespace进行深入的分析,此外还有关于AWS EC2 Nodejs |严重错误:无效的标记压缩接近堆限制分配失败-vuejs项目中的JavaScript堆内存不足、C++:找不到 numpy/arrayobject.h 和 Python.h、Cython 基础 --Cython 入门、Cython 循环优化 | Python 循环 => Cython 循环的实用技巧。

本文目录一览:

Cython:“严重错误:numpy / arrayobject.h:没有此类文件或目录”(numpy has no attribute linespace)

Cython:“严重错误:numpy / arrayobject.h:没有此类文件或目录”(numpy has no attribute linespace)

我试图加快答案在这里使用用Cython。我尝试编译代码(在完成此处cygwinccompiler.py介绍的hack之后),但出现错误。谁能告诉我我的代码是否有问题,或者Cython有点神秘?fatal error:numpy/arrayobject.h: No such file ordirectory...compilation terminated

下面是我的代码。

import numpy as npimport scipy as spcimport numpy as npcimport cythoncdef 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 ccdef 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()

答案1

小编典典

在你里面setup.pyExtension应该有论据include_dirs=[numpy.get_include()]

另外,您np.import_array()的代码中缺少您。

-

示例setup.py:

from distutils.core import setup, Extensionfrom Cython.Build import cythonizeimport numpysetup(    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()])

AWS EC2 Nodejs |严重错误:无效的标记压缩接近堆限制分配失败-vuejs项目中的JavaScript堆内存不足

AWS EC2 Nodejs |严重错误:无效的标记压缩接近堆限制分配失败-vuejs项目中的JavaScript堆内存不足

如何解决AWS EC2 Nodejs |严重错误:无效的标记压缩接近堆限制分配失败-vuejs项目中的JavaScript堆内存不足

我正在AWS ec2实例(t2.small)上运行nodejs应用。之后大约一个星期。以下是内存不足错误。搜索相同的问题,发现有一种解决方法可以增加“ --max-old-space-size”选项。但是,我的问题是

  1. 如果我将--max-old-space-size设置为4096(4gb)。我是否需要更新aws实例以获取更多的内存(因为小字体只有2 GB内存)
  2. 此max-old-space-size设置,最终还会达到内存限制吗?开始时,该应用程序运行良好,但一段时间后,该应用程序被压缩。如果那样,如何有效解决这个问题?

完整的错误消息如下:

[30600:0x5007800] 429029005 ms:扫频977.8(1007.7)-> 970.6(1008.9)MB,2379.3 / 0.5 ms(平均mu = 0.220,当前mu = 0.190)分配失败清除可能 成功了 [30600:0x5007800] 429031937毫秒:扫频979.0(1008.9)-> 971.8(1010.4)MB,2396.6 / 0.4毫秒(平均亩= 0.202,当前亩= 0.183)分配失败清除可能 成功了

致命错误:接近堆限制的无效标记紧凑分配失败-JavaScript堆内存不足 1:0xa02ec0节点:: Abort()[节点] 2:0x94e471 node :: FatalError(char const *,char const *)[node] 3:0xb765de v8 :: Utils :: ReportOOMFailure(v8 :: internal :: Isolate *,char const *,bool)[node] 4:0xb76957 v8 :: internal :: V8 :: FatalProcessOutOfMemory(v8 :: internal :: Isolate *,char const *,bool)[node] 5:0xd311f5 [node] 6:0xd31d7f [node] 7:0xd3fe0b v8 :: internal :: Heap :: CollectGarbage(v8 :: internal :: AllocationSpace,v8 :: internal :: GarbageCollectionReason,v8 :: GCCallbackFlags)[node] 8:0xd439cc v8 :: internal :: Heap :: AllocaterawWithRetryOrFailSlowPath(int,v8 :: internal :: AllocationType,v8 :: internal :: Allocationorigin,v8 :: internal :: AllocationAlignment)[否 de] 9:0xd120ab v8 :: internal :: Factory :: NewFillerObject(int,bool,v8 :: internal :: AllocationType,v8 :: internal :: Allocationorigin)[节点] 10:0x105802f v8 :: internal :: Runtime_AllocateInYoungGeneration(int,unsigned long *,v8 :: internal :: Isolate *)[节点] 11:0x13fdfd9 [节点] ./backend.sh:第6行:30600中止的节点server.js

感谢您的所有帮助!

C++:找不到 numpy/arrayobject.h 和 Python.h

C++:找不到 numpy/arrayobject.h 和 Python.h

如何解决C++:找不到 numpy/arrayobject.h 和 Python.h

我正在尝试编译以下 C++ 文件

  1. #include <iostream>
  2. #include <Python.h>
  3. #include <numpy/npy_math.h>
  4. using namespace std;
  5. int main()
  6. {
  7. cout << "Hello World" << endl; // prints value of i
  8. }

以下内容

  1. clang++ main.cpp -std=c++1y

它返回:main.cpp:3:10: Fatal error: ''Python.h'' file not found

然后我找到了这个解决方案 Python.h header file missing on Mac OS X 10.6 然后我将文件更改为

  1. #include <iostream>
  2. #include <Python/Python.h>
  3. #include <numpy/npy_math.h>
  4. using namespace std;
  5. int main()
  6. {
  7. cout << "Hello World" << endl; // prints value of i
  8. }

它有效,但出现了第二个问题:main.cpp:3:10: Fatal error: ''numpy/npy_math.h'' file not found

好的,然后我找到numpy的路径

  1. clang++ main.cpp -std=c++1y -I /Users/nicolas/opt/anaconda3/lib/python3.7/site-packages/numpy/core/include

然后,它返回

/Users/nicolas/opt/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/npy_common.h:11:10: 致命错误: ''Python.h'' 文件未找到 #include

有人可以帮我吗?

解决方法

您可以通过打开 npy_common.h 并将 #include <Python.h> 更改为 #include <Python/Python.h>#include <../../Python/Python.h> 或该文件的相对路径来快速解决此问题,甚至可以复制文件 Python .h 到 Users/nicolas/opt/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/ 文件夹

找不到文件的原因是npy_common.h在同一目录下寻找Python.h

Cython 基础 --Cython 入门

Cython 基础 --Cython 入门

Cython入门

1 Cython 是什么?

对你没有看错,是 Cython, 不是 Python

当初也我也对这个名字感到很奇怪,但是了解之后,再才知这是我一直想要的工具,比起 swig,这个工具要好上很多

它是一个用来快速生成 python 扩展模块 (extention module) 的工具

它的语法是 python 语言语法和 C 语言语法的混血

他比 swig 更容易编写 python 的扩展模块

也许你会说 swig 可以直接通过 c 的头文件生成扩展模块,但是 swig 对回调函数的支持不是很好,

另外,如果用 swig,很多情况下,你要写额外的代码将输入的参数转换成 python 对象以及将输出转成 python 对象,例如如果封装的一个 C 函数的参数是输入输出的话,又如如果 C 函数的参数中有回调函数的话

而在 Cython,C 里的类型,如 int,float,long,char * 等都会在必要的时候自动转成 python 对象,或者从 python 对象转成 C 类型,在转换失败时会抛出异常,这正是 Cython 最神奇的地方

另外,Cython 对回调函数的支持也很好。

总之,如果你有写 python 扩展模块的需求,那么 Cython 真的是一个很好的工具

2 安装

2.1 Windows 下安装

 先从 http://cython.org 下载软件包:Cython-0.19.2.zip

解压到一个目录,如 D:\Cython-0.19.2

从开始菜单里打开 Visual Studio Command Prompt,例如:MicroSoft Visual Studio  2010-> Visual Studio Tools -> Visual Studio Command Prompt (2010)

敲入如下命令:

D:

cd Cython-0.19.2

python setup.py install

命令执行完后就会把 cython 安装到 python 的安装目录下

2.2 Ubuntu 下安装

apt-get install cython

3 第一个例子:

3.1 创建 helloworld 目录

创建 helloworld.pyx, 内容如下:

cdef extern from"stdio.h":
    extern int printf(const char *format, ...)

def SayHello():
    printf("hello,world\n")

代码非常简单,就是调用了 C 函数 printf 打印 hello,world

4 如何编译

4.1 最方便的当然是利用 python 的 Distutils 了,看下如何来实现

先在 helloworld 目录下创建 Setup.py, 内容如下:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

setup(
  name = ''helloworld'',
  ext_modules=cythonize([
    Extension("helloworld", ["helloworld.pyx"]),
    ]),
)

编译:

python Setup.py build

安装:

python Setup.py install

安装后,会将在 build/lib.??? 目录下生成的 helloworld.pyd 拷贝到 Lib/site-packages

注:

  有时我们只是希望测试一下,并不希望安装,这时可以把 build/lib.??? 目录下的 helloworld.pyd 拷贝到当前目录

  或者在 import helloworld 前执行脚本:import sys;sys.path.append (pathof helloworld.pyd)

测试:

>>>import helloworld

>>>helloworld.SayHello()

hello,world

4.2 其次就是可以自己写 Makefile 进行编译

写 Makefile 的好处就是可以知道编译的实质:

下面是用于 Windows 下编译的 Makefile,Makefile 内容如下:

ALL :helloworld.pyd

helloworld.c : helloworld.pyx

     cython -o helloworld.c helloworld.pyx

helloworld.obj :helloworld.c

     cl -c -Id:\python27\include helloworld.c

helloworld.pyd :helloworld.obj

     link /DLL /LIBPATH:d:\python27\libshelloworld.obj /OUT:helloworld.pyd

执行命令:

set PATH=D:\Python27\Scripts;%PATH%

nmake

进行编译,会在根目录下生成 helloworld.pyd

linux 下的 Makefile 和 Windows 下的类似,只是编译器不同而己,另外,生成的文件名为:helloworld.so,而不是 helloworld.pyd

测试:

>>>import helloworld

>>>helloworld.SayHello()

hello,world

(完)

本文示例下载

Cython

Cython 循环优化 | Python 循环 => Cython 循环

Cython 循环优化 | Python 循环 => Cython 循环

如何解决Cython 循环优化 | Python 循环 => Cython 循环

我需要加速我的 Python 循环,我决定为此选择 Cython。不幸的是,我没有使用 Cython(和 C lang)的经验。我在 Cython 中创建快速循环的尝试失败了。

这是我的 Python 函数:

def backtest(data,min_distance):
    balance = 1000
    
    t1 = time()

    for shift_1 in range(1,101):
        for shift_2 in range(1,101):
            for shift_3 in range(1,101):
                if shift_2 > shift_1 * min_distance and shift_3 > shift_2 * min_distance:

                    pnls_shift_1 = data[shift_1 / 10]
                    pnls_shift_2 = data[shift_2 / 10]
                    pnls_shift_3 = data[shift_3 / 10]

                    for pnl in pnls_shift_1:
                        balance *= (pnls_shift_1[pnl] / 100) * (33 / 100)

                        if pnl in pnls_shift_2:
                            balance *= (pnls_shift_2[pnl] / 100) * (33 / 100)

                            if pnl in pnls_shift_3:
                                balance *= (pnls_shift_3[pnl] / 100) * (33 / 100)

    print(time() - t1)
    return balance

这是我尝试过的(Cython):

def backtest(dict data,float min_distance):
    cdef dict pnls_shift_1,pnls_shift_2,pnls_shift_3
    cdef int shift_1,shift_2,shift_3,pnl
    cdef int balance = 1000
    
    t1 = time()

    for shift_1 in range(1,101):
                if shift_2 > shift_1 * min_distance and shift_3 > shift_2 * min_distance:
                    pnls_shift_1 = data[shift_1 / 10]
                    pnls_shift_2 = data[shift_2 / 10]
                    pnls_shift_3 = data[shift_3 / 10]

                    for pnl in pnls_shift_1:
                        balance *= (pnls_shift_1[pnl] / 100) * (33 / 100)

                        if pnl in pnls_shift_2:
                            balance *= (pnls_shift_2[pnl] / 100) * (33 / 100)

                            if pnl in pnls_shift_3:
                                balance *= (pnls_shift_3[pnl] / 100) * (33 / 100)

    print(time() - t1)
    return balance

结果,Cython 中的执行时间比原生 Python 还要长。 我认为问题出在我使用的 Python 字典中,但我不确定。我试图从 dict 切换到 list,但它很糟糕,最后什么也没发生。

我的输入字典如下所示: {0.1: {1: -2.5,4: 3},0.2: {3: -1.5,4: 1,6: 0.24},... {10.0: {...}}}

如果有人建议在 Cython 中优化此循环,我将不胜感激。

关于Cython:“严重错误:numpy / arrayobject.h:没有此类文件或目录”numpy has no attribute linespace的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于AWS EC2 Nodejs |严重错误:无效的标记压缩接近堆限制分配失败-vuejs项目中的JavaScript堆内存不足、C++:找不到 numpy/arrayobject.h 和 Python.h、Cython 基础 --Cython 入门、Cython 循环优化 | Python 循环 => Cython 循环的相关信息,请在本站寻找。

本文标签: