java - 使用 jcc 从 numpy 数组创建 nd JArray

标签 java python numpy jcc

我有一些 java 类(不是我写的),我试图从 python 访问它们。我已经用 JCC 成功编译了它们,并且可以毫无问题地访问这些方法。我可以调用需要 numpy 1D 数组(或列表,它们是相同的)的方法,但我不知道如何创建 2D 或 3D JArray 来提供给代码。

似乎由于 numpy 数组是嵌入列表,因此它们无法直接转换为 JArray。 JCC 文档试图解释如何做到这一点,但它完全晦涩难懂。

http://lucene.apache.org/pylucene/jcc/readme.html

cast obj to an array of Document

       JArray('object').cast_(obj, Document)

In both cases, the java type of obj must be compatible with the array type it is being >cast to.

using nested array:

       d = JArray('object')(1, Document) d[0] = Document() d JArray<object>[<Document: Document<>>] d[0] <Document: Document<>> a = JArray('object')(2) a[0] = d a[1] = JArray('int')([0, 1, 2]) a JArray<object>[<Object: [Lorg.apache.lucene.document.Document;@694f12>, <Object: [I@234265>] a[0] <Object: [Lorg.apache.lucene.document.Document;@694f12> a[1] <Object: [I@234265> JArray('object').cast_(a[0])[0] <Object: Document<>> JArray('object').cast_(a[0], Document)[0] <Document: Document<>> JArray('int').cast_(a[1]) JArray<int>[0, 1, 2] JArray('int').cast_(a[1])[0] 0

这里有一些代码,但是由于这还需要一个编译的java类才能工作,所以我只想让JArrays在超过1d的时间内工作。

import my_java_class
import numpy as np
my_java_class.initVM()

arr = np.ones(10)
# This makes a 1d JArray that can be later used by the java classes in my_java_class
j_arr = my_java_class.JArray('float')(arr) 
arr_2d = np.ones((10,10))
# This fails since the nested lists can't be converted to a 2d JArray.
j_arr2d = my_java_class.JArray('float')(arr_2d) 

我还尝试在 JArray 中重新嵌入 JArray,就像 jcc 文档似乎所做的那样,但这似乎也不起作用。

最佳答案

至少对于双数组,您可以通过以下方式执行此操作:

a = JArray('double')(np.ones(3))
b = JArray('double')(np.ones(3))

ab = JArray('object')((a, b))

这创建了一个二维数组(还没有弄清楚如何使用张量来做到这一点)

关于java - 使用 jcc 从 numpy 数组创建 nd JArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20195402/

相关文章:

java - 在 HQL 和 MySql 数据库中使用 TRIM LEADING 函数

java - 将 Python POST 请求转换为 Android

python - 我如何在 django Rest 框架中的查询参数中创建 OR 运算符

python - 在不违反类封装的情况下在两个窗口之间进行通信

python - 使用 genfromtxt 拆分数据

java - 子实体的外键在保存时未添加到父实体,一对一

java - 在处理java响应Ajax中的重音字符时,应该使用哪种unicode UTF-8和UTF-16

python - 如何在没有循环的情况下均匀裁剪图像

python - 将 hdf5 文件组合成单个数据集

python - Python 中的级数求和