I am studying in machine learning now and I want to build up a recommender system. First, I would like to make a top-N recommendation using two existing methods and they are both written in C++ code. As the file are huge and complex, I want to call them with Python, instead of adding code on that. Which tool is suitable for my case? Thank you in advance!
-
2You can use something like SWIG.wendelbsilva– wendelbsilva2015-12-11 16:56:32 +00:00Commented Dec 11, 2015 at 16:56
-
1I've heard good things about Boost.Python.Qaz– Qaz2015-12-11 16:57:43 +00:00Commented Dec 11, 2015 at 16:57
-
3Just create a wrapper by extending Python.James Mertz– James Mertz2015-12-11 17:02:08 +00:00Commented Dec 11, 2015 at 17:02
-
1If you have the sources, wrap them with Cython. It's way easier than using the standard Python C APIEli Korvigo– Eli Korvigo2015-12-11 17:11:48 +00:00Commented Dec 11, 2015 at 17:11
-
2Possible duplicate of Calling C/C++ from python?Ulrich Eckhardt– Ulrich Eckhardt2015-12-11 18:57:50 +00:00Commented Dec 11, 2015 at 18:57
2 Answers
You can use standard python api , Cython or Boost.python. It is much easier to work with boost.python. You have to add very little code to your c++ library and compile it as a module library which then you can call from python.
with boost you can easily add your classes and their methods. Additionally you can introduce vector of an object which makes it easier to pass data to python and back to your library.
I recommend boost.python but you can look for yourself. There are a lot of tutorials on both cython and boost.python if you google it.
Comments
Definitely use Cython. Useful tools to make the process easier: runcython (simplifies Cython use) and Google's protobuf library (simple / fast messaging serialization library). Here's a really simple example to help people get started: https://github.com/nicodjimenez/python2cpp