s****h 发帖数: 3979 | 1 新手请教一个scala+spark问题:
数据格式: userid itemid1,itemid2,itemid3,...
sample:
5 7,13,20,27
12 7,10,27,33
需要做的是,每两个userid的item vector算cosine, 每个user, sort其他user by
cosine value.
其实就是一个KNN.
我想得是
a. 一行行读进来,每行一个sparse vector
or
b. 一起读进一个sparse matrix
两种方式都不会.
多谢多谢。
scala+spark经验只限于根据spark summit上的alswr code照猫画虎,处理了一下自己
的数据。 | n*****3 发帖数: 1584 | 2 做过一个小project with spark, 没太多经验。。
你要想好 first 你的data manipulation steps before really starts,
spark tokenlize them differently
BTW spark 最近 搞个 data frame API , 你 可以 试试, should be better
【在 s****h 的大作中提到】 : 新手请教一个scala+spark问题: : 数据格式: userid itemid1,itemid2,itemid3,... : sample: : 5 7,13,20,27 : 12 7,10,27,33 : 需要做的是,每两个userid的item vector算cosine, 每个user, sort其他user by : cosine value. : 其实就是一个KNN. : 我想得是 : a. 一行行读进来,每行一个sparse vector
| s****h 发帖数: 3979 | 3 data frame API 1.3 才有。
弄进一个dense vector很直接:
import org.apache.spark.mllib.linalg.{Vector, Vectors}
val path = "~/test60m.txt"
val minPartitions = 4
val parsed = sc.textFile(path, minPartitions)
.map(_.trim)
.filter(line => !(line.isEmpty || line.startsWith("#")))
val a = parsed.map({ line =>
val fields = line.split(" ")
(fields(0).toInt, fields(1))})
val dataLoad = a.map(x => (x._1, Vectors.dense(x._2.split(',').map(_.
toDouble))))
可是sparse vector就不会了。
不知道怎么结合下面的例子
// Create a dense vector (1.0, 0.0, 3.0).
val dv: Vector = Vectors.dense(1.0, 0.0, 3.0)
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and
values corresponding to nonzero entries.
val sv1: Vector = Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0))
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries.
val sv2: Vector = Vectors.sparse(3, Seq((0, 1.0), (2, 3.0))) | s****h 发帖数: 3979 | 4 ok
这样就行了
val b = a.map(x => (x._1, Vectors.sparse(30000, x._2.split(',').map(_.toInt)
, (new Array[Double](x._2.split(',').length).map(y=>y+1.0) ))))
下一个问题是:这么搞效率好么?对performance会不会有打的影响呢? | H****E 发帖数: 254 | 5 https://databricks.com/blog/2014/07/16/new-features-in-mllib-in-spark-1-0.
html
"So storage-wise, the sparse format is better than the dense format when
more than 1/3 of the elements are zero. However, assuming that the data can
be fit into memory in both formats, we usually need sparser data to observe
a speedup, because the sparse format is not as efficient as the dense format
in computation. Our experience suggests a sparsity of around 10%, while the
exact switching point for the running time is indeed problem-dependent." | v*******e 发帖数: 3714 | 6 你看看 MLlib 上 cosine similarity 的 source code,直接用或者自己改一下应该没
问题吧
https://github.com/apache/spark/blob/master/examples/src/main/scala/org/
apache/spark/examples/mllib/CosineSimilarity.scala | m*********r 发帖数: 119 | 7 想自学下spark
请问怎么入手好?
youtube 上得视频都不怎么样啊? | h*********d 发帖数: 109 | 8
【在 s****h 的大作中提到】 : 新手请教一个scala+spark问题: : 数据格式: userid itemid1,itemid2,itemid3,... : sample: : 5 7,13,20,27 : 12 7,10,27,33 : 需要做的是,每两个userid的item vector算cosine, 每个user, sort其他user by : cosine value. : 其实就是一个KNN. : 我想得是 : a. 一行行读进来,每行一个sparse vector
|
|