-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classifiers.R
171 lines (146 loc) · 3.48 KB
/
Classifiers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
library(caret)
#1.
filename <- "BlogFeedback\\wine1.csv"
dataset <- read.csv(filename, header=FALSE)
colnames(dataset) <- c(
"ClassId",
"Alcohol",
"Malic acid",
"Ash",
"Alcalinity of ash",
"Magnesium",
"Total phenols",
"Flavanoids",
"Nonflavanoid phenols",
"Proanthocyanins",
"Color intensity",
"Hue",
"OD280/OD315",
"Proline"
)
#2.1
head(dataset,n=15)
#2.2
dim(dataset)
sapply(dataset, class)
#2.3
dataset$ClassId <- factor(dataset$ClassId)
table(dataset$ClassId)
percentage <- prop.table(table(dataset$ClassId)) * 100
cbind(freq=table(dataset$ClassId), percentage=percentage)
#2.4
summary(dataset)
sapply(dataset[2:14], sd)
library(moments)
skew <- apply(dataset[2:14], 2, skewness)
print(skew)
kurt <- apply(dataset[2:14], 2, kurtosis)
print(kurt)
#3
# split input and output
x <- dataset[,2:14]
y <- dataset[,1]
#4.1
#histogramy
par(mfrow=c(3,5))
for(i in 2:14) {
hist(x[,i-1], main=names(wine)[i])
}
# barplot for class breakdown
plot(y)
#4.2
correlations <- cor(dataset[2:14])
print(correlations)
install.packages("corrplot")
library(corrplot)
par(mfrow=c(1,1))
corrplot(correlations, method="circle")
#4.3
# boxplot for each attribute on one image
par(mfrow=c(3,5))
for(i in 2:14) {
boxplot(x[,i-1], main=names(wine)[i])
}
#4.4
# box and whisker plots for each attribute
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="ellipse", scales=scales)
#4.5
# density plots for each attribute by class value
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)
#5
colnames(dataset) <- make.names(colnames(dataset))
validationIndex <- createDataPartition(dataset$ClassId, p=0.75, list=FALSE)
validation <- dataset[-validationIndex,]
dataset <- dataset[validationIndex,]
#6
# Run algorithms using 10-fold cross validation
trainControl <- trainControl(method="cv", number=10)
metric <- "Accuracy"
#6.1
# Naive Bayes
set.seed(7)
fit.nb <- train(ClassId~., data=dataset, method="nb", metric=metric, trControl=trainControl)
#6.2
# KNN
set.seed(7)
fit.knn <- train(ClassId~., data=dataset, method="knn", metric=metric, trControl=trainControl)
#6.3
# SVM
set.seed(7)
fit.svm <- train(ClassId~., data=dataset, method="svmRadial", metric=metric, trControl=trainControl)
#6.4
# Decision Tree
set.seed(7)
fit.cart <- train(ClassId~., data=dataset, method="rpart", metric=metric,trControl=trainControl)
#6.5
#Random Forest
set.seed(7)
fit.rf <- train(ClassId~., data=dataset, method="rf", metric=metric, trControl=trainControl)
#6.6
# Neural Network
set.seed(7)
fit.nnet <- train(ClassId~., data=dataset, method="nnet", metric=metric, trControl=trainControl)
#7
# summarize accuracy of models
results <- resamples(list(nb=fit.nb, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf, nnet=fit.nnet))
summary(results)
dotplot(results)
#8
print(fit.svm)
predictions <- predict(fit.svm, validation)
predictions <- factor(predictions)
validation$ClassId <- factor(validation$ClassId)
#9
cm <- confusionMatrix(predictions, as.factor(validation$ClassId))
cm
tab <- cm$table
TP <- sum(tab[1,1])
TP
FN <- sum(tab[2:3,1])
FN
FP <- sum(tab[1,2:3])
FP
TN <- sum(tab[2:3,2:3])
TN
accuracy <- (TP+TN) / (TP+FN+FP+TN)
accuracy
TPR <- TP / (TP+FN)
TPR
SPEC = TN / (TN+FP)
SPEC
FPR <- FP/ (FP+TN)
FPR
FDR <- FP / (FP+TP)
FDR
POS <- TP/(TP+FP)
POS
NEG <- TN/(TN+FN)
NEG
F1 <-TP / (TP+0.5*(FP+FN))
F1
FBETA<-(1+(0.5^2)) * ((TPR*POS)/(((0.5^2)*TPR)+POS))
FBETA
MCC <- (TP*TN - FP*FN) / (sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN)))
MCC