Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Numpy and scikit-learn from C#

Tags:

python

c#

numpy

I'm building a classifier that I wish to host as a c# win service, exposing an endpoint I can call remotely with text I wish to classify. I currently have one working using ironpython and the natural language toolkit, using c#4.0 dynamics. code like this:

var py = Python.CreateEngine();
dynamic script = py.ImportModule("MyPythonScript");
classifier = script.GetClassifier();
//build features etc, then train
trainedClassifier = classifier.TrainClassifier(featureSet);

The classifier trains itself on startup (like above) and I call the classifier realtime with text I wish to classify.

My issue is I want to use the classifiers and vectorizers in scikit-learn.

Ironpython doesn't support scikit-learn as per this link. (Can scikit be used from IronPython?)

Can anyone suggest the best methodology for this? I'm open to suggestions, but I need to hold the trained classifier in memory, as training it on every call will be prohibitive.

My research has yielded the following.

  1. IronPython 2.7 can support numpy and scipy, (https://www.enthought.com/repo/.iron/). Although when I try to run this I have an issue with NumpyDotNet.dll not being found. I gave up as scikit-learn probably wont work with IronPython anyway.

  2. I've looked at 'python for .net' (http://pythonnet.github.io/), but haven't been able to call it from c#. I reference the Python.Runtime.dll but have the same reference issue as this guy (https://stackoverflow.com/questions/22844519/missing-py-gil-from-c-pythonnet-example)

  3. Has anyone used sharpkit.learn. (https://github.com/foreverzet/Sharpkit.Learn). I specifically need Linear SVM and TfidfVectorizer?

  4. I'm open to other solutions for running a python script. However, I'll need to cache the trained classifier, and can't repeatedly train it.

I'm open to all ideas and any help appreciated. thank you.

like image 513
user3661633 Avatar asked Nov 01 '22 22:11

user3661633


1 Answers

marking this question as answered as per the comments above. I couldn't host scikit-learn in iron python and have instead written a service using cpython.

like image 196
user3661633 Avatar answered Nov 12 '22 21:11

user3661633