用列表预测<Double>

Predict with a List<Double>

我有一个来自 libsvm 文件的经过训练的 RandomForestModel 对象,现在我想使用 List<Double> 作为特征来调用该模型的预测方法。如何将 List<Double> 转换为 Java 中的必要输入并查看预测目标 class?

据我所知,predict(...) 接收一个 JavaRDD<Vector> 但我不确定如何将 List<Double> 转换为那个 .

通过浏览 docs, it looks like RandomForestModel can also call predict on a Vector. You can convert a List<Double> to a DenseVector (docs) by converting your list to a double array as in this 问题然后执行 new DenseVector(double_array)

根据数据的稀疏性,您可以使用 SparseVector or DenseVector

编写从 List 到 Vector 的 map 转换
int length = yourList.size();
double[] inputArray = new double[length];
yourList.toArray(inputArray);
Vector inputVector = new DenseVector(inputArray);