Skip to content

Commit f75f633

Browse files
mengxrjkbradley
authored andcommitted
[SPARK-6571][MLLIB] use wrapper in MatrixFactorizationModel.load
This fixes `predictAll` after load. jkbradley Author: Xiangrui Meng <[email protected]> Closes #5243 from mengxr/SPARK-6571 and squashes the following commits: 82dcaa7 [Xiangrui Meng] use wrapper in MatrixFactorizationModel.load
1 parent 9963143 commit f75f633

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.mllib.api.python
19+
20+
import org.apache.spark.api.java.JavaRDD
21+
import org.apache.spark.mllib.recommendation.{MatrixFactorizationModel, Rating}
22+
import org.apache.spark.rdd.RDD
23+
24+
/**
25+
* A Wrapper of MatrixFactorizationModel to provide helper method for Python.
26+
*/
27+
private[python] class MatrixFactorizationModelWrapper(model: MatrixFactorizationModel)
28+
extends MatrixFactorizationModel(model.rank, model.userFeatures, model.productFeatures) {
29+
30+
def predict(userAndProducts: JavaRDD[Array[Any]]): RDD[Rating] =
31+
predict(SerDe.asTupleRDD(userAndProducts.rdd))
32+
33+
def getUserFeatures: RDD[Array[Any]] = {
34+
SerDe.fromTuple2RDD(userFeatures.asInstanceOf[RDD[(Any, Any)]])
35+
}
36+
37+
def getProductFeatures: RDD[Array[Any]] = {
38+
SerDe.fromTuple2RDD(productFeatures.asInstanceOf[RDD[(Any, Any)]])
39+
}
40+
}

mllib/src/main/scala/org/apache/spark/mllib/api/python/PythonMLLibAPI.scala

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import org.apache.spark.util.Utils
5858
*/
5959
private[python] class PythonMLLibAPI extends Serializable {
6060

61-
6261
/**
6362
* Loads and serializes labeled points saved with `RDD#saveAsTextFile`.
6463
* @param jsc Java SparkContext
@@ -346,24 +345,7 @@ private[python] class PythonMLLibAPI extends Serializable {
346345
model.predictSoft(data)
347346
}
348347

349-
/**
350-
* A Wrapper of MatrixFactorizationModel to provide helpfer method for Python
351-
*/
352-
private[python] class MatrixFactorizationModelWrapper(model: MatrixFactorizationModel)
353-
extends MatrixFactorizationModel(model.rank, model.userFeatures, model.productFeatures) {
354348

355-
def predict(userAndProducts: JavaRDD[Array[Any]]): RDD[Rating] =
356-
predict(SerDe.asTupleRDD(userAndProducts.rdd))
357-
358-
def getUserFeatures: RDD[Array[Any]] = {
359-
SerDe.fromTuple2RDD(userFeatures.asInstanceOf[RDD[(Any, Any)]])
360-
}
361-
362-
def getProductFeatures: RDD[Array[Any]] = {
363-
SerDe.fromTuple2RDD(productFeatures.asInstanceOf[RDD[(Any, Any)]])
364-
}
365-
366-
}
367349

368350
/**
369351
* Java stub for Python mllib ALS.train(). This stub returns a handle

python/pyspark/mllib/recommendation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class MatrixFactorizationModel(JavaModelWrapper, JavaSaveable, JavaLoader):
9090
>>> sameModel = MatrixFactorizationModel.load(sc, path)
9191
>>> sameModel.predict(2,2)
9292
0.43...
93+
>>> sameModel.predictAll(testset).collect()
94+
[Rating(...
9395
>>> try:
9496
... os.removedirs(path)
9597
... except OSError:
@@ -111,6 +113,12 @@ def userFeatures(self):
111113
def productFeatures(self):
112114
return self.call("getProductFeatures")
113115

116+
@classmethod
117+
def load(cls, sc, path):
118+
model = cls._load_java(sc, path)
119+
wrapper = sc._jvm.MatrixFactorizationModelWrapper(model)
120+
return MatrixFactorizationModel(wrapper)
121+
114122

115123
class ALS(object):
116124

0 commit comments

Comments
 (0)