1. 程式人生 > >spark廈大----基本的統計工具(2)

spark廈大----基本的統計工具(2)

來源:http://mocom.xmu.edu.cn/article/show/584d1fc5bd8177b41ebbd8bc/0/1

五、假設檢驗 Hypothesis testing

​ Spark目前支援皮爾森卡方檢測(Pearson’s chi-squared tests),包括“適配度檢定”(Goodness of fit)以及“獨立性檢定”(independence)。

​ 首先,我們匯入必要的包

import org.apache.spark.SparkContext
import org.apache.spark.mllib.linalg._
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.stat.Statistics._

​ 接下來,我們從資料集中選擇要分析的資料,比如說我們取出iris資料集中的前兩條資料v1和v2。不同的輸入型別決定了是做擬合度檢驗還是獨立性檢驗。擬合度檢驗要求輸入為Vector, 獨立性檢驗要求輸入是Matrix。

scala> val v1: Vector = sc.textFile("G:/spark/iris.data").map(_.split(",")).map(p => Vectors.dense(p(0).toDouble, p(1).toDouble, p(2).toDouble, p(3
).toDouble)).first v1: org.apache.spark.mllib.linalg.Vector = [5.1,3.5,1.4,0.2] scala> val v2: Vector = sc.textFile("G:/spark/iris.data").map(_.split(",")).map(p => Vectors.dense(p(0).toDouble, p(1).toDouble, p(2).toDouble, p(3).toDouble)).take(2).last v2: org.apache.spark.mllib.linalg.Vector = [4.9
,3.0,1.4,0.2]

(一) 適合度檢驗 Goodness fo fit

Goodness fo fit(適合度檢驗):驗證一組觀察值的次數分配是否異於理論上的分配。其 H0假設(虛無假設,null hypothesis)為一個樣本中已發生事件的次數分配會服從某個特定的理論分配。實際執行多項式試驗而得到的觀察次數,與虛無假設的期望次數相比較,檢驗二者接近的程度,利用樣本資料以檢驗總體分佈是否為某一特定分佈的統計方法。

通常情況下這個特定的理論分配指的是均勻分配,目前Spark預設的是均勻分配。以下是程式碼:

scala> val goodnessOfFitTestResult = Statistics.chiSqTest(v1)
goodnessOfFitTestResult: org.apache.spark.mllib.stat.test.ChiSqTestResult =
Chi squared test summary:
method: pearson
degrees of freedom = 3
statistic = 5.588235294117647
pValue = 0.1334553914430291
No presumption against null hypothesis: observed follows the same distribution as expected..

可以看到P值,自由度,檢驗統計量,所使用的方法,以及零假設等資訊。我們先簡單介紹下每個輸出的意義:

method: 方法。這裡採用pearson方法。

statistic: 檢驗統計量。簡單來說就是用來決定是否可以拒絕原假設的證據。檢驗統計量的值是利用樣本資料計算得到的,它代表了樣本中的資訊。檢驗統計量的絕對值越大,拒絕原假設的理由越充分,反之,不拒絕原假設的理由越充分。

degrees of freedom:自由度。表示可自由變動的樣本觀測值的數目,

pValue:統計學根據顯著性檢驗方法所得到的P 值。一般以P < 0.05 為顯著, P<0.01 為非常顯著,其含義是樣本間的差異由抽樣誤差所致的概率小於0.05 或0.01。

一般來說,假設檢驗主要看P值就夠了。在本例中pValue =0.133,說明兩組的差別無顯著意義。通過V1的觀測值[5.1, 3.5, 1.4, 0.2],無法拒絕其服從於期望分配(這裡預設是均勻分配)的假設。

(二)獨立性檢驗 Indenpendence

卡方獨立性檢驗是用來檢驗兩個屬性間是否獨立。其中一個屬性做為行,另外一個做為列,通過貌似相關的關係考察其是否真實存在相關性。比如天氣溫變化和肺炎發病率。

首先,我們通過v1、v2構造一個舉證Matrix,然後進行獨立性檢驗:

scala> val mat: Matrix =Matrices.dense(2,2,Array(v1(0),v1(1),v2(0),v2(1)))
mat: org.apache.spark.mllib.linalg.Matrix =
5.1  4.9
3.5  3.0
scala> val a =Statistics.chiSqTest(mat)
a: org.apache.spark.mllib.stat.test.ChiSqTestResult =
Chi squared test summary:
method: pearson
degrees of freedom = 1
statistic = 0.012787584067389817
pValue = 0.90996538641943
No presumption against null hypothesis: the occurrence of the outcomes is statistically independent..

​ 這裡所要檢驗是否獨立的兩個屬性,一個是樣本的序號,另一個是樣本的資料值。在本例中pValue =0.91,說明無法拒絕“樣本序號與資料值無關”的假設。這也符合資料集的實際情況,因為v1和v2是從同一個樣本抽取的兩條資料,樣本的序號與資料的取值應該是沒有關係的。

