-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.R
38 lines (32 loc) · 1.16 KB
/
utils.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
#### conect to SOAR package
fstoreconnect = function(subdir){
oldLC = Sys.getenv("R_LOCAL_CACHE", unset = ".R_Cache")
Sys.setenv(R_LOCAL_CACHe = subdir)
}
fstoreconnect("rstore")
tmp = Objects()
samp = fread(paste0(data.dir,"/sample_sub.csv"))
###
freq.encode = function(x ,xnew = x){
if(is.factor(x) || is.character(x)){
return(as.numeric(factor(xnew, levels = names(sort(table(x))))))
}else{
return(approxfun(density(x[!is.na(x)],n=length(x)/100))(xnew))
}
}
#### XGB F1 score
eval_F1Score = function(preds,dtrain){
actuals = getinfo(dtrain,"label")
score = ModelMetrics::f1Score(actual = actuals,
predicted = preds,cutoff = 0.5)
return(list(metric = "F1-Score", value = score))
}
#### my f2 cnt
my.f2cnt = function(th2,vn1,vn2, filter = TRUE){
data = data.frame(f1= th2[,vn1],f2=th2[,vn2], filter = filter)
colnames(data) = c("f1","f2","filter")
sum1 = sqldf::sqldf("select f1,f2, count(*) as cnt from data where filter=1 group by 1,2")
tmp = sqldf::sqldf("select b.cnt from data a left join sum1 b on a.f1=b.f1 and a.f2=b.f2")
tmp$cnt[is.na(tmp$cnt)] = 0
return(tmp$cnt)
}