羅
羅左欣 8 years ago
要熟悉的概念有"決策樹"和"支援向量機" !
- 20150825 [學習筆記] R與資料探勘 (2)
- 整理自 R軟體與資料探勘之開發與應用 -- 決策樹、支援向量機
- [ GitHub ]
- (二)決策樹(Decision Tree)
- [前置作業] 基本概念
- STAGE 1 : 訓練階段
- STAGE 2 : 測試階段
- STAGE 3 : 評估階段
- (三)支援向量機(Support Vector Machine)
- [前置作業] 基本概念
- STAGE 1 : 訓練階段
- STAGE 2 : 測試階段
- STAGE 3 : 評估階段
整理自 R軟體與資料探勘之開發與應用 -- 決策樹、支援向量機
R 軟體安裝(略過)R Studio軟體安裝(略過)START : 訓練資料和測試資料(完成)(一)倒傳遞類神經網路(Back-Propagation Neural Network)(完成)(二)決策樹(Decision Tree)
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)
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
results <- predict(tree.model, newdata = testingdata)
print(results)
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results)/ length(testingtarget)
sprintf("%.2f%%", accuracy * 100)
(三)支援向量機(Support Vector Machine)
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)
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
results <- predict(tree.model, newdata = testingdata)
print(results)
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results) /length(testingtarget)
sprintf("%.2f%%", accuracy * 100)