基础数理统计
皮尔逊相关系数
ρ x y = ∑ ( x − x ‾ ) ( y − y ‾ ) ∑ ( x − x ‾ ) 2 ∑ ( y − y ‾ ) 2 \rho_x\ _y =\frac{\sum(x-\overline{x})(y-\overline{y})}{\sqrt{\sum(x-\overline{x})^2\sum(y-\overline{y})^2}} ρx y=∑(x−x)2∑(y−y)2∑(x−x)(y−y)
皮尔逊相关系数可以看做是两组数据的向量夹角的余弦,用来描述两组数据的分开程度。
代码:
import org.apache.log4j.{Level, Logger}
import org.apache.spark.mllib.stat.Statistics
import org.apache.spark.{SparkConf, SparkContext}
object testCorrect{
def main(args: Array[String]) {
// 屏蔽不必要的日志显示终端上
Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
val conf = new SparkConf() //创建环境变量
.setMaster ("local") //设置本地化处理
.setAppName ("testCorrect ") //设定名称
val sc = new SparkContext(conf) //创建环境变量实例
val rddx = sc.textFile("e://x.txt")//读取数据
.flatMap(_.split(' ') //进行分割
.map(_.toDouble))//转化为 Double 类型
val rddy = sc.textFile("e://y.txt")//读取数据
.flatMap(_.split(' ') //进行分割
.map(_.toDouble))//转化为 Double 类型
val correlation:Double = Statistics.corr(rddx,rddy)//计算不同数据之间的相关系数
println(correlation) //打印结果
}
}
x.txt中有 1 2 3 4 5
y.txt中有 2 4 6 8 10
----------
0.9998
斯皮尔曼相关系数
ρ x y = 1 − 6 ∑ ( x i − y i ) 2 n ( n 2 − 1 ) \rho_x\ _y =1-\frac{6\sum(x_i-y_i)^2}{n(n^2-1)} ρx y=1−n(n2−1)6∑(xi−yi)2
斯皮尔曼相关系数更注重两组数据的拟合程度,即两组数据随数据量增加而增长曲线不变。
代码:
import org.apache.log4j.{Level, Logger}
import org.apache.spark.mllib.stat.Statistics
import org.apache.spark.{SparkConf, SparkContext}
object testSingleCorrect {
def main(args: Array[String]): Unit = {
// 屏蔽不必要的日志显示终端上
Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
val conf = new SparkConf() //创建环境变量
.setMaster ("local") //设置本地化处理
.setAppName ("testSingleCorrect ") //设定名称
val sc = new SparkContext(conf) //创建环境变量实例
val rddx = sc.textFile("e://x.txt")//读取数据
.flatMap(_.split(' ') //进行分割
.map(_.toDouble))//转化为 Double 类型
val rddy = sc.textFile("e://y.txt")//读取数据
.flatMap(_.split(' ') //进行分割
.map(_.toDouble))//转化为 Double 类型
val correlation:Double = Statistics.corr(rddx,rddy,"spearman")//计算不同数据之间的相关系数
println(correlation) //打印结果
}
}
----------
0.9998
如果要计算单个数据集相关系数,那么在corr中只写一个数据集就行。
分层抽样
分层抽样是一种数据提取算法,类似于hive的分桶表。
import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}
object testStratifiedSampling2 {
def main(args: Array[String]): Unit = {
// 屏蔽不必要的日志显示终端上
Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)
val conf = new SparkConf() //创建环境变量
.setMaster ("local") //设置本地化处理
.setAppName ("testStratifiedSampling2 ") //设定名称
val sc = new SparkContext(conf)
val data = sc.textFile("e://a.txt")
.map(row =>{
if(row.length == 3) // 判断字符长度,长度为3的value为1,长度为2的value为2
(row,1)
else (row,2)
})
val fractions:Map[String,Double] = Map("aa" -> 2)
val approxSample = data.sampleByKey(withReplacement = false,fractions,0)
approxSample.foreach(println)
}
}
----------
(aa,2)
假设检验
卡方检验是一种常用的假设检验方法, 能够较好地对数据集之间的拟合度、相关性和独立性进行验证。
import org.apache.spark.mllib.linalg.{Matrices,Vectors}
import org.apache.spark.mllib.stat.Statistics
object testChiSq {
def main(args: Array[String]): Unit = {
val vd = Vectors.dense(1,2,3,4,5)
val vdResult = Statistics.chiSqTest(vd)
println(vdResult)
println("----------")
val mtx = Matrices.dense(3,2,Array(1,2,3,4,5,6))
val mtxResult = Statistics.chiSqTest(mtx)
println(mtxResult)
}
}
----------
Chi squared test summary: 方法:卡方校验使用方法
method: pearson
degrees of freedom = 4 自由度:总体参数估计量中变量值独立自由变化的数目
statistic = 3.333333333333333 统计量:不同方法下的统计量
pValue = 0.5036682742334986 p值:显著性差异指标
No presumption against null hypothesis: observed follows the same distribution as expected..
----------
Chi squared test summary:
method: pearson
degrees of freedom = 2
statistic = 0.2799999999999999
pValue = 0.8693582353988059
No presumption against null hypothesis: the occurrence of the outcomes is statistically independent..