我要评分
获取效率
正确性
完整性
易理解

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

  • Function description

    Output the ALS recommendation model after you input sample data in dataset format and call the fit API.

  • 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. 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
    6. 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)
      
    7. 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
    1
    Mean Squared Error = 0.9962046583156793