从函数式语言访问 numpy 数组
我的主要语言是 Python。通常,当我需要在 numpy 数组上执行一些 CPU 繁重的任务时,我使用 scipy.weave.inline 来连接 C++,并取得很好的结果。
我怀疑许多算法(机器学习的东西)可以用函数语言(scheme、haskell...)编写得更简单。
我在想。是否可以从函数式语言访问 numpy 数组数据(读取和写入),而不必使用 C++?
My primary language is Python. Often when I need to do some cpu heavy task on a numpy array I use scipy.weave.inline to hook up c++ with great results.
I suspect many of the algorithms (machine learning stuff) can however be written simpler in a functional language (scheme, haskell...).
I was thinking. Is it possible to access numpy array data (read and write) from a functional language instead of having to use c++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会考虑使用某种共享内存数组。这个实现可能是一个很好的起点: https://bitbucket.org/cleemesser/numpy- sharedmem/src
此实现旨在在 python 进程之间共享,但它使用命名共享内存来实现,因此您应该能够从任何其他进程访问相关的内存块。
我对 haskell 不太熟悉,无法给你这方面的任何建议,但我假设你可以使用指向共享内存缓冲区的指针作为 haskell 中某种数组......
You might have a look at using a shared-memory array of some sort. This implementation would probably be a good place to start: https://bitbucket.org/cleemesser/numpy-sharedmem/src
This implementation is intended to be shared between python processes, but it's using named shared memory to do it, so you should be able to access the relevant chunk of memory from any other process.
I'm not familiar enough with haskell to give you any advice on that side, but I assume you can use a pointer to a shared memory buffer as an array of some sort in haskell...
目前还没有从 Python 调用 Haskell 的单一标准方法。当然有一些方法可以从 C 调用 haskell,这意味着调用 Haskell 原则上没有障碍——只是还没有完成让这变得特别容易的工作。
另一方面,如果您的数据结构本身并不庞大,那么将它们序列化为 Haskell 程序(通过命令行,或使用带有 thrift 等的客户端-服务器模型)非常简单,并且如果计算成本为凡是充分占主导地位的,成本可能是最小的。
最后,从 Haskell 调用 Python 非常容易!缺少此功能的经典包: http://hackage.haskell.org/package/MissingPy
还有一个名为 cpython 的新包,它试图变得更全面:http://hackage.haskell.org/package/cpython
从概念上讲,我想,在 Haskell 中托管 Python 应用程序应该不会很困难,而不是相反。
There's no single standard way to call Haskell from Python at the moment. There are certainly ways to call haskell from C, which means there's no obstacle in principle to calling Haskell -- the work simply hasn't been done to make this particularly easy.
On the other hand, if your data structures aren't themselves enormous, serializing them to a Haskell program (either via the command line, or using, a client-server model with e.g. thrift) is very straightforward, and if the computation cost is what sufficiently dominates, the cost may be minimal.
Finally, it is very easy to call Python from Haskell! The classic package for this is missingpy: http://hackage.haskell.org/package/MissingPy
There's also a newer package called cpython which attempts to be more comprehensive: http://hackage.haskell.org/package/cpython
Conceptually, it shouldn't be very hard, I imagine, to host your Python app in Haskell rather than the other way around.
如果您对使用的平台没有要求,您可以查看在 CLI 上运行的 .NET 和 IronPython 的 Numpy 实现。例如,通过此,您将能够使用 F# 作为函数式语言。 .NET 上的 Numpy 和 Scipy 的一些详细信息位于此处 和 CLI 语言 列表。
In case you have no requirements on the platform to use, you might take a look at the Numpy implementation for .NET and IronPython running on CLI. With this you'll be able to use F# as a functional language for instance. Some details to Numpy and Scipy on .NET are here and a list of CLI languages.
我无法想象尝试通过 haskell 或方案使用 numpy 会比仅仅编写函数式 python 更容易。如果您想要更具功能性的Python,请尝试使用itertools和functools。
I can't imagine trying to use numpy through haskell or scheme will be easier than just writing functional python. Try using
itertools
andfunctools
if you want a more functional flavored python.