这个问题类似于这个Converting R dataframe to H2O Frame without writing to disk 除了适用于 Java 对象之外
我的数据在 Java 应用程序中生成,然后保存为文本并传递给 H2O(通过 R 或 Flow)。我想如果我在我的应用程序中动态创建(并保存)H2O DataFrames,我可以避免一些开销。我怀疑它非常简单,但快速查看 docs没有给出简单的(SO 风格)答案
最佳答案
我花了很长时间试图解决同样的问题,因为这在任何地方都没有记录也就是说,如何在不(缓慢地)往返磁盘的情况下创建 H2O 框架。
这是我用于将 Guava 表转换为 H2O 数据帧的内存中 Java 解决方案。适应您的数据结构应该很简单。
不过,我还没有想出如何将它分成 block (或者不这样做对性能的影响是什么)。也许更了解 H2O 的人可以对此发表评论...?
/**
* Converts a Guava Table to an H2O Frame (and registers it in the Distributed
* Key/Value store) using only in-memory operations.
*
* TODO everything is contained in a single chunk. Not sure of performance implications of this...
*
* @param t
* the guava table
* @param tableKey
* a unique key to register the table in h2o under
* @return an H2O Frame
* @throws IOException
*/
public static Frame tableToFrame(LinkedHashBasedTable<Integer, String, Double> t, String tableKey) throws IOException {
Set<String> cols = t.columnKeySet();
List<Vec> vecs = new ArrayList<>();
VectorGroup group = new VectorGroup(); //make a common group to contain vector keys. This has something to do with Chunk distribution among nodes for parallel processing.
for (String col : cols) {
double[] vals = toDoubleArray(t.column(col).values());
Key<Vec> key = group.addVec();
Vec v = Vec.makeVec(vals, key);
vecs.add(v);
}
String[] names = cols.toArray(new String[cols.size()]);
Vec[] vecArr = vecs.toArray(new Vec[vecs.size()]);
Key<Frame> frameKey = Key.make(tableKey);
Frame frame = new Frame(frameKey, names, vecArr);
DKV.put(frameKey, frame); //register the new Frame in the DKV, so h2o jobs can find it.
logger.info("Converted Table to Frame with "+frame.numRows()+" rows and "+frame.numCols()+" cols");
return frame;
}
关于java - 将数据从 Java 对象保存到 H2O Frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483153/