1#
2# Licensed to the Apache Software Foundation (ASF) under one or more
3# contributor license agreements.  See the NOTICE file distributed with
4# this work for additional information regarding copyright ownership.
5# The ASF licenses this file to You under the Apache License, Version 2.0
6# (the "License"); you may not use this file except in compliance with
7# the License.  You may obtain a copy of the License at
8#
9#    http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# To run this example use
19# ./bin/spark-submit examples/src/main/r/ml/als.R
20
21# Load SparkR library into your R session
22library(SparkR)
23
24# Initialize SparkSession
25sparkR.session(appName = "SparkR-ML-als-example")
26
27# $example on$
28# Load training data
29data <- list(list(0, 0, 4.0), list(0, 1, 2.0), list(1, 1, 3.0),
30             list(1, 2, 4.0), list(2, 1, 1.0), list(2, 2, 5.0))
31df <- createDataFrame(data, c("userId", "movieId", "rating"))
32training <- df
33test <- df
34
35# Fit a recommendation model using ALS with spark.als
36model <- spark.als(training, maxIter = 5, regParam = 0.01, userCol = "userId",
37                   itemCol = "movieId", ratingCol = "rating")
38
39# Model summary
40summary(model)
41
42# Prediction
43predictions <- predict(model, test)
44showDF(predictions)
45# $example off$
46