ALS
The ALS algorithm uses ML APIs.
Model API Type |
Function API |
|---|---|
ML API |
def fit(dataset: Dataset[_]): ALSModel def fit(dataset: Dataset[_], paramMaps: Array[ParamMap]): Seq[ALSModel] def fit(dataset: Dataset[_], paramMap: ParamMap): ALSModel def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): ALSModel |
ML API
- Input and output
- Package name: package org.apache.spark.ml.recommendation
- Class name: ALS
- Method name: fit
- Input: training sample data ((Dataset[_])
- Algorithm parameters
Algorithm Parameter
def setRank(value: Int): ALS.this.type
def setNumUserBlocks(value: Int): ALS.this.type
def setNumItemBlocks(value: Int): ALS.this.type
def setImplicitPrefs(value: Boolean): ALS.this.type
def setAlpha(value: Double): ALS.this.type
def setUserCol(value: String): ALS.this.type
def setItemCol(value: String): ALS.this.type
def setRatingCol(value: String): ALS.this.type
def setPredictionCol(value: String): ALS.this.type
def setMaxIter(value: Int): ALS.this.type
def setRegParam(value: Double): ALS.this.type
def setNonnegative(value: Boolean): ALS.this.type
def setCheckpointInterval(value: Int): ALS.this.type
def setSeed(value: Long): ALS.this.type
def setIntermediateStorageLevel(value: String): ALS.this.type
def setFinalStorageLevel(value: String): ALS.this.type
def setColdStartStrategy(value: String): ALS.this.type
def setNumBlocks(value: Int): ALS.type
- Added algorithm parameters
Parameter
Description
Type
spark.boostkit.ALS.blockMaxRow
Row block size of a Gram matrix (You are not advised to change the value.)
Positive integer
spark.boostkit.ALS.unpersistCycle
srcFactorRDD anti-persistence operation cycle (You are not advised to change the value.)
Positive integer
An example is provided as follows:
1 2 3 4 5 6 7 8 9 10 11 12
val als = new ALS() .setMaxIter(numIterations) .setUserCol("user") .setItemCol("product") .setRatingCol("rating") .setNonnegative(nonnegative) .setImplicitPrefs(implicitPrefs) .setNumItemBlocks(numItemBlocks) .setNumUserBlocks(numUserBlocks) .setRegParam(regParam) .setAlpha(alpha) val model = als.fit(ratings)
- Output: ALS recommendation model (ALSModel)
- Example usage
1 2 3
val model = als.fit(ratings) val predictions = model.transform(ratings) val p = predictions.select("rating", "prediction").rdd .map { case Row(prediction: Float, label: Float) => (prediction, label) } .map{t => val err = (t._1 - t._2) err * err }.mean() println("Mean Squared Error = " + p)
- Example result
1Mean Squared Error = 0.9962046583156793