我有来自Spark中ml库的HashingTF的DataFrame [SparseVector]。以下是我的架构:
root
|-- docId: string (nullable = true)
|-- docNGrams: array (nullable = true)
| |-- element: string (containsNull = true)
|-- HashedNGrams: vector (nullable = true)
HashedNGrams是稀疏向量2 ^ 31 - 1 = 2147483647个特征数(最大值)。 HashedNGrams的一个例子是:
[doc/00000.txt,(2147483647,[70921,235056,....],[1.0,2.0,...])
[doc/00001.txt,(2147483647,[6067499,8758008,....],[1.0,1.0,...])
......
......
我想要的是获得Just the Values:
70921,235056
6067499,8758008
..... , ...
从这个DataFrame到Vector,List [Int]等等,所以要做一些数据操作。到目前为止我尝试过的是: 1)尝试将其转换为密集的Vector,但它给了我一个超出VM限制的限制 2)尝试了从这里找到的所有可能的解决方案但完全不缺!他们都给我一个错误。 3)甚至将它保存在文件中,然后尝试将这些值作为字符串(尚未完成此操作,但我觉得这是非常错误的方法)。
请帮忙!
答案 0 :(得分:0)
您可以使用将let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: -152.404419, longitude: 61.370716)
state_marker.title = "Test"
state_marker.snippet = "Hey, this is Test"
state_marker.map = mapView
转换为值数组的用户定义函数(UDF) - 以下是如何使用这些数组添加名为SparseVector
的新列:
values
UPDATE :正如@ zero323所述,OP确实在 indices 之后,而不是值。如评论所述,通过在UDF中使用import org.apache.spark.sql.functions._
val valuesOnly = udf { s: SparseVector => s.values }
val result = df.withColumn("values", valuesOnly(col("HashedNGrams")))
而不是s.indices
可以轻松实现这一点。