* 相关性检测
* 计算两个系列数据之间的相关性是“统计”中的常见操作。
* spark提供了很多系列中的灵活性,计算两两相关性。
* 目前支持的相关方法是Pearson和Spearman的相关。
def correlationTest(): Unit = {
val spark: SparkSession = SparkSession.builder().appName("implicits").master("local[2]").getOrCreate()
val sqlContext: SQLContext = spark.sqlContext
import sqlContext.implicits._
val data = Seq(
Vectors.sparse(4, Seq((0, 1.0), (3, -2.0))),
Vectors.dense(4.0, 5.0, 0.0, 3.0),
Vectors.dense(6.0, 7.0, 0.0, 8.0),
Vectors.sparse(4, Seq((0, 9.0), (3, 1.0)))
)
val df = data.map(Tuple1.apply).toDF("features")
val Row(coeff1: Matrix) = Correlation.corr(df, "features").head
println(s"皮尔森相关矩阵:\n $coeff1")
val Row(coeff2: Matrix) = Correlation.corr(df, "features", "spearman").head
println(s"斯皮尔曼相关矩阵:\n $coeff1")
}