Diff of /bin/naive_bayes.r [000000] .. [868c5d]

Switch to unified view

a b/bin/naive_bayes.r
1
setwd(".")
2
options(stringsAsFactors = FALSE)
3
# library("clusterSim")
4
# library("PRROC")
5
library("e1071")
6
7
source("./confusion_matrix_rates.r")
8
9
threshold <- 0.5
10
11
cat("threshold = ", threshold, "\n", sep="")
12
13
fileName <- "../data/LungCancerDataset_AllRecords_NORM_27reduced_features.csv"
14
prc_data_norm <- read.csv(file=fileName,head=TRUE,sep=",",stringsAsFactors=FALSE)
15
16
prc_data_norm <- prc_data_norm[sample(nrow(prc_data_norm)),] # shuffle the rows
17
18
target_index <- dim(prc_data_norm)[2]
19
20
training_set_perce = 80
21
cat("training_set_perce = ", training_set_perce, "%\n", sep="")
22
23
# the training set is the first 60% of the whole dataset
24
training_set_first_index <- 1 # NEW
25
training_set_last_index <- round(dim(prc_data_norm)[1]*training_set_perce/100) # NEW
26
27
# the test set is the last 40% of the whole dataset
28
test_set_first_index <- training_set_last_index+1 # NEW
29
test_set_last_index <- dim(prc_data_norm)[1] # NEW
30
31
cat("[Creating the subsets for the values]\n")
32
prc_data_train <- prc_data_norm[training_set_first_index:training_set_last_index, 1:(target_index)] # NEW
33
prc_data_test <- prc_data_norm[test_set_first_index:test_set_last_index, 1:(target_index)] # NEW
34
35
cat("[Creating the subsets for the labels \"1\"-\"0\"]\n")
36
prc_data_train_labels <- prc_data_norm[training_set_first_index:training_set_last_index, target_index] # NEW
37
prc_data_test_labels <- prc_data_norm[test_set_first_index:test_set_last_index, target_index]   # NEW
38
39
40
print("dim(prc_data_train)")
41
print(dim(prc_data_train))
42
43
print("dim(prc_data_test)")
44
print(dim(prc_data_test))
45
46
47
library(class)
48
library(gmodels)
49
50
naive_bayes_model <-  naiveBayes(as.factor(Metastasis) ~ . , data=prc_data_train)
51
52
prc_data_test_PRED <- predict((naive_bayes_model), prc_data_test)
53
prc_data_test_PRED_binary <- as.numeric(prc_data_test_PRED)-1
54
55
prc_data_test_PRED_binary[prc_data_test_PRED_binary>=threshold]=1
56
prc_data_test_PRED_binary[prc_data_test_PRED_binary<threshold]=0
57
58
# print("predictions:")
59
# print(prc_data_test_PRED_binary)
60
# 
61
# 
62
# print("labels:")
63
# print(prc_data_test$Metastasis)
64
65
66
confusion_matrix_rates(prc_data_test_labels, prc_data_test_PRED_binary, "@@@ Test set @@@")
67
68
69