spark mllib推荐算法使用
生活随笔
收集整理的這篇文章主要介紹了
spark mllib推荐算法使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
一、pom.xml
<!-- 機(jī)器學(xué)習(xí)包 --><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(評(píng)分)對(duì)象val ratings = data.map(_.split(',') match { case Array(user, item, rate) =>Rating(user.toInt, item.toInt, rate.toDouble)})//發(fā)生購買行為(評(píng)分)數(shù)據(jù)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)//預(yù)測(cè)評(píng)分. // val predictions = model.predict(unbuy)val predictions = model.predict(usersProducts)predictions.collect().foreach(println)}}轉(zhuǎn)載于:https://my.oschina.net/hehongbo/blog/1545039
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的spark mllib推荐算法使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是copyonwrite容器
- 下一篇: 容器为何物,为什么它对OpenStack