如何从Java调用scikit-learn分类器? [英] How can I call scikit-learn classifiers from Java?

查看:249
本文介绍了如何从Java调用scikit-learn分类器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Python的scikit-learn训练的分类器。如何使用Java程序中的分类器?我可以使用Jython吗?有没有办法在Python中保存分类器并在Java中加载它?有没有其他方法可以使用它?

I have a classifier that I trained using Python's scikit-learn. How can I use the classifier from a Java program? Can I use Jython? Is there some way to save the classifier in Python and load it in Java? Is there some other way to use it?

推荐答案

你不能使用jython作为scikit-learn在很大程度上依赖于numpy和scipy许多已编译的C和Fortran扩展因此无法在jython中工作。

You cannot use jython as scikit-learn heavily relies on numpy and scipy that have many compiled C and Fortran extensions hence cannot work in jython.

在java环境中使用scikit-learn的最简单方法是:

The easiest ways to use scikit-learn in a java environment would be to:


  • 将分类器公开为HTTP / Json服务,例如使用微框架,例如 flask 瓶子 cornice 并使用HTTP客户端库从java调用它

  • expose the classifier as a HTTP / Json service, for instance using a microframework such as flask or bottle or cornice and call it from java using an HTTP client library

在python中编写命令行包装器应用程序,读取stdin上的数据并使用某种格式(如CSV或JSON(或某些低级二进制表示))在stdout上输出预测,并使用 Apache Commons Exec

write a commandline wrapper application in python that reads data on stdin and output predictions on stdout using some format such as CSV or JSON (or some lower level binary representation) and call the python program from java for instance using Apache Commons Exec.

使python程序输出在拟合时学习的原始数值参数(通常作为浮点值数组)并重新实现java中的预测函数(对于预测线性模型,这通常很容易,其中预测通常只是一个阈值点积)。

make the python program output the raw numerical parameters learnt at fit time (typically as an array of floating point values) and reimplement the predict function in java (this is typically easy for predictive linear models where the prediction is often just a thresholded dot product).

如果你需要在Java中重新实现特征提取,那么最后一种方法将会有更多的工作。

The last approach will be a lot more work if you need to re-implement feature extraction in Java as well.

最后,您可以使用Weka或Mahout等Java库来实现您需要的算法,而不是尝试使用scikit-learn from Java。

Finally you can use a Java library such as Weka or Mahout that implement the algorithms you need instead of trying to use scikit-learn from Java.

这篇关于如何从Java调用scikit-learn分类器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