-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Spark-19535][ML] RecommendForAllUsers RecommendForAllItems for ALS on Dataframe #17090
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bcac949
skeleton fns, doesn't compile
sueann d4616ec
simple working tests
2de5b45
comments
6de4c38
scaladoc, formatting
1b35f0a
clean-ups, comments.
08a58e4
added tests for TopByKeyAggregator, more precise tests for recommendA…
b139c26
cleanup
c1973e6
name the output columns in the recommendations Array
b0680db
comments
6a7e3d1
no longer needing to cause serialization costs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
mllib/src/main/scala/org/apache/spark/ml/recommendation/TopByKeyAggregator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.recommendation | ||
|
||
import scala.language.implicitConversions | ||
import scala.reflect.runtime.universe.TypeTag | ||
|
||
import org.apache.spark.sql.{Encoder, Encoders} | ||
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
import org.apache.spark.sql.expressions.Aggregator | ||
import org.apache.spark.util.BoundedPriorityQueue | ||
|
||
|
||
/** | ||
* Works on rows of the form (K1, K2, V) where K1 & K2 are IDs and V is the score value. Finds | ||
* the top `num` K2 items based on the given Ordering. | ||
*/ | ||
private[recommendation] class TopByKeyAggregator[K1: TypeTag, K2: TypeTag, V: TypeTag] | ||
(num: Int, ord: Ordering[(K2, V)]) | ||
extends Aggregator[(K1, K2, V), BoundedPriorityQueue[(K2, V)], Array[(K2, V)]] { | ||
|
||
override def zero: BoundedPriorityQueue[(K2, V)] = new BoundedPriorityQueue[(K2, V)](num)(ord) | ||
|
||
override def reduce( | ||
q: BoundedPriorityQueue[(K2, V)], | ||
a: (K1, K2, V)): BoundedPriorityQueue[(K2, V)] = { | ||
q += {(a._2, a._3)} | ||
} | ||
|
||
override def merge( | ||
q1: BoundedPriorityQueue[(K2, V)], | ||
q2: BoundedPriorityQueue[(K2, V)]): BoundedPriorityQueue[(K2, V)] = { | ||
q1 ++= q2 | ||
} | ||
|
||
override def finish(r: BoundedPriorityQueue[(K2, V)]): Array[(K2, V)] = { | ||
r.toArray.sorted(ord.reverse) | ||
} | ||
|
||
override def bufferEncoder: Encoder[BoundedPriorityQueue[(K2, V)]] = { | ||
Encoders.kryo[BoundedPriorityQueue[(K2, V)]] | ||
} | ||
|
||
override def outputEncoder: Encoder[Array[(K2, V)]] = ExpressionEncoder[Array[(K2, V)]]() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
mllib/src/test/scala/org/apache/spark/ml/recommendation/TopByKeyAggregatorSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.recommendation | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.mllib.util.MLlibTestSparkContext | ||
import org.apache.spark.sql.Dataset | ||
|
||
|
||
class TopByKeyAggregatorSuite extends SparkFunSuite with MLlibTestSparkContext { | ||
|
||
private def getTopK(k: Int): Dataset[(Int, Array[(Int, Float)])] = { | ||
val sqlContext = spark.sqlContext | ||
import sqlContext.implicits._ | ||
|
||
val topKAggregator = new TopByKeyAggregator[Int, Int, Float](k, Ordering.by(_._2)) | ||
Seq( | ||
(0, 3, 54f), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a good idea to have varying # values = like maybe one with only |
||
(0, 4, 44f), | ||
(0, 5, 42f), | ||
(0, 6, 28f), | ||
(1, 3, 39f), | ||
(2, 3, 51f), | ||
(2, 5, 45f), | ||
(2, 6, 18f) | ||
).toDS().groupByKey(_._1).agg(topKAggregator.toColumn) | ||
} | ||
|
||
test("topByKey with k < #items") { | ||
val topK = getTopK(2) | ||
assert(topK.count() === 3) | ||
|
||
val expected = Map( | ||
0 -> Array((3, 54f), (4, 44f)), | ||
1 -> Array((3, 39f)), | ||
2 -> Array((3, 51f), (5, 45f)) | ||
) | ||
checkTopK(topK, expected) | ||
} | ||
|
||
test("topByKey with k > #items") { | ||
val topK = getTopK(5) | ||
assert(topK.count() === 3) | ||
|
||
val expected = Map( | ||
0 -> Array((3, 54f), (4, 44f), (5, 42f), (6, 28f)), | ||
1 -> Array((3, 39f)), | ||
2 -> Array((3, 51f), (5, 45f), (6, 18f)) | ||
) | ||
checkTopK(topK, expected) | ||
} | ||
|
||
private def checkTopK( | ||
topK: Dataset[(Int, Array[(Int, Float)])], | ||
expected: Map[Int, Array[(Int, Float)]]): Unit = { | ||
topK.collect().foreach { case (id, recs) => assert(recs === expected(id)) } | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd think we should have at least some basic tests for this - see
MLPairRDDFunctionsSuite
for example