I am working on a Spark project with scala. I want to train a model which can be k_means, gaussian_mixture, logistic regression, naive_bayes etc. But I cannot define a generic model as a return type. Since these algorithms' types are different like GaussianMixtureModel, KMeansModel etc. I cannot find any logical way to return this trained model.
Here is a peace of code from the project:
model.model_algorithm match {
case "k_means" =>
val model_k_means = k_means(data, parameters)
case "gaussian_mixture" =>
val model_gaussian_mixture = gaussian_mixture(data, parameters)
case "logistic_regression" =>
val model_logistic_regression = logistic_regression(data, parameters)
}
So is there a way to return this trained model or to define a generic model that accepts all types?
org.apache.spark.mllib.util.Saveable,AntRefandAny, so your method can return any of these types, but that won't necessarily help you. If you want to perform action X on these results later, you might want to create a traitModelResultwith method X, make this pattern-matching returnModelResult, and have three implementations of that trait, each handling a different model.