ALS
The ALS algorithm provides 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[_])
- Parameters optimized based on native algorithms
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
- Newly added parameters
Parameter
Value Type
Description
spark.boostkit.ALS.blockMaxRow
Positive integer
Row block size of a Gram matrix. Retain the default value.
spark.boostkit.ALS.unpersistCycle
Positive integer
srcFactorRDD anti-persistence operation cycle. Retain the default value.
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
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)
- Result
1Mean Squared Error = 0.9962046583156793
Parent topic: Recommendation and Pattern Mining