-
Notifications
You must be signed in to change notification settings - Fork 0
/
iris kNN practise.R
99 lines (43 loc) · 1.72 KB
/
iris kNN practise.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
library(mlr)
library(tidyverse)
library(mclust)
data("iris")
newData <- as_tibble(iris)
ggplot(newData, aes(x = Sepal.Length, y = Sepal.Width,
col = Species, shape = Species)) +
geom_point() +
theme_bw()
ggplot(newData, aes(x = Petal.Length, y = Sepal.Length,
col = Species, shape = Species)) +
geom_point() +
theme_bw()
#creating a task:
newDataTask <- makeClassifTask(data = newData, target = "Species")
newDataTask
#Creating a learner:
kNN <- makeLearner("classif.knn", par.vals = list("k" = 2))
#Creating holdout CV:
holdoutNew <- makeResampleDesc(method = "Holdout", split = 2/3,
stratify = TRUE)
holdoutNewCV <- resample(learner = kNN, task = newDataTask,
resampling = holdoutNew,
measures = list(mmce, acc))
calculateConfusionMatrix(holdoutNewCV$pred, relative = TRUE)
#Tuning k for kNN:
ParamSpacekNN <- makeParamSet(makeDiscreteParam("k", values = 1:10))
gridSearch <- makeTuneControlGrid()
cvForTuning <- makeResampleDesc(method = "Holdout", split = 2/3,
stratify = TRUE)
tunedK <- tuneParams(learner = "classif.knn", task = newDataTask,
resampling = cvForTuning, par.set = ParamSpacekNN,
control = gridSearch)
tunedK
tunedK$x
knnTuningData <- generateHyperParsEffectData(tunedK)
plotHyperParsEffect(knnTuningData, x = "k", y = "mmce.test.mean",
plot.type = "line") +
theme_bw()
###Training the final model, using the tuned k value:
tunedKnn <- setHyperPars(makeLearner("classif.knn"),
par.vals = tunedK$x)
tunedKnnModel <- train(tunedKnn, newDataTask)