![](/img/trans.png)
[英]calculating the mean ,variation and standard deviation from a training data Set using Spark Scala
[英]Calculating mean and standard deviation using Spark / SCALA
我有一个数据框:
+------------------+
| speed |
+------------------+
| 0.0|
| 0.0|
| 0.0|
| 0.0|
| 0.0|
| 0.0|
| 3.851015222867941|
| 4.456657435740331|
| 0.0|
| NaN|
| 0.0|
| 0.0|
| NaN|
| 0.0|
| 0.0|
| 5.424094717765175|
|1.5781185921913181|
|2.6695439462433033|
| 17.43513658955467|
| 5.440912941359523|
|11.507138536880484|
|12.895677610360089|
| 9.930875909722456|
+------------------+
我想计算 speed 列的均值和标准差。 当我执行此代码时
dataframe_final.select("speed").orderBy("id").agg(avg("speed")).show(1000)
我得到
+------------+
|avg(speed)|
+------------+
| NaN|
+------------+
问题从何而来? 有没有可能解决它?
谢谢
您的数据集中有NaN
(非数字)值。 你不能用这些计算平均值。
要么过滤它们:
dataframe_final
.filter($"speed".isNotNull())
.select("speed")
.orderBy("id")
.agg(avg("speed"))
.show(1000)
或者使用fill
函数将它们替换为0
:
dataframe_final
.select("speed")
.na.fill(0)
.agg(avg("speed"))
.show(1000)
此外,您正在尝试聚合Vitesse
列而不是speed
。
we can also createOrReplaceTempView(dataframe_final) and then we can use spark sql to query and take avg of the speed column
val tableview= dataframe_final.createOrReplaceTempView()
val query = select avg(speed) from tableview where speed IS NOT NULL order by Id
spark.sql(query).show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.