第五章 資料科學平台
5.3 方法介紹
5.3.2 機器學習方法
在這一部分中,我們將介紹在這個網頁應用程序中使用的幾種機器學習方法,
包括一些樹模型,集合方法等。
5.3.2.1 決策樹
決策樹是一種預測模型,決策樹中每個分叉路徑表示一個可能的結果,每個葉 節點對應於從根節點到葉節點的路徑所表示的對象的值。數據挖掘中的決策樹是 一種常用技術在經過視覺化後有助於使用者了解決策樹配適模型的結果,可用於
分析數據也可用於預測。圖 5.17 顯示裝袋集成學習的介紹,及我們在介面中繪製
決策樹範例。
圖5.18 決策樹介紹頁面
進行完決策樹的基本介紹後,我們在下一部分中提供了使用者幾種使用採用 不同演算法的決策樹,例如:CART(Classification And Decision Tree)、C4.5 樹及 C5.0 樹。將分為三個頁面供使用者進行模型配適及視覺化,圖 5.18 為使用 CART 頁面配適之結果。
圖5.19 CART 樹模型配適結果
ui:
1. verbatimTextOutput("other_val_show_DCTree ") 2. plotOutput("DCTreePlot")
server:
1. observeEvent(input$goButton_DCT, {
output$other_val_show_DCTree<-renderPrint({
input$other_var_select_DCT input$ind_var_select_DCT f =upload_data()
library(rpart)
form=sprintf("%s~%s",input$ind_var_select_DCT, paste0(input$other_var_select_DCT,collapse="+")) DCTree = rpart(as.formula(form),data=f,method="class") print( DCTree) }) })
2. library(rpart.plot)
observeEvent(input$goButton_DCT, { output$DCTreePlot<-renderPlot({
prp(fitDCTree(), faclen=0, fallen.leaves=F, shadow.col="gray", extra=2) }) })
表5.9 CART 樹模型程式碼
5.3.2.2 Random Forest 隨機森林
隨機森林是一種結合隨機節點優化和裝袋集成學習使用 CART 過程構建森林
的方法。因此,在這一部分,我們首先介紹裝袋集成學習,隨後介紹隨機森林及其 優點,最後讓使用者使用上傳的資料配適隨機森林模型。下圖顯示隨機森林的示意 圖及優點,最後則是使用者使用配適隨機森林模型的準確率與誤差曲線。
圖5.20 隨機森林介紹頁面
ui:
1. verbatimTextOutput("other_val_show_RMF") 2. plotOutput("RMFPlot")
server:
1. library(randomForest)
output$other_val_show_RMF<-renderPrint({
input$other_var_select_RMF input$ind_var_select_RMF f<-upload_data()
form = sprintf("%s~%s",input$ind_var_select_RMF, paste0(input$other_var_select_RMF,collapse="+"))
RMF =randomForest(as.formula(form),data=f, ntree=100, nodesize=7,) print(RMF) })
2. output$RMFPlot<-renderPlot({
input$other_var_select_RMF input$ind_var_select_RMF f<-upload_data()
form = sprintf("%s~%s",input$ind_var_select_RMF, paste0(input$other_var_select_RMF,collapse="+"))
RMF <-randomForest(as.formula(form),data=f,ntree=100,nodesize=7) plot(RMF) })
圖5.22 單純貝氏分類器頁面
圖5.23 單純貝氏分類器模型配適結果頁面
ui:
verbatimTextOutput("other_val_show_ nbc") server:
input$ind_var_select_nbc f = upload_data()
form <- sprintf("%s~%s",input$ind_var_select_nbc, paste0(input$other_var_select_nbc,collapse="+")) NBclassfier <-naiveBayes(as.formula(form),data=f) print(NBclassfier) })
表5.11 單純貝式分類器模型程式碼
5.3.2.4 Adaboost
Adaboost(Adaptive Boosting,簡稱 Adaboost)是集成學習中的一種方法,是 Yoav Freund 和 Robert Schapire 提出的機器學習方法。Adaboost 的基本概念是將不 同權重分配給多個分類器,並通過加權多數投票對其進行分類,並改善前一步分類 器分類錯誤的樣本的權重。每次訓練新分類器時,它將集中於錯誤分類的訓練樣本。
在這一部分中,我們將讓使用者了解Adaboost 的原理,並使用一個簡單的範例向
用戶展示adaboost 改進每次迭代錯誤的過程。圖 5.23 中間的混淆矩陣表格顯示了 adaboost 中每次迭代改進錯誤的地方。
圖5.24 Adaboost 介紹頁面
在Adaboost 的下一頁中,我們讓使用者使用上傳的資料配適 Adaboost 模型,
他們可以選擇他們想要在模型中進行多少次迭代並繪製了誤差線圖,讓使用者知 道在多少次迭代後誤差將達到最小。
圖5.25 Adaboost 模型配適結果頁面
圖5.26 Adaboost 誤差下降曲線
ui:
1. verbatimTextOutput("other_val_show_ ADA") 2. plotOutput("ADAPlot")
server:
1. library("ada")
output$other_val_show_ADA<-renderPrint({
input$other_var_select_ADA input$ind_var_select_ADA f = upload_data()
form <- sprintf("%s~%s",input$ind_var_select_ADA, paste0(input$other_var_select_ADA,collapse="+"))
print(Adaboost) })
2. output$ADAPlot<-renderPlot({
input$other_var_select_ADA input$ind_var_select_ADA f = upload_data()
form <- sprintf("%s~%s",input$ind_var_select_ADA, paste0(input$other_var_select_ADA,collapse="+"))
Adaboost <-ada(as.formula(form),data=f,loss="exponential", type=c("real"),iter=100)
plot(Adaboost) })
表5.12 Adaboost 模型程式碼
5.3.2.5 支持向量機
支持向量機(簡稱SVM)是機器學習中常見的分類器,由 Vladimir N.Vapnik 和Alexey Ya Chervonenkis 於 1963 年所提出。它是一種通過調整參數來解決優化 問題的分類方法。支持向量機的基本概念是找到一個分隔不同類型數據的超平面,
這個超平面稱為分離超平面。分隔數據的兩個邊界稱為支持超平面,它們之間的距 離稱為邊界。我們將介紹它的原理及提供一個互動式支持向量機的例子,再更改支 持向量機的參數或是對資料進行調整時,可以馬上見到分離超平面的改變。
圖5.27 支持向量機介紹頁面
在讓使用者了解支持向量機之後,我們提供支持向量機和支持向量迴歸模型,
讓使用者使用上傳的資料配適模型。下圖顯示配適支持向量機及配適支持向量迴 歸的結果。
圖5.28 支持向量機模型配適結果頁面
圖5.29 支持向量迴歸配適結果頁面
ui:
verbatimTextOutput("other_val_show_ svm") server:
output$other_val_show_svm<-renderPrint({
input$other_var_select_svm input$ind_var_select_svm f = upload_data()
form <- sprintf("%s~%s",input$ind_var_select_svm, paste0(input$other_var_select_svm,collapse="+")) svm <-svm(as.formula(form),data=f)
print(svm) })
表5.13 支持向量機模型程式碼