20150825 [學習筆記] R與資料探勘 (2)

整理自 R軟體與資料探勘之開發與應用 -- 決策樹、支援向量機

[ GitHub ]
 
 
  • 背景知識
 
 
  • 學習內容
  • R 軟體安裝 (略過)
  • R Studio軟體安裝 (略過)
 
  • 資料探勘方法
  • START : 訓練資料和測試資料 (完成)
 
  • 分類演算法
  • (一)倒傳遞類神經網路(Back-Propagation Neural Network) (完成)
  • (二)決策樹(Decision Tree)
  • (三)支援向量機(Support Vector Machine)
  • (四)貝式分類(Naïve Bayes)
  • (五)k位最近鄰居法(k-Nearest Neighbor)
 
  • 分群演算法
  • (六)階層分群演算法(Hierarchical Clustering)
  • (七)k平均演算法(k-Means)
  • (八)模糊c平均演算法(Fuzzy c-Means)
  • (九)期望最大值演算法(Expectation-Maximization)
  • (十)自組織映射圖(Self Organizing Maps)
 
 
 
 

(二)決策樹(Decision Tree)

[前置作業] 基本概念
  • 相關知識
 
 
STAGE 1 : 訓練階段
  • 1.  新增R程式 , 在R Script中撰寫 , 並將檔案另存為"decision tree.R": 
install.packages('party')
library("party")
 
# training stage
trainingdata <- read.csv(file.choose())
 
tree.model <- ctree(factor(Species)~Sepal.Length + Sepal.Width + Petal.Length +
Petal.Width, data=trainingdata)
 
print(tree.model)
plot(tree.model)
 
  • 2.  存檔後把程式碼標記起來點選 run , 在對話窗中選擇之前在"START : 訓練資料與測試資料"建立的"trainingdata.csv" : 
 
  • 3.  顯示訓練結果
 
 
STAGE 2 : 測試階段
  • 1.  在R Script中新增以下內容 , 完成後把它標記起來並點選 run : 
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
 
results <- predict(tree.model, newdata = testingdata)
print(results)
 
  •  
 
  • 2.   在對話視窗中選擇之前在"START : 訓練資料與測試資料"建立的"testingdata.csv" : 
  •  
  • 3.  顯示測試結果
 
 
STAGE 3 : 評估階段
  • 1.  在R Script中新增以下內容 , 完成後把它標記起來並點選 run : 
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results)/ length(testingtarget)
sprintf("%.2f%%", accuracy * 100)
 
  • 2.  顯示分類結果 , 以及得知正確率為 94.67%
 
 
 
 

(三)支援向量機(Support Vector Machine)

[前置作業] 基本概念
  • 相關知識
 
 
STAGE 1 : 訓練階段
  • 1.  新增R程式 , 在R Script中撰寫 , 並將檔案另存為"support vector machine.R": 
install.packages("e1071")
library("e1071")
 
# training stage
trainingdata <- read.csv(file.choose())
 
svm.model <- svm(factor(Species)~Sepal.Length + Sepal.Width + Petal.Length +
                   Petal.Width, data=trainingdata)
 
print(svm.model)
plot(svm.model,trainingdata,Petal.Length~Petal.Width)
 
 
  • 2.  在存檔後把程式碼標記起來點選 run , 並且在對話窗中選擇之前建立的"trainingdata.csv" : 
  •  
  • 3.  顯示訓練結果
  •  
 
STAGE 2 : 測試階段
  • 1.  在R Script中新增以下內容 , 完成後把它標記起來並點選 run : 
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
 
results <- predict(tree.model, newdata = testingdata)
print(results)
 
  •  
  •  
  • 2.   在對話視窗中選擇之前建立的"testingdata.csv" : 
  •  
  • 3.  顯示測試結果
  •  
 
STAGE 3 : 評估階段
  • 1.  在R Script中新增以下內容 , 完成後把它標記起來並點選 run : 
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results) /length(testingtarget)
sprintf("%.2f%%", accuracy * 100)
 
  •  
  • 2.  顯示分類結果 , 以及得知正確率為 96.00%