2016-04-19 44 views
-1

我正在查看PySpark中Statistics.corr的文檔:https://spark.apache.org/docs/1.1.0/api/python/pyspark.mllib.stat.Statistics-class.html#corr這個例子爲什麼會導致NaN?

爲什麼這裏的相關性會導致NaN

>>> rdd = sc.parallelize([Vectors.dense([1, 0, 0, -2]), Vectors.dense([4, 5, 0, 3]), 
...      Vectors.dense([6, 7, 0, 8]), Vectors.dense([9, 0, 0, 1])]) 
>>> pearsonCorr = Statistics.corr(rdd) 
>>> print str(pearsonCorr).replace('nan', 'NaN') 
[[ 1.   0.05564149   NaN 0.40047142] 
[ 0.05564149 1.     NaN 0.91359586] 
[  NaN   NaN 1.     NaN] 
[ 0.40047142 0.91359586   NaN 1.  ]] 

回答

3

這是很simple.Pearson相關係數定義如下:

enter image description here

由於用於第二列([0, 0, 0, 0])標準偏差等於0時,整個方程生成NaN。

相關問題