在代码中,我有一个SparkML PipelineModel,可以作为最后一个阶段,并预测一个RandomForest或GradientTreeBoosting模型。我想根据类型创建对象。我已经尝试过了,但是由于变量被包含在if块内,所以它不起作用。无论如何,有没有投射正确的对象,还是我需要将vectors循环封装在每个if块内?
if (object instanceof PipelineModel) {
PipelineModel pipelineModel = (PipelineModel) object;
if (pipelineModel.stages()[pipelineModel.stages().length - 1] instanceof RandomForestRegressionModel) {
RandomForestRegressionModel rfModel = (RandomForestRegressionModel) pipelineModel.stages()[pipelineModel.stages().length - 1];
}
if (pipelineModel.stages()[pipelineModel.stages().length - 1] instanceof GBTRegressionModel) {
GBTRegressionModel rfModel = (GBTRegressionModel) pipelineModel.stages()[pipelineModel.stages().length - 1];
}
for (int i = 0; i < vectors.size(); i++) {
double[] vector = new double[vectors.get(i).length];
for (int j = vector.length; j < vector.length; j++) {
vector[j] = vectors.get(i)[j];
}
double predictResult = rfModel.predict(Vectors.dense(vector));
result.add(predictResult);
}
}