gpt4 book ai didi

scala - spark mllib 将函数应用于 rowMatrix 的所有元素

转载 作者:行者123 更新时间:2023-12-02 08:29:40 25 4
gpt4 key购买 nike

我有一个 rowMatrix xw

scala> xw
res109: org.apache.spark.mllib.linalg.distributed.RowMatrix = org.apache.spark.mllib.linalg.distributed.RowMatrix@8e74950

我想对它的每个元素应用一个函数:

f(x)=exp(-x*x)

矩阵的元素类型可以形象化为:

scala> xw.rows.first

res110: org.apache.spark.mllib.linalg.Vector = [0.008930720313311474,0.017169380001300985,-0.013414238595719104,0.02239106636801034,0.023009502628798143,0.02891937604244297,0.03378470969100948,0.03644030110678057,0.0031586143217048825,0.011230244437457062,0.00477455053405408,0.020251682490519785,-0.005429788421130285,0.011578489275815267,0.0019301805575977788,0.022513736483645713,0.009475039307158668,0.019457912132044935,0.019209006632742498,-0.029811133879879596]

我的主要问题是我不能在矢量上使用 map

scala> xw.rows.map(row => row.map(e => breeze.numerics.exp(e)))
<console>:44: error: value map is not a member of org.apache.spark.mllib.linalg.Vector
xw.rows.map(row => row.map(e => breeze.numerics.exp(e)))
^

scala>

我该如何解决?

最佳答案

这是假设您知道您实际上有一个 DenseVector(看起来确实如此)。您可以在具有 map 的向量上调用 toArray,然后使用 Vectors.dense 转换回 DenseVector:

xw.rows.map{row => Vectors.dense(row.toArray.map{e => breeze.numerics.exp(e)})}

您也可以在 SparseVector 上执行此操作;它在数学上是正确的,但转换为数组可能效率极低。另一种选择是调用 row.copy,然后使用 foreachActive,这对密集和稀疏向量都有意义。但是 copy 可能不会针对您正在使用的特定 Vector 类实现,而且如果您不知道矢量的类型,则无法改变数据。如果你真的需要支持稀疏和密集向量,我会做类似的事情:

xw.rows.map{
case denseVec: DenseVector =>
Vectors.dense(denseVec.toArray.map{e => breeze.numerics.exp(e)})}
case sparseVec: SparseVector =>
//we only need to update values of the sparse vector -- the indices remain the same
val newValues: Array[Double] = sparseVec.values.map{e => breeze.numerics.exp(e)}
Vectors.sparse(sparseVec.size, sparseVec.indices, newValues)
}

关于scala - spark mllib 将函数应用于 rowMatrix 的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28438908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com