-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRECONHECIMENTO_DE_LOCUTOR_RandomForest.R
116 lines (83 loc) · 3.98 KB
/
RECONHECIMENTO_DE_LOCUTOR_RandomForest.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
install.packages("neuralnet")
library(neuralnet)
library(RSNNS)
library(caret)
library(datasets)
library(randomForest)
############################## DATASET #############################
seed <- 123
metric <- "Accuracy"
setwd("C:/Program Files/RStudio")
tabela <-read.csv("gender_voice_dataset.csv", header = T, sep = ",", dec = ".")
tabela <-na.omit(tabela)
tabelaClass <- tabela[21]
# Apresenta quantidade respectiva as classes de veiculos presentes no conjunto de dados
#summary(tabela)
#table(tabela$Class)
## Preparacao dos dados: Separa 80% para treinamento e 20% para teste
set.seed(seed)
grupos <- sample(2, nrow(tabela), replace=TRUE, prob=c(0.8, 0.2))
dados_treinamento <- tabela[grupos==1,]
dados_teste <- tabela[grupos==2,]
#library(caret) caso usar o normalizeData
# Remove a coluna que identifica a classe de cada veiculo em ambos os grupos
dados_treinamento_sem_label <- dados_treinamento[-21]
#dados_treinamento_sem_label_normalizado <- normalizeData(dados_treinamento_sem_label, "norm")
dados_treinamento_sem_label_scaled = as.data.frame(lapply(dados_treinamento_sem_label, scale))
dados_teste_sem_label <- dados_teste[-21]
dados_teste_sem_label_scaled = as.data.frame(lapply(dados_teste_sem_label, scale))
# Guarda a classe que identifica cada veiculo em ambos os grupos
labels_dados_treinamento <- dados_treinamento$label
labels_dados_teste <- dados_teste$label
############################## PARAMETROS FUNCAO TRAIN ##############################
# Define funcao trainControl para validacao cruzada
library(caret)
library(nnet)
########## RNA ##########
ctrl <- trainControl(method = "repeatedcv", number = 10, savePredictions = TRUE)
set.seed(seed)
model_rna1 <- train(label~., data=dados_treinamento, method="nnet", metric=metric, preProcess = c("center","scale"),trControl=ctrl)
#set.seed(seed)
#model_rna2 <- train(label~., data=dados_treinamento, trControl=ctrl,metric=metric, method="mlp", preProcess = c("center","scale"))
#set.seed(seed)
model_rna3 <- train(label~., data=dados_treinamento, trControl=ctrl,metric=metric, method="mlpML", preProcess = c("center","scale"))
results <- resamples(list("RNA 1"=model_rna1, "RNA 2" = model_rna2, "RNA 3"=model_rna3))
############################## RANDOM FOREST ##############################
set.seed(seed)
model_rf1 <- train(label~., data=dados_treinamento, method="rf", metric=metric, trControl=ctrl)
set.seed(seed)
model_rf2 <- train(label~., data=dados_treinamento, method="cforest", metric=metric, trControl=ctrl)
set.seed(seed)
model_rf3 <- train(label~., data=dados_treinamento, method="parRF", metric=metric, trControl=ctrl)
getTree(randomForest(dados_treinamento, labels_dados_treinamento, ntree=10), 3, labelVar=TRUE)
results <- resamples(list("Random Forest 1"=model_rf1,"Random Forest 2"=model_rf2,"Random Forest 3"=model_rf3))
###### RF
model_rf3
model_rf3$results
# Compara????o
summary(results)
# Boxplots dos resultados
accuracy_training_models=bwplot(results,pch='|')
plot(accuracy_training_models[1])
predicao_rf3 <- predict(model_rf3, dados_teste_sem_label)
t<-table(predictions = predicao_rf3, actual = labels_dados_teste)
t
prop.table(table(predicao_rf3 == labels_dados_teste))
cfm_rf3 <- confusionMatrix(predicao_rf3, labels_dados_teste)
cfm_rf3
################################################################################################################################
# Compara????o
summary(results)
# Boxplots dos resultados
accuracy_training_models=bwplot(results,pch='|')
plot(accuracy_training_models[1])
########################## RESULTADOS ##########################
######## RNA
model_rna1
model_rna1$results
predicao_rna1 <- predict(model_rna1, dados_teste_sem_label)
t<-table(predictions = predicao_rna1, actual = labels_dados_teste)
t
prop.table(table(predicao_rna1 == labels_dados_teste))
############ Matriz de confus??o ####################
cfm_rna1 <- confusionMatrix(predicao_rna1, labels_dados_teste)