You are on page 1of 111

R


R
RStudio

(Back-Propagation Neural Network)


(Decision Tree)
(Support Vector Machine)
(Nave Bayes)
k(k-Nearest Neighbor)
(Hierarchical Clustering)
k(k-Means)
c(Fuzzy c-Means)
(Expectation-Maximization)
(Self Organizing Maps)

R
http://cran.csie.ntu.edu.tw/

R
base

R
R-3.0.2-win.exe

10

11

RStudio

http://www.rstudio.com/ide/download/

12

RStudio

13

RStudio

RStudio-0.98.493.exe

14

RStudio

15

RStudio

16

RStudio

RStudio

17

RStudio

RStudio

18

RStudio

19

(Back-Propagation Neural Network)


(Decision Tree)
(Support Vector Machine)
(Nave Bayes)
k(k-Nearest Neighbor)

20

(Hierarchical Clustering)
k(k-Means)
c(Fuzzy c-Means)
(Expectation-Maximization)
(Self Organizing Maps)

File\New File\R ScriptR

21

iris
R Script
write.csv(iris, file.choose())

22

rundata.csv

23

data.csviris150
setosa1
versicolor2
virginica3
trainingdata.csv
testingdata.csv

24

File\New File\R ScriptR

25

R Script
install.packages('neuralnet')
library("neuralnet")
# training stage
trainingdata <- read.csv(file.choose())
net.model <- neuralnet(Species ~ Sepal.Length + Sepal.Width
+ Petal.Length + Petal.Width,trainingdata, hidden=10,
threshold=0.01)
print(net.model)
plot(net.model)
26

run

27

28

R Script
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
results <- compute(net.model, testingdata)
print(round(results$net.result))

29

run

30

31

R Script
# accuracy
table(testingtarget, round(results$net.result))
accuracy <- sum(testingtarget ==
round(results$net.result))/length(testingtarget)
sprintf("%.2f%%", accuracy * 100)

32

run

33

96.00%

34

File\New File\R ScriptR

35

R Script
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)
36

run

37

38

R Script
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
results <- predict(tree.model, newdata = testingdata)
print(results)

39

run

40

41

R Script
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results)/
length(testingtarget)
sprintf("%.2f%%", accuracy * 100)

42

run

43

94.67%

44

File\New File\R ScriptR

45

R Script
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)
46

run

47

48

R Script
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
results <- predict(svm.model, newdata = testingdata)
print(results)

49

run

50

51

R Script
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results)
/length(testingtarget)
sprintf("%.2f%%", accuracy * 100)

52

run

53

96.00%

54

File\New File\R ScriptR

55

R Script
library("e1071")
# training stage
trainingdata <- read.csv(file.choose())
nb.model <- naiveBayes(factor(Species)~Sepal.Length +
Sepal.Width + Petal.Length +
Petal.Width, data=trainingdata)
print(nb.model)
56

run

57

58

R Script
# testing stage
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species
results <- predict(nb.model, newdata = testingdata)
print(results)

59

run

60

61

R Script
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results)
/length(testingtarget)
sprintf("%.2f%%", accuracy * 100)

62

run

63

96.00%

64

File\New File\R ScriptR

65

k-

R Script
# training data
trainingfile <- read.csv(file.choose())
trainingdata <- subset(trainingfile, select = -Species)
trainingtarget <- trainingfile$Species

66

k-

run

67

k-

R Script
# testing data
testingfile <- read.csv(file.choose())
testingdata <- subset(testingfile, select = -Species)
testingtarget <- testingfile$Species

68

k-

run

69

k-

R Script
# computation
results <- knn(trainingdata, testingdata, trainingtarget, k =
3, prob=TRUE)
print(results)

70

k-

run

71

k-

72

k-

R Script
# accuracy
table(results, testingtarget)
accuracy <- sum(testingtarget == results)
/length(testingtarget)
sprintf("%.2f%%", accuracy * 100)

73

k-

run

74

k-

96.00%

75

File\New File\R ScriptR

76

R Script
# input data
inputdata <- read.csv(file.choose())
# computation
hc.model <- hclust(dist(inputdata), method="ave")
print(hc.model)

77

run

78

79

R Script
# evaluation
plot(hc.model, hang = -1, labels=inputdata$Species)

80

run

81

82

83

File\New File\R ScriptR

84

k-

R Script
# input data
inputdata <- read.csv(file.choose())
# computation
km.model <- kmeans(inputdata, 3)
print(km.model)

85

k-

run

86

k-

87

k-

R Script
# evaluation
table(inputdata$Species, km.model$cluster)
plot(inputdata[c("Sepal.Length", "Sepal.Width")],
col=km.model$cluster)
points(km.model$centers[,c("Sepal.Length",
"Sepal.Width")],
col=1:3, pch=8, cex=2)

88

k-

run

89

k-

90

File\New File\R ScriptR

91

c-

R Script
library(e1071)
# input data
inputfile <- read.csv(file.choose())
inputdata <- subset(inputfile, select = -Species)
# computation
cm.model <- cmeans(inputdata, 3, 100, m=2,
method="cmeans")
print(cm.model)
92

c-

run

93

c-

94

c-

R Script
# evaluation
table(inputfile$Species, cm.model$cluster)
plot(inputdata[,1], inputdata[,2], col=cm.model$cluster)

95

c-

run

96

c-

97

File\New File\R ScriptR

98

R Script
library(mclust)
# input data
inputfile <- read.csv(file.choose())
inputdata <- subset(inputfile, select = -Species)
# computation
em.model <- Mclust(inputdata, 3)
print(em.model)
99

run

100

101

R Script
# evaluation
table(inputfile$Species, em.model$classification)
plot(em.model, what=c('classification'), mens=c(3,4))

102

run

103

104

File\New File\R ScriptR

105

R Script
library(som)
# input data
inputfile <- read.csv(file.choose())
inputdata <- subset(inputfile, select = -Species)
inputdata.n <- normalize(inputdata, byrow=F)
# computation
som.model <- som(inputdata.n, xdim=4, ydim=4,
topol="rect", neigh="gaussian")
print(som.model)
106

run

107

108

R Script
# evaluation
plot(som.model)

109

run

110

111

You might also like