forked from denniscwylie/maclearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnnGrid.R
58 lines (50 loc) · 1.46 KB
/
KnnGrid.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
library(caret)
library(class)
library(ggplot2)
source("modelpipe.R")
## source("LoadData.R")
## source("NormalizeData.R")
## source("RestrictData.R")
## source("ExtractYs.R")
load("prepared_datasets.RData")
## Note that caret has some nice built-in capabilities
## for tuning model parameters over a grid of potential values...
## Not using them here both for more explicit illustration
## and b/c I haven't yet made SolderedPipeFitter
## objects compatible with these capabilities.
fsKnnFitterGenerator = function(k) {
return(SolderedPipeFitter(
FastTSelector(nFeat = 10),
KnnFitter(k = k)
))
}
setnames = names(xnorms)
names(setnames) = setnames
ks = c(3, 5, 9, 15)
names(ks) = as.character(ks)
## lapply below takes a while to run!
knnModels = lapply(
X = setnames,
FUN = function(setname) {
return(lapply(
X = ks,
FUN = function(k) {
return(train(
fsKnnFitterGenerator(k),
xnorms[[setname]],
ys[[setname]],
trControl = trainControl(
method = "cv",
number = 5,
seeds = as.list(rep(123, 6))
)
))
}
))
}
)
## ## save(knnModels, file="knnGridModels.RData")
## load("knnGridModels.RData")
knnCvAccs = sapply(knnModels, function(kmods) {
sapply(kmods, function(kmod) {kmod$results$Accuracy})
})