Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.資料分類 Classification 分類範例一:鳶尾花各種分類模型 分類範例二:新生兒體重數值預測 分類範例三:交叉驗證與部署模型

Similar presentations


Presentation on theme: "2.資料分類 Classification 分類範例一:鳶尾花各種分類模型 分類範例二:新生兒體重數值預測 分類範例三:交叉驗證與部署模型"— Presentation transcript:

1 2.資料分類 Classification 分類範例一:鳶尾花各種分類模型 分類範例二:新生兒體重數值預測 分類範例三:交叉驗證與部署模型
分類範例四:客戶信用風險評估 4 2 3 1 分類範例三:交叉驗證與部署模型

2 函數【C5.0】 【tree】 【ctree】 【RandomForest】檔案iris 150 records 5 fields
資料分類範例 一 函數【C5.0】 【tree】 【ctree】 【RandomForest】檔案iris 150 records 5 fields

3 資料探勘:決策樹 決策樹(decision tree)是常用的資料探勘技術,可視為迴歸分析的擴充
決策樹可用於分類預測,此類決策樹稱為分類樹(classification tree),有些決策樹演算法可達成類似迴歸分析的數值應變數預測功能,此類決策樹稱為迴歸樹(regression tree) 決策樹是將資料依照每一階段不同的條件作循環切割(recursive partition),跟迴歸分析最大的不同在於一個解釋變數可在不同的切割階段被重複使用

4 決策樹圖例

5 R軟體常用的幾種決策樹 C5.0決策樹: R提供C50套件的C5.0函數
iris.C5=C5.0(Species~ . ,data=iris) CART分類迴歸樹: R提供tree和rpart套件(內建)的tree和rpart函數 iris.tree=tree(Species~ . ,data=iris) Random Forest隨機森林: R提供randomForest套件的randomForest函數 iris.rf=randomForest(Species~ . ,data=iris)

6 C5.0決策樹基本概念 Quinlan在1986年所提出的ID3演算法後,因其無法處理連續屬性的問題且不適用在處理大的資料集,因此1993又發表C5.0的前身4.5,直到現在所使用的C5.0決策樹演算法 C5.0演算法的結果可產生決策樹及規則集兩種模型,並且依最大資訊增益的欄位來切割樣本,並重複進行切割直到樣本子集不能再被分割爲止 C5.0能處理連續型變數與類別型的變數資料,,目標欄位必須是類別型變數

7 iris的C5.0決策樹程式碼 #test group(test.index sampling 30% records), remaining 70% as train group n=0.3*nrow(iris) test.index=sample(1:nrow(iris),n) iris.train=iris[-test.index,] iris.test=iris[test.index,] library(C50) iris.tree=C5.0(Species~ . ,data=iris.train) summary(iris.tree) plot(iris.tree)

8 #train confusion matrix
species.train=iris$Species[-test.index] train.pred=predict(iris.tree,iris.train,type='class') table.train=table(species.train,train.pred) table.train cat("Total records(train)=",nrow(iris.train),"\n") cat("Correct Classification Ratio(train)=", sum(diag(table.train))/sum(table.train)*100,"%\n") #test confusion matrix species.test=iris$Species[test.index] test.pred=predict(iris.tree,iris.test,type='class') table.test=table(species.test,test.pred) table.test cat("Total records(test)=",nrow(iris.test),"\n") cat("Correct Classification Ratio(test)=", sum(diag(table.test))/sum(table.test)*100,"%\n")

9 C5.0規則組及預測變數重要性

10 C5.0的決策樹圖

11 C5.0混淆矩陣和預測正確率

12 Modeler的決策樹模型

13 Modeler規則組及預測變數重要性

14 Modeler測試組的混淆矩陣和分析

15 比較party套件的ctree模型函數

16 ctree混淆矩陣和預測正確率

17 ctree的決策樹圖

18 隨機森林基本概念 隨機森林是由Brieman在2001年提出的Random Forest決策樹,將訓練樣本所有觀察值作多次抽出放回的隨機取樣,再用這些隨機樣本建構出數百數千棵決策樹,一個新物件會被分到哪個分類是由許多樹共同投票來決定 隨機森林可以應用在分類,也可以用在集群分析的領域 R提供randomForest套件建構randomForest決策樹,語法: library(randomForest) randomForest(Y ~.,data=…) .表示Y以外的其他所有變數

19 Random Forest程式碼 #test group(test.index sampling 30% records), remaining 70% as train group n=0.3*nrow(iris) test.index=sample(1:nrow(iris),n) iris.train=iris[-test.index,] iris.test=iris[test.index,] install.packages("randomForest") library(randomForest) iris.rf=randomForest(Species ~ ., data=iris.train,importance=T,proximity=T) print(iris.rf) round(importance(iris.rf),2)

20 names(iris.rf) (table.rf=iris.rf$confusion) cat("CORRECTION RATIO(train)=", sum(diag(table.rf)/sum(table.rf))*100,"%\n") #predict (rf.pred=predict(iris.rf,newdata=iris.test)) species.test=iris$Species[test.index] (table.test=table(Species=species.test,Predicted=rf.pred)) cat("CORRECTION RATIO(test)=", sum(diag(table.test)/sum(table.test))*100,"%\n") #clustering (iris.clutrf=randomForest(iris[,-5])) MDSplot(iris.clutrf,iris$Species,palette=rep(1,3), pch=as.numeric(iris$Species))

21 randomForest決策樹

22 訓練組混淆矩陣與預測正確率

23 測試組混淆矩陣與預測正確率

