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

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

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