例如:
scala> val my_array = Array(4,5,Double.NaN,6,5,6, Double.NaN)
my_array: Array[Double] = Array(4.0, 5.0, NaN, 6.0, 5.0, 6.0, NaN)
scala> my_array.count(_ == Double.NaN)
res13: Int = 0
我理解两个Double.NaN
彼此并不相同
scala> Double.NaN == Double.NaN
res14: Boolean = false
因此,我得到了我得到的结果,但我找不到能告诉我Double.NaN
的数量的函数,我错过了什么?
在python
中,行为将如下所示:
In [43]: import numpy as np
In [44]: a = np.array([5,np.nan,5,7,4,np.nan])
In [45]: np.isnan(a)
Out[45]: array([False, True, False, False, False, True], dtype=bool)
In [46]: np.isnan(a).sum()
Out[46]: 2
答案 0 :(得分:4)
Double.isNan
完成工作:
scala> val array = Array(4,5,Double.NaN,6,5,6, Double.NaN)
array: Array[Double] = Array(4.0, 5.0, NaN, 6.0, 5.0, 6.0, NaN)
scala> array.count(_.isNaN)
res0: Int = 2