-
Notifications
You must be signed in to change notification settings - Fork 2
/
YSA-Classification.R
140 lines (110 loc) · 3.26 KB
/
YSA-Classification.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
#install.packages("ISLR")
library(ISLR)
#install.packages("tidyverse")
library(tidyverse)
#install.packages("funModeling")
library(funModeling)
#install.packages("caret")
library(caret)
#install.packages("pROC")
library(pROC)
#install.packages("class")
library(class)#knn icin
#install.packages("e1071")
library(e1071)#knn icin
#install.packages("kernlab")
library(kernlab) #svm icin
#install.packages("ROCR")
library(ROCR) #roc icin
#install.packages("neuralnet")
library(neuralnet)
#install.packages("GGally")
library(GGally)
#install.packages("nnet")
library(nnet)
#install.packages("rpart")
library(rpart)
#install.packages("cli")
library(cli)
#install.packages("tree")
library(tree)
#install.packages("rpart.plot")
library(rpart.plot)
#install.packages("randomForest")
library(randomForest)
#install.packages("gbm")
library(gbm)
#install.packages("xgboost")
library(xgboost)
#install.packages("DiagrammeR")
library(DiagrammeR)
#install.packages("mlbench")
library(mlbench)
#KULLANACAĞIMIZ VERİSETİ
# MEME KANSERİ AMLEİYATINDAN SONRA HAYATTA KALMA DURUMUNU İNCELİYORUZ.
# DOLAYISIYLA BİR SINIFLANDRIMA PROBLEMİNE ODAKLANACAĞIZ.
url <- "http://archive.ics.uci.edu/ml/machine-learning-databases//haberman/haberman.data"
df <- read_csv(file = url,
col_names = c("Age", "Operation_Year", "Number_Pos_Nodes", "Survival"))
#VERİSETİNİN DÜZENLENMESİ
summary(df)
df$Survival <- ifelse(df$Survival == 1, 0, 1)
df$Survival <- factor(df$Survival)
summary(df)
ggpairs(df[,1:3])
ggpairs(df)
freq(df)
#SCALE FONKSİYONU
scale01 <- function(x) {
(x-min(x)) / (max(x)-min(x))
}
df <- df %>% mutate(Age = scale01(Age),
Number_Pos_Nodes = scale01(Number_Pos_Nodes),
Operation_Year = scale01(Operation_Year))
# TRAİN TEST AYRIMA İŞLEMLERİ
train_indeks <- createDataPartition(df$Survival, p = 0.8, list = F, times = 1)
train <- df[train_indeks,]
test <- df[-train_indeks,]
train_x <- train %>% select(-Survival)
train_y <- train$Survival
test_x <- test %>% select(-Survival)
test_y <- test$Survival
levels(train$Survival) <- make.names(levels(as.factor(train$Survival)))
train_y <- train$Survival
test_y <- test$Survival
# NEURAL NETWORK KURULMASI VE EĞİTİLMESİ
set.seed(800)
nn_fit <- nnet(Survival~., data = train, size=12, decay=0.0001)
summary(nn_fit)
# TAHMİN
predict(nn_fit, test_x)
pred_y <- predict(nn_fit, test_x, type="class")
pred_y <- ifelse(pred_y=="X0", 0, 1)
pred_y
test_y
# TAHMİN SONUÇLARININ DEĞERLENDİRİLMESİ
defaultSummary(data.frame(pred=pred_y,
obs=test_y))
#YSA MODEL TUNE
ctrl <- trainControl(method = "cv", number = 10, classProbs = TRUE)
ysa_grid <- expand.grid(size=c(2:12), decay=seq(0.0001, 0.1, length=15))
maxSize <- max(nnetGrid$size)
numWts <- 1*(maxSize * (length(ysa_train_x) + 1) + maxSize + 1)
ysa_tune <- train(
train_x, train_y,
method = "nnet",
metric = "ROC",
tuneGrid = ysa_grid,
trace = FALSE,
maxit = 2000,
MaxNWts = numWts,
trControl = ctrl
)
plot(ysa_tune)
#YSA TUNE EDİLMİŞ MODEL İLE TAHMİN VE DEĞERLENDİRME
pred_y <- predict(ysa_tune, test_x)
pred_y <- ifelse(pred_y=="X0", 0, 1)
pred_y <- factor(pred_y)
defaultSummary(data.frame(pred=factor(pred_y),
obs=test_y))
confusionMatrix(test_y, pred_y, positive = "1")