推荐系统模型-基于用户推荐

import org.apache.spark.mllib.recommendation.{ALS, Rating}
import org.apache.spark.{SparkConf, SparkContext}

object demo01 {

  def main(args: Array[String]): Unit = {
    val conf=new SparkConf().setMaster("local").setAppName("demo1")
    val sc=new SparkContext(conf)
    val data=sc.textFile("D:\\bigdata\\data\\ml\\u.data")
    val ratings=data.map{x=>
      val info=x.split("\\t")
      val userId=info(0).toInt
      val movieId=info(1).toInt
      val score=info(2).toDouble
      Rating(userId,movieId,score)
    }

    //建立推荐系统模型,底层通过ALS算法来求解
    //①参:数据集 
    //②参 隐藏因子k的数量不宜过大,避免产生过大的计算代价,介于u和i之间
    //③参:最大迭代次数,生产环境建议多一些,使其充分收敛
    //④参 λ正则化参数,引入正则化参数,防止模型过拟合
    val model=ALS.train(ratings,50,15,0.01)

    val moviedata=sc.textFile("D://bigdata/data/ml/u.item")

    def getmovie(userId:Int,num:Int)={
      val movieMap=moviedata.map{x=>
        val info=x.split("\\|")
        val movieId=info(0).toInt
        val movieName=info(1)
        (movieId,movieName)
      }.collectAsMap()

      val predictResult=model.recommendProducts(userId,num)

      val Result=predictResult.map{x=>
        val userId=x.user
        val movieId=x.product
        val movieName=movieMap(movieId)
        val score=x.rating
        (userId,movieName,score)
      }

      Result.foreach(println)
    }

    model.save(sc, "hdfs://demo1:9000/rec-result")
  }
}

你可能感兴趣的:(spark,spark,大数据,big,data,机器学习)