7

Is there way to call scikit-learn's functions from c++? I have the rest of most my code in C++ with opencv. I would like to be able use the classifiers scikit-learn provides. As far as I understand, there's no easy way - I need to use boost::python or swig. I also came across this project (https://github.com/spillai/numpy-opencv-converter) which shows interop between numpy arrays <==> cv::Mat objects, but I know only how to use to call C++ code from my python script, not the other way around.

4 Answers 4

10

Have you looked at native C++ libraries like shogun and mlpack? These might be much easier for you to use.

Sign up to request clarification or add additional context in comments.

3 Comments

Please, share if there are any new C++ tools since your answer in 2015, thanks.
Does Shogun support DBSCAN?
@Avrdan it doesn't look like it but MLPack does: mlpack.org/doc/stable/cli_documentation.html#mlpack_dbscan just check both of their documentations.
8

Following up from the original answer, There seem to be two approaches today to solve the problem

  1. A mechanism to wrap python into C++ and call it: A modern approach to do this is with PyBind11
  2. Use native C++ ML libraries - MLPack seems to be the gold standard, and OpenCV for vision based ML.

Comments

5

You can do it in a pretty straightforward way, by including python headers and just calling your python script and/or scikit methods via Py* wrappers.

See https://docs.python.org/2/extending/embedding.html#pure-embedding for a thorough example.

Comments

0

For some supported models, it may be possible to convert them to ONNX runtime an run that via C++.

  1. convert the sklearn model (pipeline) to ONNX (see here) and
  2. run ONNX-runtime in C++ (see here)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.