Rate This Document
Findability
Accuracy
Completeness
Readability

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

  • Function

    This type of APIs is used to import sample data in dataset format, call the fit API, and output the ALS recommendation model.

  • Input and output
    1. Package name: package org.apache.spark.ml.recommendation
    2. Class name: ALS
    3. Method name: fit
    4. Input: training sample data (Dataset[_])
    5. 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

    6. Added algorithm parameters

      Parameter

      Description

      Type

      spark.sophon.ALS.bloc kMaxRow

      Row block size of a Gram matrix. Retain the default value.

      Positive integer

      spark.sophon.ALS.unp ersistCycle

      srcFactorRDD anti-persistence operation cycle. Retain the default value.

      Positive integer

      An example is provided as follows:

      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)
    7. Output: ALS recommendation model (ALSModel)
  • Sample usage
    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)
  • Sample result
    Mean Squared Error = 0.9962046583156793