1

I have a trained RandomForestModel object from a libsvm file and would now like to call the predict method with that model using a List<Double> as features. How can I convert a List<Double> to the necessary input in Java and see the predicted target class?

From what I can tell, predict(...) takes in a JavaRDD<Vector> but I'm uncertain how to convert a List<Double> to that .

rimsky
  • 1,163
  • 3
  • 16
  • 27
  • I'm assuming the one I should use is https://spark.apache.org/docs/2.2.0/api/java/org/apache/spark/mllib/tree/model/RandomForestModel.html#predict-org.apache.spark.api.java.JavaRDD- – rimsky Nov 04 '17 at 23:10
  • I suggest you read the documentation of the JavaRDD class. – Code-Apprentice Nov 05 '17 at 00:37

2 Answers2

1

From a browse of the 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 question and then doing new DenseVector(double_array).

rlms
  • 10,650
  • 8
  • 44
  • 61
  • Before I saw this, I ended up doing Vector dv = Vectors.dense(Doubles.toArray(myListOfDoubles)), where Doubles.toArray comes from Guava – rimsky Nov 05 '17 at 03:50
1

Depends on the sparsity of your data, you can write a map transformation from List to Vector using either SparseVector or DenseVector

int length = yourList.size();
double[] inputArray = new double[length];
yourList.toArray(inputArray);
Vector inputVector = new DenseVector(inputArray);
Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70