24 randomForest集群分析圖

25 函數【rpart】檔案babies.txt 1236 records 7 fields
資料分類範例二 函數【rpart】檔案babies.txt 1236 records 7 fields

26 分類迴歸樹基本概念 分類迴歸樹(CART, Classification and Regression Tree)由Brieman在1984年提出CART以反覆運算的方式,由根部開始反覆建立二元分支樹,直到樹節點中的同質性達到某個標準,或觸發反覆運算終止條件為止 CART的應變數欄位既可以是數值型資料,也可以是類別型資料 R提供tree和rpart建構CART決策樹,語法: rpart(Y ~ X1+X2+X3+…+Xk,data=…) tree(Y ~.,data=…) .表示Y以外的其他所有變數

27 babies資料檔範例 為了研究影響嬰兒體重的因素,使用rpart函數(Recursive Partitioning And Regression Trees),預測變數bwt是數值變數 資料的筆數計有1236筆,共有7個欄位, : 1. bwt:嬰兒體重 2. gestation:懷孕日數 3. parity:胎序,懷孕過幾胎 4. age:母親年齡 5. height :母親身高 6. weight :母親體重 7. smoke:母親抽煙與否,1表抽煙0表不抽煙

28 建立rpart模型的程式碼 babies=read.table("d:/stella/R/babies.txt",header=T)
babies=na.exclude(babies) n=0.3*nrow(babies) test.index=sample(1:nrow(babies),n) babies.train=babies[-test.index,] babies.test=babies[test.index,] #decision tree library(rpart) babies.tree=rpart(bwt~gestation+parity+age+height+ weight+smoke,data=babies.train) babies.tree plot(babies.tree) text(babies.tree)

29 rpart決策樹

30 rpart決策樹圖

31 MAPE:數值變數預測效果評估 MAPE(Mean Absolute Percentage Error)的公式為: 各個樣本的(實際值-預測值)/實際值取絕對值後的平均 MAPE<10%則預測效果為GOOD 10%<MAPE<20%則預測效果為OK MAPE>20%則預測效果為BAD predict函數需加上type=“vector”以傳回預測值(原為type=“class”)

32 MAPE評估的程式碼 #variable importance babies.tree$variable.importance
#MAPE of train and test group bwt.train=babies$bwt[-test.index] train.pred=predict(babies.tree,newdata=babies.train,type="vector") train.MAPE=mean(abs(bwt.train-train.pred)/bwt.train) cat("MAPE(train)=",train.MAPE*100,"%\n") bwt.test=babies$bwt[test.index] test.pred=predict(babies.tree,newdata=babies.test,type="vector") test.MAPE=mean(abs(bwt.test-test.pred)/bwt.test) cat("MAPE(test)=",test.MAPE*100,"%\n")

33 函數【C5.0】檔案iris 150 records 5 fields
資料分類範例三 函數【C5.0】檔案iris 150 records 5 fields

34 交叉驗證 目前為止我們使用過tree、 rpart 、 C5.0 、 ctree 、 randomForest等分類函數,分類正確率的預測又因為是取亂數,每次都不盡相同 ,所以我們希望各做10次再取平均值較為客觀 只要在程式片段以迴圈框住即可: for (i in 1:10) { }

35 交叉驗證10次的執行結果

36 自動化交叉驗證10次

37 模型的部署iris 部署(deployment)就是將模型運用在新資料上,亦即先訓練舊資料再以新資料預測
新資料檔iris_new2.txt有10筆新資料,有花瓣花萼長寬但沒有種類欄位,需由我們建好的iris模型來預測,又假設交叉驗證結果是C5.0模型勝出,所以我們決定用C5.0模型來預測 因為新資料檔沒有Species欄位,因此產生的模型就無法產生混淆矩陣和正確率分析的結果 預測後的分類結果要和新資料檔合併並且輸出成一個新檔iris_all.txt

38 新資料與預測後的結果輸出

39 模型部署的程式碼 #C5.0 irisnew2.txt: 10 records library(C50)
irisnew2=read.table("d:\\stella\\R\\iris_new2.txt",header=T,sep=",") #build C5.0 model iris.tree=C5.0(Species~ . ,data=iris) #predict irisnew2 iris.pred=predict(iris.tree,irisnew2,type='class') #merge predict result and output (irisall=data.frame(irisnew2,Spec.Pred=iris.pred)) write.table(irisall,"d:\\stella\\R\\iris_all.txt",row.names=F)

40 訓練iris C5.0模型再以新資料預測

41 資料分類範例四 函數【C5.0】 檔案risk.xlsx 4117 records 12 fields
檔案riskNew.xlsx records 10 fields

42 客戶信用風險評估 1.從risk.xlsx作資料準備 2.建立C5.0模型 3.判斷欄位的重要性 4.選出重要欄位重新建模
5.模型產生的規則組集 6.模型的決策樹圖 7.訓練和測試組正確率分析 8.以新資料riskNew.xlsx來預測

43 1. 從risk.xlsx作資料準備

44 2.建立C5.0模型

45 3.判斷欄位的重要性

46 4.選出重要欄位重新建模

47 5.模型產生的規則組集

48 6.模型的決策樹圖

49 7.訓練和測試組正確率分析

50 8.以新資料riskNew.xlsx預測

51 Modeler的混淆矩陣和分析結果

52 Modeler模型的直方圖


Download ppt "2.資料分類 Classification 分類範例一:鳶尾花各種分類模型 分類範例二:新生兒體重數值預測 分類範例三:交叉驗證與部署模型"

Similar presentations


Ads by Google