So, I am creating a recommendation system in Spark. Although I was able to evaluate and run the algorithm in the data set with the initial values of the hyperparametric parameters manually. I want to automate it by allowing the cross-validation method to select from a grid of hyperparameter values. So I wrote the following function for the same
def recommendation(train):
""" This function trains a collaborative filtering
algorithm on a ratings training data
We use a Cross Validator and Grid Search to find the right hyper-parameter values
Param:
train----> training data
TUNING PARAMETERS:
alpha----> Alpha value to calculate the confidence matrix (only for implicit datasets)
rank-----> no. of latent factors of the resulting X, Y matrix
reg------> regularization parameter for penalising the X, Y factors
Returns:
model-> ALS model object
"""
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.ml.recommendation import ALS
alsImplicit = ALS(implicitPrefs=True)
paramMapImplicit = ParamGridBuilder() \
.addGrid(alsImplicit.rank, [20, 120]) \
.addGrid(alsImplicit.maxIter, [10, 15]) \
.addGrid(alsImplicit.regParam, [0.01, 1.0]) \
.addGrid(alsImplicit.alpha, [10.0, 40.0]) \
.build()
evaluator=BinaryClassificationEvaluator(rawPredictionCol="prediction", labelCol="rating",metricName="areaUnderROC")
cvEstimator= CrossValidator(estimator=alsImplicit, estimatorParamMaps=paramMapImplicit, evaluator=evaluator)
cvModel=cvEstimator.fit(train)
return cvModel,evaluator
The problem is that when I call this function, it will result in the following error:
Launch ALS function for data training
model,evaluator=recommendation(train)
IllegalArgumentException Traceback (most recent call last)
<ipython-input-21-ea5de889f984> in <module>()
1
2
<ipython-input-15-0fb855b138b1> in recommendation(train)
138 cvEstimator= CrossValidator(estimator=alsImplicit, estimatorParamMaps=paramMapImplicit, evaluator=evaluator)
139
141
142 return cvModel,evaluator
/Users/i854319/spark/python/pyspark/ml/pipeline.pyc in fit(self, dataset, params)
67 return self.copy(params)._fit(dataset)
68 else:
70 else:
71 raise ValueError("Params must be either a param map or a list/tuple of param maps, "
/Users/i854319/spark/python/pyspark/ml/tuning.pyc in _fit(self, dataset)
239 model = est.fit(train, epm[j])
240
242 metrics[j] += metric
243
/Users/i854319/spark/python/pyspark/ml/evaluation.pyc in evaluate(self, dataset, params)
67 return self.copy(params)._evaluate(dataset)
68 else:
70 else:
71 raise ValueError("Params must be a param map but got %s." % type(params))
/Users/i854319/spark/python/pyspark/ml/evaluation.pyc in _evaluate(self, dataset)
97 """
98 self._transfer_params_to_java()
---> 99 return self._java_obj.evaluate(dataset._jdf)
100
101 def isLargerBetter(self):
/Users/i854319/spark/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
811 answer = self.gateway_client.send_command(command)
812 return_value = get_return_value(
--> 813 answer, self.gateway_client, self.target_id, self.name)
814
815 for temp_arg in temp_args:
/Users/i854319/spark/python/pyspark/sql/utils.pyc in deco(*a, **kw)
51 raise AnalysisException(s.split(': ', 1)[1], stackTrace)
52 if s.startswith('java.lang.IllegalArgumentException: '):
---> 53 raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
54 raise
55 return deco
IllegalArgumentException: u'requirement failed: Column prediction must be of type org.apache.spark.mllib.linalg.VectorUDT@f71b0bce but was actually FloatType.'
This is expected because the BinaryClassificationEvaluator method expects a probability vector for the prediction values. So far, cvmodel.bestModel.tranform (data) gives the float values for prediction.
, DenseVector , ,
def calcEval(testDF,predictions,evaluator):
""" This function checks the evaluation metric for the recommendation algorithm
testDF-> Validation or Test data to check the evalutation metric on
"""
from pyspark.sql.functions import udf
from pyspark.mllib.linalg import VectorUDT, DenseVector
from pyspark.sql.types import DoubleType
from pyspark.ml.evaluation import BinaryClassificationEvaluator
as_prob = udf(lambda x: DenseVector([1-x,x]), VectorUDT())
predictions=predictions.withColumn("prediction", as_prob(predictions["prediction"]))
predictions.show(5)
print evaluator.getMetricName(), "The AUC of the Model is {}".format(evaluator.evaluate(predictions))
print "The AUC under PR curve is {}".format(evaluator.evaluate(predictions, {evaluator.metricName: "areaUnderPR"}))
, , , , , crossValidator .
- ?