我們也可以把v1作為樣本,把v2作為期望值,進行卡方檢驗:

scala> val c1 = Statistics.chiSqTest(v1, v2)
c1: org.apache.spark.mllib.stat.test.ChiSqTestResult =
Chi squared test summary:
method: pearson
degrees of freedom = 3
statistic = 0.03717820461517941
pValue = 0.9981145601231336
No presumption against null hypothesis: observed follows the same distribution as expected..

本例中pValue =0.998,說明樣本v1與期望值等於V2的資料分佈並無顯著差異。事實上,v1=[5.1,3.5,1.4,0.2]與v2= [4.9,3.0,1.4,0.2]很像,v1可以看做是從期望值為v2的資料分佈中抽樣出來的的。

同樣的,鍵值對也可以進行獨立性檢驗,這裡我們取iris的資料組成鍵值對:

scala> val data=sc.textFile("G:/spark/iris.data")
data: org.apache.spark.rdd.RDD[String] = G:/spark/iris.data MapPartitionsRDD[13] at textFile at :44
scala>     val obs = data.map{ line =>
     |       val parts = line.split(',')
     |       LabeledPoint(if(parts(4)=="Iris-setosa") 0.toDouble else if (parts(4)=="Iris-versicolor") 1.toDouble else
     |       2.toDouble, Vectors.dense(parts(0).toDouble,parts(1).toDouble,parts
(2).toDouble,parts(3).toDouble))}
obs: org.apache.spark.rdd.RDD[org.apache.spark.mllib.regression.LabeledPoint] = MapPartitionsRDD[14] at map at :46

​ 進行獨立性檢驗,返回一個包含每個特徵對於標籤的卡方檢驗的陣列:

