Skip to content

Instantly share code, notes, and snippets.

View mbq's full-sized avatar

Miron Kursa mbq

View GitHub Profile
library(ggplot2)
readRDS('ans-ps.RDS')->ss
do.call(rbind,lapply(
split(ss,paste(ss$met,ss$k)),
function(S){
median(S$time[S$p==1])->bl
S$speedup<-bl/S$time
S
})
setNames(
do.call(expand.grid,rep(list(c(T,F)),5)),
c("A","B","N1","N2","N3")
)->X
cbind(X,AoB=with(X,A|B),AnB=with(X,A&B),nA=!X$A)->X
factor(with(X,A!=B))->Y
data.frame(lapply(X,factor))->X
greedySVM<-function(X,Y,sel=names(X),err=Inf){
#Empty selection is not interesting
@mbq
mbq / prf.R
Created December 29, 2016 14:46
Precision, recall and F-score one-liners
precision<-function(y,p) sum(y&p)/sum(p)
recall<-function(y,p) sum(y&p)/sum(y)
Fscore<-function(y,p) 2/(1+sum(y|p)/sum(y&p))
precisionS<-function(Y,P) mean(P%in%Y)
recallS<-function(Y,P) mean(Y%in%P)
FscoreS<-function(Y,P) mean(c(Y,P)%in%intersect(P,Y))
#AUROC calculation code
auroc<-function(score,cls){
n1<-sum(!cls); sum(cls)->n2;
U<-sum(rank(score)[!cls])-n1*(n1+1)/2;
return(1-U/n1/n2);
}
#... as a cryptic one-liner
auroc1l<-function(score,cls)
mean(rank(score)[cls]-1:sum(cls))/sum(!cls)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbq
mbq / genToken.R
Last active February 9, 2016 12:02
Random ipv6 token generator in R
cat(sprintf("::%s\n",paste(replicate(4,paste(sample(z,4,rep=TRUE),collapse="")),collapse=":")))
@mbq
mbq / smooth9.R
Created September 23, 2015 19:57
Fast 9-point mean matrix smoothing in R
s9<-function(M,iters=1){
M*0+9->n;
n[,1]<-n[1,]<-n[,ncol(n)]<-n[nrow(n),]<-6;
n[1,1]<-n[1,ncol(n)]<-n[nrow(n),1]<-n[nrow(n),ncol(n)]<-4;
for(e in 1:iters){
s<-M+
cbind(M[,-1],0)+
cbind(0,M[,-ncol(M)])+
rbind(M[-1,],0)+
rbind(0,M[-nrow(M),])+