2019独角兽企业重金招聘Python工程师标准>>>
一、pom.xml
<!-- 机器学习包 --><dependency><groupId>org.apache.spark</groupId><artifactId>spark-mllib_2.10</artifactId><version>${spark.version}</version></dependency>
二、api使用
package cn.hhb.spark.mllibimport org.apache.spark.SparkConf
/***向用户推荐商品*/
import org.apache.spark.mllib.recommendation.{ALS, Rating}
import org.apache.spark.{SparkConf, SparkContext}
// $example off$object RecommendationExample2 {def main(args: Array[String]) {var conf = new SparkConf().setAppName("RecommendationExample2").setMaster("local[4]").set("spark.testing.memory", "2147480000")val sc = new SparkContext(conf)val data = sc.textFile("c://test.data")//变换成rationg(评分)对象val ratings = data.map(_.split(',') match { case Array(user, item, rate) =>Rating(user.toInt, item.toInt, rate.toDouble)})//发生购买行为(评分)数据val realData = ratings.map( t=>(t.user,t.product) )// Build the recommendation model using ALSval rank = 10val numIterations = 10//模型,举证分解模型val model = ALS.train(ratings, rank, numIterations, 0.01)val usersProducts = sc.makeRDD(Array((1,1), (2,1)))//笛卡尔积
// val allUsers = sc.makeRDD(Array(1,2,3,4))
// val allProduct = sc.makeRDD(Array(1,2,3,4))
// val userProducts = allUsers.cartesian(allProduct)
// val unbuy = userProducts.subtract(realData)
// unbuy.collect().foreach(println)//预测评分.
// val predictions = model.predict(unbuy)val predictions = model.predict(usersProducts)predictions.collect().foreach(println)}}