scala> val featureTestResults= Statistics.chiSqTest(obs)
featureTestResults: Array[org.apache.spark.mllib.stat.test.ChiSqTestResult] =
Array(Chi squared test summary:
method: pearson
degrees of freedom = 68
statistic = 156.26666666666665
pValue = 6.6659873176888595E-9
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent.., Chi squared test summary:
method: pearson
degrees of freedom = 44
statistic = 88.36446886446883
pValue = 8.303947787857702E-5
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent.., Chi squared test summary:
method: pearson
degrees of freedom = 84
statistic = 271.79999999999995
pValue = 0.0
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent.., Chi...

​ 這裡實際上是把特徵資料中的每一列都與標籤進行獨立性檢驗。可以看出,P值都非常小,說明可以拒絕“某列與標籤列無關”的假設。也就是說,可以認為每一列的資料都與最後的標籤有相關性。我們用foreach把完整結果打印出來:

scala> var i = 1
i: Int = 1
scala> featureTestResults.foreach { result =>
     |   println(s"Column $i:\n$result")
     |   i += 1
     | }
Column 1:
Chi squared test summary:
method: pearson
degrees of freedom = 68
statistic = 156.26666666666665
pValue = 6.6659873176888595E-9
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent..
Column 2:
Chi squared test summary:
method: pearson
degrees of freedom = 44
statistic = 88.36446886446883
pValue = 8.303947787857702E-5
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent..
Column 3:
Chi squared test summary:
method: pearson
degrees of freedom = 84
statistic = 271.79999999999995
pValue = 0.0
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent..
Column 4:
Chi squared test summary:
method: pearson
degrees of freedom = 42
statistic = 271.75
pValue = 0.0
Very strong presumption against null hypothesis: the occurrence of the outcomes
is statistically independent..

spark也支援Kolmogorov-Smirnov 檢驗,下面將展示具體的步驟:

scala> val test = sc.textFile("G:/spark/iris.data").map(_.split(",")).map(p => p(0).toDouble)
test: org.apache.spark.rdd.RDD[Double] = MapPartitionsRDD[22] at map at :44
// run a KS test for the sample versus a standard normal distribution
scala> val testResult = Statistics.kolmogorovSmirnovTest(test, "norm", 0, 1)
testResult: org.apache.spark.mllib.stat.test.KolmogorovSmirnovTestResult =
Kolmogorov-Smirnov test summary:
degrees of freedom = 0
statistic = 0.999991460094529
pValue = 0.0
Very strong presumption against null hypothesis: Sample follows theoretical distribution.
// perform a KS test using a cumulative distribution function of our making
scala>     val myCDF: Double => Double = (p=>p*2)
myCDF: Double => Double = 
scala>     val testResult2 = Statistics.kolmogorovSmirnovTest(test, myCDF)
testResult2: org.apache.spark.mllib.stat.test.KolmogorovSmirnovTestResult = Kolmogorov-Smirnov test summary:
degrees of freedom = 0
statistic = 14.806666666666668
pValue = 0.0
Very strong presumption against null hypothesis: Sample follows theoretical distribution.

六、隨機數生成 Random data generation

​ RandomRDDs 是一個工具集,用來生成含有隨機數的RDD,可以按各種給定的分佈模式生成資料集,Random RDDs包下現支援正態分佈、泊松分佈和均勻分佈三種分佈方式。RandomRDDs提供隨機double RDDS或vector RDDS。

​ 下面的例子中生成一個隨機double RDD,其值是標準正態分佈N(0,1),然後將其對映到N(1,4)。

​ 首先,匯入必要的包:

import org.apache.spark.SparkContext
import org.apache.spark.mllib.random.RandomRDDs._

​ 生成1000000個服從正態分配N(0,1)的RDD[Double],並且分佈在 10 個分割槽中:

scala> val u = normalRDD(sc, 10000000L, 10)
u: org.apache.spark.rdd.RDD[Double] = RandomRDD[35] at RDD at RandomRDD.scala:38

​ 把生成的隨機數轉化成N(1,4) 正態分佈:

scala> val v = u.map(x => 1.0 + 2.0 * x)
v: org.apache.spark.rdd.RDD[Double] = MapPartitionsRDD[36] at map at :50

七、核密度估計 Kernel density estimation

​ Spark ML 提供了一個工具類 KernelDensity 用於核密度估算,核密度估算的意思是根據已知的樣本估計未知的密度,屬於非引數檢驗方法之一。核密度估計的原理是。觀察某一事物的已知分佈,如果某一個數在觀察中出現了,可認為這個數的概率密度很大,和這個數比較近的數的概率密度也會比較大,而那些離這個數遠的數的概率密度會比較小。Spark1.6.2版本支援高斯核(Gaussian kernel)。

​ 首先,匯入必要的包:

import org.apache.spark.mllib.stat.KernelDensity
import org.apache.spark.rdd.RDD

​ 同時留意到已經匯入的資料:

scala> val test = sc.textFile("G:/spark/iris.data").map(_.split(",")).map(p => p(0).toDouble)
test: org.apache.spark.rdd.RDD[Double] = MapPartitionsRDD[22] at map at :44

用樣本資料構建核函式,這裡用假設檢驗中得到的iris的第一個屬性的資料作為樣本資料進行估計:


scala> val kd = new KernelDensity().setSample(test).setBandwidth(3.0)
kd: org.apache.spark.mllib.stat.KernelDensity = org.apache.spark.mllib.stat.KernelDensity@26216fa3

其中setBandwidth表示高斯核的寬度,為一個平滑引數,可以看做是高斯核的標準差。

構造了核密度估計kd,就可以對給定資料資料進行核估計:

scala> val densities = kd.estimate(Array(-1.0, 2.0, 5.0, 5.8))
densities: Array[Double] = Array(0.011372003554433524, 0.059925911357198915, 0.12365409462424519, 0.12816280708978114)

這裡表示的是,在樣本-1.0, 2.0, 5.0, 5.8等樣本點上,其估算的概率密度函式值分別是:0.011372003554433524, 0.059925911357198915, 0.12365409462424519, 0.12816280708978114。 


相關推薦

spark----基本統計工具2

來源:http://mocom.xmu.edu.cn/article/show/584d1fc5bd8177b41ebbd8bc/0/1 五、假設檢驗 Hypothesis testing ​ Spark目前支援皮爾森卡方檢測(Pearson’s chi-square

R語言實戰 - 基本統計分析1- 描述性統計分析

4.3 summary eas 方法 func -- 4.4 1.0 6.5 > vars <- c("mpg", "hp", "wt") > head(mtcars[vars]) mpg hp wt Maz

#使用abp框架與vue一步一步寫我是月老的小工具2 後臺搭建初體驗

使用 IT UC 文件 情況 base https 檢查 目標 #使用abp框架與vue一步一步寫我是月老的小工具(2) 後臺搭建初體驗 一、續上前言 關於這個小玩意的產品思考,假設我暫時把他叫我是月老熱心人 這是一個沒有中心的關系鏈,每個人進入以後都

Apache 流框架 Flink,Spark Streaming,Storm對比分析2

此文已由作者嶽猛授權網易雲社群釋出。 歡迎訪問網易雲社群,瞭解更多網易技術產品運營經驗。 2.Spark Streaming架構及特性分析 2.1 基本架構 基於是spark core的spark streaming架構。 Spark Streaming是將流式計算分解成一系列短小的批處理作業。這裡的批處

影象處理基本概念筆記2

作者:cvvision 連結:http://www.cvvision.cn/8907.html 二、 22、均值濾波 均值濾波是典型的線性濾波演算法,它是指在影象上對目標畫素給一個模板,該模板包括了其周圍的臨近畫素(以目標象素為中心的周圍8個畫素,構成一個濾波模板,即去掉目標畫素本身),再用模

機器學習十經典演算法:2k-means演算法

1.基本Kmeans演算法[1] [cpp]  view plain  copy 選擇K個點作為初始質心   repeat  

Java和資料結合學習2

抽象類 1.abstract宣告; 為什麼介面的物件可以指向例項化物件呢? 介面 1.介面不能例項化,但是介面的物件可以指向實現類的物件。 2.介面內的方法預設為public abstract,所以不能被例項化, 3.介面其實就是為了實現多繼承

通用工具2---Tuple

Tuple作為pair的擴充套件,可以擁有任意數量的元素 tuple的操作 tuple也是能預設構造,拷貝構造,賦值,比較等,他們都支援隱式轉換,使用make_tuple,其所有元素型別都是通過自動推導獲得型別。使用get時候不允許執行期才傳入索引值。 tuple<int,

流式資料計算實踐2----Hadoop叢集和Zookeeper

一、前言 1、上一文搭建好了Hadoop單機模式,這一文繼續搭建Hadoop叢集 二、搭建Hadoop叢集 1、根據上文的流程得到兩臺單機模式的機器,並保證兩臺單機模式正常啟動,記得第二臺機器core-site.xml內的fs.defaultFS引數值要改成本機的來啟動,啟動完畢後再改回來 2、清空資

Spark Streaming實時流處理筆記2—— 實時處理介紹

1 實時和離線計算對比 1.1 資料來源 離線:HDFS 歷史資料,資料量較大 實時:訊息佇列(Kafka) 1.2 處理過程 離線:Mapreduce 實時:Spark(DStream/SS) 1.3 處理速度 離

Java開發小工具2生成可雙擊執行的exe檔案

一、概述在Java開發小工具(1)中,我們已經可以生成一個可執行的jar包,這篇博文我們將這個jar包通過工具(exe4j)包裝成一個exe檔案,這樣不用在cmd中執行java -jar XXX了,可以

資料預處理——基本統計描述

        如果想要進行一次成功的資料預處理,把握資料的全貌是至關重要的。而基本統計描述可以用來識別資料的性質,即資料的分佈特點,如離散點的識別問題等。 中心趨勢度量:均值、中位數、眾數 均值:衡量一組資料的平均水平,不必多說。可是

Spark之訓練分類模型練習2

上接博文。 1 改進模型及引數調優 1.1 數值特徵標準化 使用RowMatrix類計算列的統計量。每一行為某一樣本的特徵向量 import org.apache.spark.mllib.linalg.distributed.RowMatrix

資料運算系統2--- 圖計算系統

同步圖運算:訊息傳遞 非同步圖運算:共享記憶體,可以立即看到完成的計算結果 一、同步圖運算系統 1、圖演算法 (1)PageRank Google用於對網頁重要性打分的演算法。 頂點:網頁 邊:超連結 (2)計算方法 初始化:所有頂點的PageRank為1/N 迭代:

用wxpython來做自己的第一個介面小工具2

本節我們需要新增panel ,你可以理解為面板。一個大主介面,需要有一個或者更多面版。各種控制元件:按鈕/輸入框/靜態文字 什麼的都是放在這個面板上的 先來看第一節成功之後的程式碼: class testFrame(wx.Frame): def

TensorFlow 入門 第一課--基本函式學習2:tf.nn.conv2d 、tf.contrib.layers.flatten、tf.nn.max_pool 詳解

Tensorflow 提供了一些內建的API實現了CNN網路結構中的卷積,池化,全連線網路等運算操作。tf.nn.conv2d(input,filter, strides, padding, data_

DevOps 10IT管理工具翻譯

使用最新的IT管理工具就像嘗試星巴克的最新飲品。下面我們簡單的過一下從監控到DevOps編排以及APM最常用的工具。    IT管理工具進入一個全盛時期,這需要感謝雲端計算、DevOps及移動的到來。基於新的基礎設施、軟體及開發方法,開發者和IT經理期待每一天都

python學習記錄之---------PYQT5做工具2下拉選項框的使用

工具/版本(1)安裝環境:Windows7 64bit(2)使用版本Python3.6(3)PYQT5(4)eric6-6.1.0基本的使用前文有介紹,直入正題初始形態選擇省份後,列出對應省份所有城市,都選擇後,點選開始,顯示在文字框中實現方法,首先將所有省份和對應城市放在對

python 內建資料結構的基本操作 —— dict2

A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapp

資料分析工具

查詢引擎 一、Phoenix 簡介:這是一個Java中間層,可以讓開發者在Apache HBase上執行SQL查詢。Phoenix完全使用Java編寫,程式碼位於GitHub上,並且提供了一個客戶端可嵌入的JDBC驅動。 Phoenix查詢引擎會將SQL查詢轉換為