Reputation: 1635
I'm trying to tune the hyper-parameters of a Spark (PySpark) ALS
model by TrainValidationSplit
.
It works well, but I want to know which combination of hyper-parameters is the best. How to get best params after evaluation ?
from pyspark.ml.recommendation import ALS
from pyspark.ml.tuning import TrainValidationSplit, ParamGridBuilder
from pyspark.ml.evaluation import RegressionEvaluator
df = sqlCtx.createDataFrame(
[(0, 0, 4.0), (0, 1, 2.0), (1, 1, 3.0), (1, 2, 4.0), (2, 1, 1.0), (2, 2, 5.0)],
["user", "item", "rating"],
)
df_test = sqlCtx.createDataFrame(
[(0, 0), (0, 1), (1, 1), (1, 2), (2, 1), (2, 2)],
["user", "item"],
)
als = ALS()
param_grid = ParamGridBuilder().addGrid(
als.rank,
[10, 15],
).addGrid(
als.maxIter,
[10, 15],
).build()
evaluator = RegressionEvaluator(
metricName="rmse",
labelCol="rating",
)
tvs = TrainValidationSplit(
estimator=als,
estimatorParamMaps=param_grid,
evaluator=evaluator,
)
model = tvs.fit(df)
Question: How to get best rank and maxIter ?
Upvotes: 6
Views: 4611
Reputation: 330173
You can access best model using bestModel
property of the TrainValidationSplitModel
:
best_model = model.bestModel
Rank can be accessed directly using rank
property of the ALSModel
:
best_model.rank
10
Getting maximum number of iterations requires a bit more trickery:
(best_model
._java_obj # Get Java object
.parent() # Get parent (ALS estimator)
.getMaxIter()) # Get maxIter
10
Upvotes: 7