Skip to content

Commit ec2e407

Browse files
committed
format scala example for ALS
1 parent cd9f40b commit ec2e407

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

docs/mllib-collaborative-filtering.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,29 @@ import org.apache.spark.mllib.recommendation.Rating
5959

6060
// Load and parse the data
6161
val data = sc.textFile("mllib/data/als/test.data")
62-
val ratings = data.map(_.split(',') match {
63-
case Array(user, item, rate) => Rating(user.toInt, item.toInt, rate.toDouble)
64-
})
62+
val ratings = data.map(_.split(',') match { case Array(user, item, rate) =>
63+
Rating(user.toInt, item.toInt, rate.toDouble)
64+
})
6565

6666
// Build the recommendation model using ALS
6767
val rank = 10
6868
val numIterations = 20
6969
val model = ALS.train(ratings, rank, numIterations, 0.01)
7070

7171
// Evaluate the model on rating data
72-
val usersProducts = ratings.map{ case Rating(user, product, rate) => (user, product)}
73-
val predictions = model.predict(usersProducts).map{
74-
case Rating(user, product, rate) => ((user, product), rate)
72+
val usersProducts = ratings.map { case Rating(user, product, rate) =>
73+
(user, product)
7574
}
76-
val ratesAndPreds = ratings.map{
77-
case Rating(user, product, rate) => ((user, product), rate)
75+
val predictions =
76+
model.predict(usersProducts).map { case Rating(user, product, rate) =>
77+
((user, product), rate)
78+
}
79+
val ratesAndPreds = ratings.map { case Rating(user, product, rate) =>
80+
((user, product), rate)
7881
}.join(predictions)
79-
val MSE = ratesAndPreds.map{
80-
case ((user, product), (r1, r2)) => math.pow((r1- r2), 2)
82+
val MSE = ratesAndPreds.map { case ((user, product), (r1, r2)) =>
83+
val err = (r1 - r2)
84+
err * err
8185
}.mean()
8286
println("Mean Squared Error = " + MSE)
8387
{% endhighlight %}

0 commit comments

Comments
 (0)