【问题标题】:Why does this example result in NaN?为什么这个例子会导致 NaN?
【发布时间】:2016-04-20 04:46:43
【问题描述】:

我正在查看 PySpark 中 Statistics.corr 的文档:https://spark.apache.org/docs/1.1.0/api/python/pyspark.mllib.stat.Statistics-class.html#corr

为什么这里的关联会导致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.        ]]

【问题讨论】:

    标签: hadoop apache-spark pyspark pearson-correlation


    【解决方案1】:

    很简单。皮尔逊相关系数定义如下:

    由于第二列 ([0, 0, 0, 0]) 的标准差等于 0,因此整个方程的结果为 NaN。

    【讨论】:

    • 等等,它不计算向量之间的相关性,而是计算向量内列的相关性?
    猜你喜欢
    • 2019-11-15
    • 2013-10-04
    • 2021-09-10
    • 2013-02-25
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多