-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatient4.Rmd
198 lines (160 loc) · 4.48 KB
/
patient4.Rmd
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
title: "Patient4"
author: "Anuhya B S"
date: '2022-06-08'
output:
pdf_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
#Importing libraries
library('R.matlab')
library(caTools)
library(e1071)
library(class)
library(tree)
library(randomForest)
```
```{r}
#Loading Data
p1 <- readMat("data-science-P4.mat")
info <- as.data.frame(p1[2])
info <- t(info)
info <- as.data.frame(info)
lab.grp <-as.data.frame(matrix(nrow=0,ncol=1))
lab.wrd <-as.data.frame(matrix(nrow=0,ncol=1))
for (i in 1:360){
lab.grp <- rbind(lab.grp,info$cond[[i]])
lab.wrd <- rbind(lab.wrd,info$word[[i]])
}
p1.data <- p1$data
voxels <-as.data.frame(matrix(nrow=0,ncol=21764))
for (i in 1:360){
voxels <- rbind(voxels,p1.data[[i]][[1]])
}
```
```{r}
# Principal Component Analysis for Feature Reduction
pr.out <- prcomp(voxels)
cumsum((pr.out$sdev^2)/sum(pr.out$sdev^2))
pcs <- as.data.frame(pr.out$x[,1:300])
pcs$grp <- lab.grp$V1
#pcs$wrd <- lab.wrd$V1
```
```{r}
# Splitting data into training and test data
set.seed(100)
#sample <- sample(1:nrow(pcs), 300)
pcs.train <- pcs[1:300,]
pcs.test <- pcs[301:360,]
#pcs.train <- subset(pcs, sample == TRUE)
#pcs.test <- subset(pcs, sample == FALSE)
pcs.train.x <- subset(pcs.train, select = -c(grp))
pcs.train.labs <- pcs.train$grp
pcs.test.x <- subset(pcs.test, select = -c(grp))
pcs.test.labs <- pcs.test$grp
```
```{r}
# Classification Algorithms
# Naive Bayes Classifier
nb.fit <- naiveBayes(grp ~ . , data = pcs.train)
nb.class <- predict(nb.fit,pcs.test.x)
nb.class
confusion_mat.nb = as.matrix(table(Actual_Values = pcs.test.labs, Predicted_Values = nb.class))
print(confusion_mat.nb)
print(mean(nb.class == pcs.test$grp))
```
```{r}
# KNN
knn.pred <- knn(pcs.train.x, pcs.test.x, pcs.train.labs, k=5)
confusion_mat.knn = as.matrix(table(pcs.test.labs, knn.pred))
print(confusion_mat.knn)
print(mean(knn.pred == pcs.test$grp))
```
```{r}
# Decision Trees
set.seed(100)
tree.fit <- tree(as.factor(grp) ~ ., data = pcs.train)
summary(tree.fit)
plot(tree.fit)
text(tree.fit, pretty = 0)
tree.pred <- predict(tree.fit, newdata = pcs.test, type = "class")
tree.pred
confusion_mat.dt = as.matrix(table(pcs.test.labs, tree.pred))
print(confusion_mat.dt)
print(mean(tree.pred == pcs.test$grp))
```
```{r}
# Random Forest
rf.fit <- randomForest(as.factor(grp) ~ ., data = pcs.train,, mtry = 80, importance = TRUE)
summary(rf.fit)
rf.pred <- predict(rf.fit, newdata = pcs.test, type = "class")
confusion_mat.rf = as.matrix(table(pcs.test.labs, rf.pred))
print(confusion_mat.rf)
print(mean(rf.pred == pcs.test$grp))
```
```{r}
manmade <- c("furniture", "clothing", "manmade", "tool", "kitchen", "vehicle", "building", "buildpart")
natural <- c("insect", "animal", "vegetable", "bodypart")
df_new <- within(pcs, {
cls <- "manmade"
cls[grp %in% manmade] <- "manmade"
cls[grp %in% natural] <- "natural"
})
pcs$cls <- df_new$cls
```
```{r}
# Splitting data into training and test data
set.seed(100)
#sample <- sample(1:nrow(pcs), 300)
pcs.train <- pcs[1:300,]
pcs.test <- pcs[301:360,]
#pcs.train <- subset(pcs, sample == TRUE)
#pcs.test <- subset(pcs, sample == FALSE)
pcs.train.x <- subset(pcs.train, select = -c(grp,cls))
pcs.train.labs <- pcs.train$cls
pcs.test.x <- subset(pcs.test, select = -c(grp,cls))
pcs.test.labs <- pcs.test$cls
```
```{r}
# Classification Algorithms
# Naive Bayes Classifier
nb.fit <- naiveBayes(cls ~ . , data = pcs.train)
nb.class <- predict(nb.fit,pcs.test.x)
nb.class
confusion_mat.nb = as.matrix(table(Actual_Values = pcs.test.labs, Predicted_Values = nb.class))
print(confusion_mat.nb)
print(mean(nb.class == pcs.test$cls))
```
```{r}
# KNN
knn.pred <- knn(pcs.train.x, pcs.test.x, pcs.train.labs, k=3)
confusion_mat.knn = as.matrix(table(pcs.test.labs, knn.pred))
print(confusion_mat.knn)
print(mean(knn.pred== pcs.test$cls))
```
```{r}
# Decision Trees
set.seed(100)
tree.fit <- tree(as.factor(cls) ~ ., data = pcs.train)
summary(tree.fit)
plot(tree.fit)
text(tree.fit, pretty = 0)
tree.pred <- predict(tree.fit, newdata = pcs.test, type = "class")
tree.pred
confusion_mat.dt = as.matrix(table(pcs.test.labs, tree.pred))
print(confusion_mat.dt)
print(mean(tree.pred== pcs.test$cls))
```
```{r}
# Random Forest
rf.fit <- randomForest(as.factor(cls) ~ ., data = pcs.train,, mtry = 80, importance = TRUE)
summary(rf.fit)
rf.pred <- predict(rf.fit, newdata = pcs.test, type = "class")
confusion_mat.rf = as.matrix(table(pcs.test.labs, rf.pred))
print(confusion_mat.rf)
print(mean(rf.pred== pcs.test$cls))
```