Paul Reiners Paul Reiners - 22 days ago 5
Scala Question

Least common upper class of DecisionTreeRegressor and RandomForestRegressor

I want to create a method that will return one of these two types:

- org.apache.spark.ml.regression.DecisionTreeRegressor
- org.apache.spark.ml.regression.RandomForestRegressor


What will be the return type of this method? I believe that it will have a return type of

- org.apache.spark.ml.Estimator<M>


but I don't know what the type parameter
M
should be.

If I simply do this (for example):

def getRegressor(): org.apache.spark.ml.Estimator = {
new DecisionTreeRegressor()
.setLabelCol("label")
.setFeaturesCol("features")
.setMaxBins(100)
}


I get the following error:


class Estimator takes type parameters

Answer

Just ignore the type by using _ (underscore) If you don't care about it.

def getRegressor(): org.apache.spark.ml.Estimator[_] = {
    new DecisionTreeRegressor()
      .setLabelCol("label")
      .setFeaturesCol("features")
      .setMaxBins(100)
  }