Github user MLnick commented on a diff in the pull request:
https://github.com/apache/spark/pull/12896#discussion_r62070675
--- Diff:
mllib/src/test/scala/org/apache/spark/ml/recommendation/ALSSuite.scala ---
@@ -486,6 +486,39 @@ class ALSSuite
assert(getFactors(model.userFactors) ===
getFactors(model2.userFactors))
assert(getFactors(model.itemFactors) ===
getFactors(model2.itemFactors))
}
+
+ test("ALS unknown user/item prediction strategy") {
+ val sqlContext = this.sqlContext
+ import sqlContext.implicits._
+ import org.apache.spark.sql.functions._
+
+ val (ratings, _) = genExplicitTestData(numUsers = 4, numItems = 4,
rank = 1)
+ val data = ratings.toDF
+ val knownUser = data.select(max("user")).as[Int].first()
+ val unknownUser = knownUser + 10
+ val knownItem = data.select(max("item")).as[Int].first()
+ val unknownItem = knownItem + 20
+ val test = Seq(
+ (unknownUser, unknownItem),
+ (knownUser, unknownItem),
+ (unknownUser, knownItem),
+ (knownUser, knownItem)
+ ).toDF("user", "item")
+
+ val als = new ALS().setMaxIter(1).setRank(1)
+ // default is 'nan'
+ val defaultModel = als.fit(data)
+ val defaultPredictions =
defaultModel.transform(test).select("prediction").as[Float].collect()
+ assert(defaultPredictions.length == 4)
+ defaultPredictions.slice(0, 3).foreach(p => assert(p.isNaN))
+ assert(!defaultPredictions.last.isNaN)
+
+ // check 'drop' strategy should filter out rows with unknown
users/items
+ val dropModel = als.setUnknownStrategy("drop").fit(data)
--- End diff --
True the fit is not strictly necessary.
On Wed, 4 May 2016 at 17:52, Seth Hendrickson <[email protected]>
wrote:
> In mllib/src/test/scala/org/apache/spark/ml/recommendation/ALSSuite.scala
> <https://github.com/apache/spark/pull/12896#discussion_r62064543>:
>
> > + (unknownUser, unknownItem),
> > + (knownUser, unknownItem),
> > + (unknownUser, knownItem),
> > + (knownUser, knownItem)
> > + ).toDF("user", "item")
> > +
> > + val als = new ALS().setMaxIter(1).setRank(1)
> > + // default is 'nan'
> > + val defaultModel = als.fit(data)
> > + val defaultPredictions =
defaultModel.transform(test).select("prediction").as[Float].collect()
> > + assert(defaultPredictions.length == 4)
> > + defaultPredictions.slice(0, 3).foreach(p => assert(p.isNaN))
> > + assert(!defaultPredictions.last.isNaN)
> > +
> > + // check 'drop' strategy should filter out rows with unknown
users/items
> > + val dropModel = als.setUnknownStrategy("drop").fit(data)
>
> I don't see a reason to call fit a second time since the value only has
> effect in the transformer. This only checks that the param get transferred
> to the transformer properly, right?
>
> â
> You are receiving this because you authored the thread.
> Reply to this email directly or view it on GitHub
>
<https://github.com/apache/spark/pull/12896/files/fc437451a598221f0878b7a2e0b87d17572019cc#r62064543>
>
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]