banner



How Can I Change Python Machine Learning Model To C++

A iii-pace tutorial on how to migrate your ML model to Java, Go, C++ or whatsoever other language. It's easier than you may call back.

Photo by Fleck Deject on Unsplash

I recently worked on a project, where I needed to train a Machine Learning model that would run on the Edge — meaning, the processing and prediction occur on the device that collects the data.

As usual, I did my Automobile Learning part in Python and I oasis't thought much near how we're going to port my ML stuff to the edge device, which was written in Java.

When the modeling office was nearing the end, I started researching how to load a LightGBM model in Java. Prior to this, I had a discussion with a colleague who recommended that I retrain the model with the XGBoost model, which can be loaded in Java with XGBoost4J dependency.

LightGBM and XGBoost are both gradient boosting libraries with a few differences. I would await to get a similar model if I decided to retrain the model with XGBoost, but I didn't want to rerun all the experiments as there had to be a better manner.

To my luck, I found a simple manner to load any Auto Learning model in Python to any other language.

By reading this article, you'll learn:

  • What is PMML?
  • How to save a Python model to PMML format?
  • How to load the PMML model in Java?
  • How to make predictions with the PMML model in Java?

Here are few links that might involvement you:

          - Complete your Python analyses 10x faster with Mito [Product]          - Free skill tests for Data Scientists & ML Engineers [Test]          - All New Self-Driving Automobile Engineer Nanodegree            [Course]        

Would y'all like to read more such articles? If so, you lot tin back up me past clicking on any links above. Some of them are affiliate links, just you don't need to buy anything.

Meet PMML

Photo past Dom Fou on Unsplash

From Wikipedia:

The Predictive Model Markup Linguistic communication (PMML) is an XML-based predictive model interchange format conceived by Dr. Robert Lee Grossman, so the director of the National Center for Data Mining at the University of Illinois at Chicago. PMML provides a way for analytic applications to describe and commutation predictive models produced by data mining and machine learning algorithms.

PMML supports:

  • Neural Networks
  • Support Vector Machines
  • Association rules
  • Naive Bayes classifier
  • Clustering models
  • Text models
  • Decision trees (Random forest)
  • Gradient Boosting (LightGBM and XGBoost)
  • Regression models

PMML enables us to load a Automobile Learning model, that was trained in Python, in Coffee, Get lang, C++, Ruby and others.

i. Using PMML

Photo by JESHOOTS.COM on Unsplash

My first idea after learning about PMML was that I would need to radically refactor the lawmaking, which would make retraining the model with XGBoost more feasible.

After thinking about information technology, I decided to requite PMML a try. Information technology has a well-maintained repository with clear instructions — which is ever a good sign.

You can just install the PMML parcel with:

          pip install sklearn2pmml        

sklearn2pmml packet is needed to export the Python Machine Learning model to PMML format. Using it is unproblematic, nosotros just need to wrap the classifier with PMMLPipeline grade.

To make information technology easier for you lot, I wrote a simple gist that trains a LightGBM model on the Iris dataset and exports the model to PMML format:

  1. Import required Python packages
  2. Load Iris dataset
  3. Split Iris dataset to train and exam sets
  4. Railroad train LightGBM model with PMML support — this is the only required alter in your code.
  5. Mensurate classification accuracy of the model.
  6. And finally, relieve the model to the PMML format.
Railroad train a LightGBM model on the Iris dataset and export it to PMML format.

How does the PMML model look like?

The code above creates a PMML file, which is an XML file. The XML contains all the model details as seen in the image below.

PMML model is stored in an XML file (image by author).

2. How to load the model in Java?

Photograph by Jake Young on Unsplash

We trained the model with Python and exported information technology to PMML format, at present we demand to load it in Java.

I created a minimalistic repository LoadPMMLModel on Github, which shows how to load a PMML model in Java.

The first footstep is to add a PMML dependency to pom.xml (I'm using maven dependency manager):

          <dependency>
<groupId>org.jpmml</groupId>
<artifactId>pmml-evaluator</artifactId>
<version>1.five.15</version>
</dependency

I saved the PMML file to the project'due south resources binder so that compiler tin can package information technology.

PMML file is in the resource binder (prototype by author)

Then nosotros demand to specify the path to the model:

          String modelFolder = LoadPMMLModel.course.getClassLoader().getResource("model").getPath();          Cord modelName = "boosting_model.pmml";          Path modelPath = Paths.get(modelFolder, modelName);        

Loading the model with PMML model is as simple as (the variable with the model in Java is Evaluator type):

          Evaluator evaluator = new LoadingModelEvaluatorBuilder()
.load(modelPath.toFile())
.build();
evaluator.verify();

3. How to make predictions in Java?

Photograph past Tadeusz Lakota on Unsplash

Now let'due south make a few predictions with the loaded model.

In Python, the prediction for the beginning sample in the exam set was 1.

Offset prediction in python (image by author).

Let's utilise the same sample as above in Python, but in Coffee:

          Map<String, Double> features = new HashMap<>();
features.put("sepal length (cm)", half-dozen.i);
features.put("sepal width (cm)", ii.8);
features.put("petal length (cm)", 4.seven);
features.put("petal width (cm)", 1.two);
Map<FieldName, FieldValue> arguments = new LinkedHashMap<>();
for (InputField inputField : inputFields) {
FieldName inputName = inputField.getName();
Double value = features.get(inputName.toString());
FieldValue inputValue = inputField.set up(value);
arguments.put(inputName, inputValue);
}

And query the model in Java for prediction:

          Map<FieldName, ?> results = evaluator.evaluate(arguments);          // Extracting prediction
Map<String, ?> resultRecord = EvaluatorUtil.decodeAll(results);
Integer yPred = (Integer) resultRecord.get(targetName.toString());
Organization.out.printf("Prediction is %d\north", yPred);
System.out.printf("PMML output %southward\n", resultRecord);

With the code above, we get the following output:

The output of the model in Java (paradigm by author)

Conclusion

Photo by Johannes Plenio on Unsplash

In my Machine Learning learning project, I used a regression boosting model. To my surprise, the exported PMML model produced the aforementioned results to the fifth decimal every bit the model in Python.

I don't have anything bad to say virtually PMML as it works reliably in production.

Remember, you don't demand to copy-paste the lawmaking from this commodity as I created LoadPMMLModel repository on Github.

Please let me know what are your thoughts almost PMML.

Before you go

Follow me on Twitter, where I regularly tweet virtually Information Science and Machine Learning.

Photo by Kelly Sikkema on Unsplash

Source: https://towardsdatascience.com/how-to-migrate-your-python-machine-learning-model-to-other-languages-6212c9bc61f9

Posted by: rodriguezfloory38.blogspot.com

0 Response to "How Can I Change Python Machine Learning Model To C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel