spark-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pablo J. Villacorta (JIRA)" <>
Subject [jira] [Created] (SPARK-26162) ALS results vary with user or item ID encodings
Date Sat, 24 Nov 2018 21:40:00 GMT
Pablo J. Villacorta created SPARK-26162:

             Summary: ALS results vary with user or item ID encodings
                 Key: SPARK-26162
             Project: Spark
          Issue Type: Bug
          Components: ML
    Affects Versions: 2.3.0
            Reporter: Pablo J. Villacorta

When calling with the same seed on a dataset, the results (both the latent factors
matrices and the accuracy of the recommendations) differ when we change the labels used to
encode the users or items. The code example below illustrates this by just changing user ID
1 or 2 to an unused ID like 30. The user factors matrix changes, but not only the rows corresponding
to users 1 or 2 but also the other rows. 

Is this the intended behaviour?
val r = scala.util.Random
val trainDataset1 = spark.sparkContext.parallelize(
    (1 to 1000).map(_=> (r.nextInt(20), r.nextInt(100), r.nextInt(4) + 1)) // users go
from 0 to 4
).toDF("user", "item", "rating")

val maxuser ="user")).head.getAs[Int](0)
println(s"maxuser is ${maxuser}")

val trainDataset2 = trainDataset1.withColumn("user", when(col("user")===1, 30).otherwise(col("user")))
val trainDataset3 = trainDataset1.withColumn("user", when(col("user")===2, 30).otherwise(col("user")))

val testDatasets = Array(trainDataset1, trainDataset2, trainDataset3).map(

val Array(als1, als2, als3) = Array(trainDataset1, trainDataset2, trainDataset3).map(new ALS().setSeed(12345).fit(_)), false), false), false){code}
If we ask for recommendations and compare them with a test dataset also modified accordingly
(in this example, the test dataset is exactly the train dataset) the results also differ:
val recommendations = Array(als1, als2, als3).map(x =>
        case Row(user: Int, recommendations: WrappedArray[Row]) => {
            val items ={case Row(item: Int, score: Float) => item}
            (user, items)
    }.toDF("user", "recommendations")

val predictionsAndActualRDD ={
    case (testDataset, recommendationsDF) =>
        testDataset.join(recommendationsDF, "user")
   => {

val metrics = RankingMetrics(_))

println(s"Precision at 5 of first model = ${metrics(0).precisionAt(5)}")
println(s"Precision at 5 of second model = ${metrics(1).precisionAt(5)}")
println(s"Precision at 5 of third model = ${metrics(2).precisionAt(5)}")

This message was sent by Atlassian JIRA

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message