[868c5d]: / bin / naive_bayes.r

Download this file

70 lines (44 with data), 2.1 kB

 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
setwd(".")
options(stringsAsFactors = FALSE)
# library("clusterSim")
# library("PRROC")
library("e1071")
source("./confusion_matrix_rates.r")
threshold <- 0.5
cat("threshold = ", threshold, "\n", sep="")
fileName <- "../data/LungCancerDataset_AllRecords_NORM_27reduced_features.csv"
prc_data_norm <- read.csv(file=fileName,head=TRUE,sep=",",stringsAsFactors=FALSE)
prc_data_norm <- prc_data_norm[sample(nrow(prc_data_norm)),] # shuffle the rows
target_index <- dim(prc_data_norm)[2]
training_set_perce = 80
cat("training_set_perce = ", training_set_perce, "%\n", sep="")
# the training set is the first 60% of the whole dataset
training_set_first_index <- 1 # NEW
training_set_last_index <- round(dim(prc_data_norm)[1]*training_set_perce/100) # NEW
# the test set is the last 40% of the whole dataset
test_set_first_index <- training_set_last_index+1 # NEW
test_set_last_index <- dim(prc_data_norm)[1] # NEW
cat("[Creating the subsets for the values]\n")
prc_data_train <- prc_data_norm[training_set_first_index:training_set_last_index, 1:(target_index)] # NEW
prc_data_test <- prc_data_norm[test_set_first_index:test_set_last_index, 1:(target_index)] # NEW
cat("[Creating the subsets for the labels \"1\"-\"0\"]\n")
prc_data_train_labels <- prc_data_norm[training_set_first_index:training_set_last_index, target_index] # NEW
prc_data_test_labels <- prc_data_norm[test_set_first_index:test_set_last_index, target_index] # NEW
print("dim(prc_data_train)")
print(dim(prc_data_train))
print("dim(prc_data_test)")
print(dim(prc_data_test))
library(class)
library(gmodels)
naive_bayes_model <- naiveBayes(as.factor(Metastasis) ~ . , data=prc_data_train)
prc_data_test_PRED <- predict((naive_bayes_model), prc_data_test)
prc_data_test_PRED_binary <- as.numeric(prc_data_test_PRED)-1
prc_data_test_PRED_binary[prc_data_test_PRED_binary>=threshold]=1
prc_data_test_PRED_binary[prc_data_test_PRED_binary<threshold]=0
# print("predictions:")
# print(prc_data_test_PRED_binary)
#
#
# print("labels:")
# print(prc_data_test$Metastasis)
confusion_matrix_rates(prc_data_test_labels, prc_data_test_PRED_binary, "@@@ Test set @@@")