Kunal Vyas
Kunal Vyas

Reputation: 1579

AttributeError: 'MatrixFactorizationModel' object has no attribute 'save'

Im trying to run the example on Apache Spark's MLlib website. Below is my code:

import sys
import os

os.environ['SPARK_HOME'] = "/usr/local/Cellar/apache-spark/1.2.1"
sys.path.append("/usr/local/Cellar/apache-spark/1.2.1/libexec/python")
sys.path.append("/usr/local/Cellar/apache-spark/1.2.1/libexec/python/build")

try:
    from pyspark import SparkContext, SparkConf
    from pyspark.mllib.recommendation import ALS, MatrixFactorizationModel, Rating
    print ("Apache-Spark v1.2.1 >>> All modules found and imported successfully.")

except ImportError as e:
    print ("Couldn't import Spark Modules", e)
    sys.exit(1)

# SETTING CONFIGURATION PARAMETERS
config = (SparkConf()
        .setMaster("local")
        .setAppName("Music Recommender")
        .set("spark.executor.memory", "16G")
        .set("spark.driver.memory", "16G")
        .set("spark.executor.cores", "8"))
sc = SparkContext(conf=config)

# Load and parse the data
data = sc.textFile("data/1aa")
ratings = data.map(lambda l: l.split('\t')).map(lambda l: Rating(int(l[0]), int(l[1]), float(l[2])))

# Build the recommendation model using Alternating Least Squares
rank = 10
numIterations = 10
model = ALS.train(ratings, rank, numIterations)

# Evaluate the model on training data
testdata = ratings.map(lambda p: (p[0], p[1]))
predictions = model.predictAll(testdata).map(lambda r: ((r[0], r[1]), r[2]))
ratesAndPreds = ratings.map(lambda r: ((r[0], r[1]), r[2])).join(predictions)
MSE = ratesAndPreds.map(lambda r: (r[1][0] - r[1][1])**2).mean()
print("Mean Squared Error = " + str(MSE))

# Save and load model
model.save(sc, "/Users/kunal/Developer/MusicRecommender")
sameModel = MatrixFactorizationModel.load(sc, "/Users/kunal/Developer/MusicRecommender/data")

The code is running till printing the MSE. The last step is to save the model to a directory. I am getting the error 'MatrixFactorizationModel' object has no attribute 'save'(I've pasted last few rows of the log) below:

15/10/06 21:00:16 INFO DAGScheduler: Stage 200 (mean at /Users/kunal/Developer/MusicRecommender/collabfiltering.py:41) finished in 12.875 s
15/10/06 21:00:16 INFO DAGScheduler: Job 8 finished: mean at /Users/kunal/Developer/MusicRecommender/collabfiltering.py:41, took 53.290203 s
Mean Squared Error = 405.148403002
Traceback (most recent call last):
  File "/Users/kunal/Developer/MusicRecommender/collabfiltering.py", line 47, in <module>
    model.save(sc, path)
AttributeError: 'MatrixFactorizationModel' object has no attribute 'save'

Process finished with exit code 1

I have reinstalled and made sure I have the latest version of Spark but that did not help it. I am running this on a 10MB file only which is a tiny split of the larger file.

Operating System: OSX 10.11.1 Beta (15B22c)

Upvotes: 0

Views: 1739

Answers (1)

zero323
zero323

Reputation: 330393

It happens because you use Spark 1.2.1 and MatrixFactorizationModel.save method has been introduced in Spark 1.3.0. Moreover documentation you use covers a current version (1.5.1).

Spark documentation urls look like this:

http://spark.apache.org/docs/SPARK_VERSION/some_topic.html

So in your case you should use:

http://spark.apache.org/docs/1.2.1/mllib-collaborative-filtering.html

Upvotes: 1

Related Questions