-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathlib_ga.R
More file actions
82 lines (57 loc) · 2.06 KB
/
lib_ga.R
File metadata and controls
82 lines (57 loc) · 2.06 KB
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
custom_fitness <- function(vars, data_x, data_y, p_sampling)
{
# speeding up things with sampling
ix=get_sample(data_x, percentage_tr_rows = p_sampling)
data_2=data_x[ix,]
data_y_smp=data_y[ix]
# keep only vars from current solution
names=colnames(data_2)
names_2=names[vars==1]
# get the columns of the current solution
data_sol=data_2[, names_2]
# get the roc value from the created model
roc_value=get_roc_metric(data_sol, data_y_smp, names_2)
# get the total number of vars for the current selection
q_vars=sum(vars)
# time for your magic
fitness_value=roc_value/q_vars
return(fitness_value)
}
get_roc_metric <- function(data_tr_sample, target, best_vars)
{
# data_tr_sample=data_sol
# target = target_var_s
# best_vars=names_2
fitControl <- trainControl(method = "cv",
number = 3,
summaryFunction = twoClassSummary,
classProbs = TRUE)
data_model=select(data_tr_sample, one_of(best_vars))
mtry = sqrt(ncol(data_model))
tunegrid = expand.grid(.mtry=round(mtry))
fit_model_1 = train(x=data_model,
y= target,
method = "rf",
trControl = fitControl,
metric = "ROC",
tuneGrid=tunegrid
)
metric=fit_model_1$results["ROC"][1,1]
return(metric)
}
get_accuracy_metric <- function(data_tr_sample, target, best_vars)
{
data_model=select(data_tr_sample, one_of(best_vars))
fitControl <- trainControl(method = "cv",
number = 3,
summaryFunction = twoClassSummary)
data_model=select(data_tr_sample, one_of(best_vars))
mtry = sqrt(ncol(data_model))
tunegrid = expand.grid(mtry=round(mtry))
fit_model_1 = train(x=data_model,
y= target,
method = "rf",
tuneGrid = tunegrid)
metric=fit_model_1$results["Accuracy"][1,1]
return(metric)
}