Search code examples
pythonapache-sparkpysparkapache-spark-mllib

Cannot set coldStartStrategy to pyspark.mllib.recommendation.ALS model


I'm using pyspark (spark 2.4.4) I'm trying to use coldStartStrategy option for the ALS recommendation model but this doesn't work for me. I tried this (from the 2.4.0 documentation: https://spark.apache.org/docs/2.4.0/api/python/pyspark.ml.html#pyspark.ml.recommendation.ALS):

from pyspark.mllib.recommendation import ALS
als = ALS(maxIter=5, regParam=0.01,coldStartStrategy="drop")

This is the error that I have:

TypeError: object() takes no parameters

I tried also to create an empty object the use the setter like this:

als = ALS()
als.setColdStartStrategy('drop')

This is the error that I have:

AttributeError: 'ALS' object has no attribute 'setColdStartStrategy'

The ALS construtor don't take any param, the ALS class has only three methods: mor, train and trainImplicit and no one take the coldStartStrategy parameter. Even the instance of the ALS Class, I navigate into all her methods and also no one takes this parameter. I navigate also into the 2.2.0 spark documentation (https://spark.apache.org/docs/2.2.0/api/python/pyspark.ml.html#pyspark.ml.recommendation.ALS) also the same problem.

I tried also to train the model and after that set the coldStartStrategy parameter for the test phase

model = ALS.train(rdd_train, rank, numIterations)

but also I can't fount the setParams method like mentioned here: https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/recommendation.html


Solution

  • The problem was caused by the importation, it seems that the ALS model imported from pyspark.mllib differs from the ALS imported from pyspark.ml and by changing the importation I fix this error. Conclusion: I just change from :

    from pyspark.mllib.recommendation import ALS
    

    to

     from pyspark.ml.recommendation import ALS