Github user sueann commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17090#discussion_r104013729
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/ml/recommendation/ALSSuite.scala ---
    @@ -594,6 +595,95 @@ class ALSSuite
           model.setColdStartStrategy(s).transform(data)
         }
       }
    +
    +  private def getALSModel = {
    +    val spark = this.spark
    +    import spark.implicits._
    +
    +    val userFactors = Seq(
    +      (0, Array(6.0f, 4.0f)),
    +      (1, Array(3.0f, 4.0f)),
    +      (2, Array(3.0f, 6.0f))
    +    ).toDF("id", "features")
    +    val itemFactors = Seq(
    +      (3, Array(5.0f, 6.0f)),
    +      (4, Array(6.0f, 2.0f)),
    +      (5, Array(3.0f, 6.0f)),
    +      (6, Array(4.0f, 1.0f))
    +    ).toDF("id", "features")
    +    val als = new ALS().setRank(2)
    +    new ALSModel(als.uid, als.getRank, userFactors, itemFactors)
    +      .setUserCol("user")
    +      .setItemCol("item")
    +  }
    +
    +  test("recommendForAllUsers with k < num_items") {
    +    val topItems = getALSModel.recommendForAllUsers(2)
    +    assert(topItems.count() == 3)
    +    assert(topItems.columns.contains("user"))
    +
    +    val expected = Map(
    +      0 -> Array(Row(3, 54f), Row(4, 44f)),
    +      1 -> Array(Row(3, 39f), Row(5, 33f)),
    +      2 -> Array(Row(3, 51f), Row(5, 45f))
    +    )
    +    checkRecommendations(topItems, expected, "item")
    +  }
    +
    +  test("recommendForAllUsers with k = num_items") {
    +    val topItems = getALSModel.recommendForAllUsers(4)
    +    assert(topItems.count() == 3)
    +    assert(topItems.columns.contains("user"))
    +
    +    val expected = Map(
    +      0 -> Array(Row(3, 54f), Row(4, 44f), Row(5, 42f), Row(6, 28f)),
    +      1 -> Array(Row(3, 39f), Row(5, 33f), Row(4, 26f), Row(6, 16f)),
    +      2 -> Array(Row(3, 51f), Row(5, 45f), Row(4, 30f), Row(6, 18f))
    +    )
    +    checkRecommendations(topItems, expected, "item")
    +  }
    +
    +  test("recommendForAllItems with k < num_users") {
    +    val topUsers = getALSModel.recommendForAllItems(2)
    +    assert(topUsers.count() == 4)
    +    assert(topUsers.columns.contains("item"))
    +
    +    val expected = Map(
    +      3 -> Array(Row(0, 54f), Row(2, 51f)),
    +      4 -> Array(Row(0, 44f), Row(2, 30f)),
    +      5 -> Array(Row(2, 45f), Row(0, 42f)),
    +      6 -> Array(Row(0, 28f), Row(2, 18f))
    +    )
    +    checkRecommendations(topUsers, expected, "user")
    +  }
    +
    +  test("recommendForAllItems with k = num_users") {
    +    val topUsers = getALSModel.recommendForAllItems(3)
    +    assert(topUsers.count() == 4)
    +    assert(topUsers.columns.contains("item"))
    +
    +    val expected = Map(
    +      3 -> Array(Row(0, 54f), Row(2, 51f), Row(1, 39f)),
    +      4 -> Array(Row(0, 44f), Row(2, 30f), Row(1, 26f)),
    +      5 -> Array(Row(2, 45f), Row(0, 42f), Row(1, 33f)),
    +      6 -> Array(Row(0, 28f), Row(2, 18f), Row(1, 16f))
    +    )
    +    checkRecommendations(topUsers, expected, "user")
    +  }
    +
    +  private def checkRecommendations(
    +      topK: DataFrame,
    +      expected: Map[Int, Array[Row]],
    +      dstColName: String): Unit = {
    +    assert(topK.columns.contains("recommendations"))
    +    topK.collect().foreach { row =>
    +      val id = row.getInt(0)
    +      val recs = row.getAs[WrappedArray[Row]]("recommendations")
    +      assert(recs === expected(id))
    +      assert(recs(0).fieldIndex(dstColName) == 0)
    +      assert(recs(0).fieldIndex("rating") == 1)
    --- End diff --
    
    Should we have this requirement in the output (vs. the unnamed tuples)? It 
does cost more to support named fields (we have to call .rdd on the resulting 
dataframe in the process of imposing a new schema) and if we decide to remove 
it later it'll break any usage of the named fields. 


---
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]

Reply via email to