1

I'd like to use

val ratings = data.map(_.split(',') match {
      case Array(user,item,rate)
      =>
        Rating(user.toInt,item.toInt,rate.toFloat)
    })
val model =  ALS.train(ratings,rank,numIterations,alpha)

However, the user data i get are stored as Long. When switched to int, it may produce error. How can i do to solve the problem?

ZMath_lin
  • 523
  • 2
  • 6
  • 14

1 Answers1

1

You can use one of ML implementations which support Long labels. RDD version it is significantly less user friendly compared to other implementations:

import org.apache.spark.ml.recommendation.ALS
import org.apache.spark.ml.recommendation.ALS.Rating

val ratings = sc.parallelize(Seq(Rating(1L, 2L, 3.0f), Rating(2L, 3L, 5.0f)))

val (userFactors, itemFactors) = ALS.train(ratings)

and returns only factors but DataFrame version returns a model:

val ratingsDF= ratings.toDF

val alsModel = new ALS().fit(ratingsDF)
zero323
  • 322,348
  • 103
  • 959
  • 935
  • Here, if the user and item values are String, will it work? In what format should the string be? – void Jul 29 '16 at 12:53
  • I tried the above example with user and item value as String. and I am getting the exception `java.lang.NullPointerException: Value at index 0 in null`, when I do the `.fit(ratingsDF)` – void Jul 29 '16 at 12:56