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

查看:42
本文介绍了如何从 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 之类的微框架或 bottlecornice 并使用 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 中编写一个命令行包装应用程序,它使用某种格式(例如 CSV 或 JSON(或一些较低级别的二进制表示))读取 stdin 上的数据并在 stdout 上输出预测,并从 java 调用 python 程序,例如使用 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.

最后,您可以使用实现所需算法的 Java 库,例如 Weka 或 Mahout,而不是尝试使用 Java 中的 scikit-learn.

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天全站免登陆