基於Spark的機器學習實踐 (六) - 基礎統計模塊

# 0 [相關源碼](https://github.com/Wasabi1234/Spark-MLlib-Tutorial) # 1 基礎統計模塊及常用統計學知識介紹 ◆ Spark 的基礎統計模塊即MLlib組件中的Basic Statistics部分 ◆ Basic Statistics主要包括Correlation 與Hypothesis testing等 ◆ 其大多被封裝在orq.apache spark.mllib.stat._ 中 ## 1.1 基礎統計學知識 ### 1.1.1 常用的統計學知識 ◆ 描述性統計 平均數,方差,衆數,中位數... ◆ 相關性度量 spark 提供了皮爾遜和斯皮爾曼相關係數,反映變量間相關關係密切程度 ◆ 假設檢驗 根據一定假設條件,由樣本推斷總體的一種統計學方法,spark提供了皮爾森卡方檢測 # 2 實戰統計彙總 ◆ 實戰的數據來源是北京市歷年降水量數據 ◆ 學習使用spark對數據進描述性統計 ◆ 在進行機器學習模型的訓練前,可以瞭解數據集的總體情況 ## 2.1 coding實戰 - 保存降水量文件 ![](https://upload-images.jianshu.io/upload_images/16782311-a89ea1a0720bf8ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 字符串值 ![](https://upload-images.jianshu.io/upload_images/16782311-66b17a4a2ea33630.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](https://upload-images.jianshu.io/upload_images/16782311-b6b775052e8e9816.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 實際內容只有一行,讀取到數組的是一個超長字符串,需要進行分割. ![](https://upload-images.jianshu.io/upload_images/16782311-95314cc5dec4fc8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - [所需依賴](https://spark.apache.org/docs/latest/mllib-statistics.html#summary-statistics) ![](https://upload-images.jianshu.io/upload_images/16782311-9d9c332d9fa900bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 導入 ![](https://upload-images.jianshu.io/upload_images/16782311-4f1b78c0706d915b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - val data = txt.flatMap(_.split(",")).map(value => linalg.Vectors.dense(value.toDouble)) ![](https://upload-images.jianshu.io/upload_images/16782311-91438edb8e8d24ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - data.take(10) ![](https://upload-images.jianshu.io/upload_images/16782311-f67b08873b671562.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 統計方法 - 最大值 ![](https://upload-images.jianshu.io/upload_images/16782311-b979d67b3b39edf0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 平均值 ![](https://upload-images.jianshu.io/upload_images/16782311-cced0e042a1db88e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) # 3 學習相關係數 ## 3.1 相關性度量 ◆ 是一種研究變量之間線性相關程度的量 ◆ 主要學習皮爾遜相關係數: ![](https://upload-images.jianshu.io/upload_images/16782311-c650b1e808b139f0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](https://upload-images.jianshu.io/upload_images/16782311-705c8ad096960127.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) >幾組(x, y)的點集,以及各個點集中x和y之間的相關係數。我們可以發現相關係數反映的是變量之間的線性關係和相關性的方向(第一排),而不是相關性的斜率(中間),也不是各種非線性關係(第三排)。請注意:中間的圖中斜率爲0,但相關係數是沒有意義的,因爲此時變量Y是0 ## 3.2 實戰相關係數 我們對北京市歷年降水量進行相關性統計,看看年份與降水量之間的相關性有多大 ![](https://upload-images.jianshu.io/upload_images/16782311-f9d2972a3e1297fd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](https://upload-images.jianshu.io/upload_images/16782311-88114ab970eba015.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 過濾 ![](https://upload-images.jianshu.io/upload_images/16782311-98c397a70d831bf9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![](https://upload-images.jianshu.io/upload_images/16782311-0515c66172fd86ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 相關係數值 ![](https://upload-images.jianshu.io/upload_images/16782311-80f9f273b374ba99.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) # 4 學習假設檢驗 ## 4.1 假設檢驗 ◆ 根據一定假設條件,由樣本推斷總體的一種統計學方法。基本思路是先提出假設(虛無假設),使用統計學方法進行計算,根據計算結果判斷是否`拒絕`假設 ◆ 假設檢驗的統計方法有很多,如卡方檢驗,T檢驗等 ◆ spark實現的是皮爾森卡方檢驗,它可以實現適配度檢測和獨立性檢測 ## 4.2 皮爾森卡方檢驗 最常用的卡方檢驗,可以分爲適配度檢驗和獨立性檢驗 ◆ 適配度檢驗:驗證觀察值的次數分配與理論值是否相等 ◆ 獨立性檢驗:兩個變量抽樣到的觀察值是否相互獨立 ## 4.3 實戰 : 判斷性別與左撇子是否存在關係 ![](https://upload-images.jianshu.io/upload_images/16782311-a531b613d399495e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 導入數據 ![](https://upload-images.jianshu.io/upload_images/16782311-0655a04dea30e153.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - 計算 ![](https://upload-images.jianshu.io/upload_images/16782311-d2c6ce572c7ab032.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 否定了假設檢驗,所以性別與左撇子是有關的! # Spark機器學習實踐系列 - [基於Spark的機器學習實踐 (一) - 初識機器學習](https://zhuanlan.zhihu.com/p/61667559) - [基於Spark的機器學習實踐 (二) - 初識MLlib](https://zhuanlan.zhihu.com/p/61784371) - [基於Spark的機器學習實踐 (三) - 實戰環境搭建](https://zhuanlan.zhihu.com/p/61848834) - [基於Spark的機器學習實踐 (四) - 數據可視化](https://zhuanlan.zhihu.com/p/61868232) - [基於Spark的機器學習實踐 (六) - 基礎統計模塊](https://zhuanlan.zhihu.com/p/62241911)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章