-
Notifications
You must be signed in to change notification settings - Fork 2
/
CART-SINIFLANDIRMA.R
202 lines (131 loc) · 3.83 KB
/
CART-SINIFLANDIRMA.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
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
#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)
library(ISLR)
df <- Carseats
str(df)
summary(df)
hist(df$Sales)
df$Sales <- factor(ifelse(df$Sales>7.5, "H", "L"))
summary(df$Sales)
set.seed(123)
train_indeks <- createDataPartition(df$Sales,
p = .8,
list = FALSE,
times = 1)
train <- df[train_indeks,]
test <- df[-train_indeks,]
train_x <- train %>% dplyr::select(-Sales)
train_y <- train$Sales
test_x <- test %>% dplyr::select(-Sales)
test_y <- test$Sales
#tek bir veri seti
training <- data.frame(train_x, Sales = train_y)
#model kurma
set_tree <- tree(Sales~., data = df)
summary(set_tree)
plot(set_tree)
text(set_tree)
tree_class <- rpart(Sales~., data = df, method = "class")
rpart.plot(tree_class)
summary(tree_class)
tree_class
plotcp(tree_class)
# en düşük hatayı veren cp parametresini modelin içinden alma
# biraz uzun ve karmaşık ama lazım.
min_cp <- tree_class$cptable[which.min(tree_class$cptable[,"xerror"]), "CP"]
min_cp
# min_cp ile karar ağacını budamak
tree_pruned <- prune(tree = tree_class, cp = min_cp)
rpart.plot(tree_pruned)
tree_class
tree_pruned
prp(tree_class, type = 1)
y_pred <- predict(tree_pruned, test_x, type="class")
#test için cm
confusionMatrix(test_y, y_pred, positive = "H")
pred_y_tr <- predict(tree_pruned, train_x, type="class")
confusionMatrix(train_y, pred_y_tr)
# model tuning işlemleri
# cv.tree ile model tune etme işlemi
seat_tree <- tree(Sales ~ . , data = train)
set.seed(12312153)
seat_tree_cv <- cv.tree(seat_tree, FUN = prune.misclass, K = 10)
min_tree <- which.min(seat_tree_cv$dev)
seat_tree_cv[min_tree]
par(mfrow = c(1,2))
plot(seat_tree_cv)
plot(seat_tree_cv$size,
seat_tree_cv$dev / nrow(train),
type = "b",
xlab = "Agac Boyutu/Dugum Sayisi", ylab = "CV Yanlis Siniflandirma Orani")
# caret ile model tune işlemi
set.seed(123)
ctrl <- trainControl(method="cv",
summaryFunction = twoClassSummary,
classProbs = TRUE)
cart_tune <- train(
x = train_x,
y = train_y,
method = "rpart",
tuneLength = 50,
metric = "ROC",
trControl = ctrl)
plot(cart_tune)
confusionMatrix(predict(cart_tune, test_x), test_y, positive = "H")
#RANDOM FOREST İLE SINIFLANDIRMA
rf_fit <- randomForest(Sales~., data = train, ntree=150, mtry=10)
pred_y <- predict(rf_fit, test_x)
test_y
pred_y
#test hatası
confusionMatrix(pred_y, test_y, positive = "H")
#train hatası
confusionMatrix(predict(rf_fit, train_x), train_y, positive = "H")
# model tuning
ctrl <- trainControl(method = "cv", number = 10, search='grid')
rf_grid <- expand.grid(data.frame(mtry=c(1:10)))
rf_tune <- train(Sales~., data=train, tuneGrid = rf_grid, trControl=ctrl,
method="rf", metric = 'Accuracy')
plot(rf_tune)
predict(rf_tune, test_x)
confusionMatrix(predict(rf_tune, test_x), test_y, positive = "H")