英文:
Using Python Pickle Object In Java
问题
以下是您要翻译的内容:
我有一个用于RandomForestClassifier的pickle对象
,它是用Python
创建的。我想在Java中使用这个对象进行预测。这是否可能?据我所见,可以以这种方式在Java
中使用Python pickle对象
,但我不确定它是否适用于机器学习模型。
编辑:我明白Pickle只是一个“序列化对象”,在Java中使用它是不合理的。我必须使用DronovIlya中的Java分类器实现,而不是在Java中使用Python对象。
英文:
I have a pickle object
for RandomForestClassifier in Python
. I want to use this object in Java to make predictions. Is that possible? As I see, a Python pickle object
can be used in Java
in that way but I'm not sure if it works for a machine learning model.
Edit: I figured out that Pickle is just a "serialized object", which was non-sense to think to use in Java. I had to use the Java classifier implementation from DronovIlya instead of using Python object in Java.
答案1
得分: 1
Short answer: 不可以。
Long answer:
根据Python文档:
> pickle 模块实现了用于序列化和反序列化 Python 对象结构的二进制协议。“Pickling” 是将 Python 对象层次结构转换为字节流的过程,“unpickling” 则是相反的操作,即将字节流(来自二进制文件或类字节对象)转换回对象层次结构。Pickling(和 unpickling)另外也被称为“序列化”、“封送” 1 或“扁平化”;然而,为了避免混淆,在此使用的术语是“pickling” 和 “unpickling”。
这意味着给定的字节流只在 Python 中具有语法上的有效性,除非你想编写一个对象转换器。
你的问题解决方法是错误的。机器学习模型只是一组经过优化以获得目标变量的近似的系数/参数集合。你应该能够将那组系数/参数在任何语言中使用。
英文:
Short answer: NO.
Long answer:
According to python documentation:
>The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” 1 or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”
This means that the given byte stream is only syntactically valid in Python only unless you want to write an object transpiler.
You are approaching your problem in the wrong way. ML model is just a set of coefficients/parameters that you have optimized to get a close approximation of your target variable. You should be able to take that set of coefficients/parameters and use it in any language.
答案2
得分: 0
我们可以使用来自Jython的cPickle类。
链接:https://javadoc.io/doc/org.python/jython-standalone/2.7.1/org/python/modules/cPickle.html
英文:
We can use cPickle class from jython
https://javadoc.io/doc/org.python/jython-standalone/2.7.1/org/python/modules/cPickle.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论