我正在尝试基于时间序列数据的滑动窗口提取特征。
在Scala中,似乎有一个基于this post和the documentation的sliding
函数
import org.apache.spark.mllib.rdd.RDDFunctions._
sc.parallelize(1 to 100, 10)
.sliding(3)
.map(curSlice => (curSlice.sum / curSlice.size))
.collect()
我的问题是PySpark中有类似的函数吗?或者,如果还没有这样的函数,我们如何实现类似的滑动窗口转换?
要添加到venuktan的答案中,这里是如何使用Spark SQL创建一个基于时间的滑动窗口,并保留窗口的全部内容,而不是对其进行聚合。在我的用例中将时间序列数据预处理到滑动窗口以输入到Spark ML中时,这是必需的
这种方法的一个限制是,我们假设您希望随着时间推移使用滑动窗口。
首先,您可以创建Spark数据帧,例如通过读取CSV文件:
我们假设CSV文件有两列:一列是unix时间戳,另一列是要从中提取滑动窗口的列。
附加:要将此数组列转换为Spark ML所需的DenseVector格式,see the UDF approach here。
额外的好处:取消嵌套结果列,这样滑动窗口的每个元素都有自己的列try this approach here。
我希望这有帮助,如果我能澄清什么,请告诉我。
spark 1.4具有窗口功能,如下所述: https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html
希望有帮助,请告诉我。
据我所知,
sliding
函数在Python中不可用,SlidingRDD
是一个私有类,不能在MLlib
外部访问。如果要在现有RDD上使用
sliding
,可以创建穷人的sliding
,如下所示:或者您可以尝试这样的方法(在^{} 的帮助下)
相关问题 更多 >
编程相关推荐