Skip to content

Instantly share code, notes, and snippets.

View jakob-r's full-sized avatar

Jakob Richter jakob-r

View GitHub Profile
# Author: Jakob Richter (https://gist.github.com/jakob-r)
# colfuncMiddle does the same as colfunc but gives respect to middle point.
# usage: x : numeric()
# colfunc: function generated by colorRampPalette()
# zeropoint: numeric(1)
colfuncMiddle = function(x, colfunc, n, zeropoint=0){
stopifnot(is.numeric(x) | is.factor(x))
if(!missing(n)){
stopifnot(length(n)==1)
library(checkmate)
library(mlr)
makePersp3DLayer = function(opt.path.dt, par.set = NULL, learner, y = "y", x = NULL, resolution = 100, nrow.limit = 400) {
x = coalesce(x, getParamIds(par.set))
assertCharacter(x, len = 2)
data = opt.path.dt[, c(x,y), with=FALSE]
if (nrow(opt.path.dt) > nrow.limit) {
data = data[sample(nrow(data), size = nrow.limit),]
}
@jakob-r
jakob-r / waitAndSubmitExpired.R
Last active October 19, 2015 16:55
BatchJobs: Automaticly submit expired jobs
waitAndSubmitExpired = function(reg, resources, wait = 30, ...) {
done.ids = findDone(reg)
error.ids = findErrors(reg)
expired.ids = findExpired(reg)
submitted.ids = findSubmitted(reg)
restarted.ids = numeric()
while (!all(submitted.ids %in% c(done.ids, error.ids))) {
if (length(expired.ids) > 0)
submitJobs(reg, expired.ids, resources = resources, ...)
@jakob-r
jakob-r / surrogateMultiplexWrapper.R
Last active May 2, 2016 09:18
Transform x together with "category" in new x so that randomForest does not mix them up
surrogate.learner = makeLearner("regr.randomForest", predict.type = "se")
myTrafo = function(data, target, category, na.val) {
catf("run mytrafon on data with")
data.notarget = data[, colnames(data) != target, drop = FALSE]
data = cbind(dcast(data.notarget, as.formula(paste0("seq_along(", category, ")~",category)), fill = na.val, value.var = "x", drop = FALSE)[, -1, drop = FALSE], data[,colnames(data) %in% c(category, target), drop = FALSE])
return(data)
}
wrpTrain = function(data, target, args) {
data = myTrafo(data = data, target = target, category = args$category, na.val = args$na.val)
library(mlrMBO)
library(data.table)
library(ggplot2)
configureMlr(on.par.without.desc = "warn")
set.seed(1)
# Define objective function
fn = makeRosenbrockFunction(5)
# define mbo control object
ctrl = makeMBOControl(propose.points = 4, store.model.at = 0:20)
@jakob-r
jakob-r / runMBO.R
Last active March 22, 2017 10:17
optimizing a executable shell script with mlrMBO
library(mlrMBO)
library(stringi)
library(jsonlite)
# read command line args (in a not very safe way)
# Script can be called like that:
# Rscript runMBO.R iters=20 time=10 seed=1
args = commandArgs(TRUE)
# defaults:
iters = 50
@jakob-r
jakob-r / sample_weights.R
Last active January 26, 2018 15:58
How to sample weights uniformly
n = 5
x = runif(1000 * n)
mat = matrix(x, ncol = n)
mat2 = apply(mat, 1, function(x) {
ox = order(x)
rox = match(x, x[ox])
dx = diff(c(0,x[ox]))
dx[rox]
})
mat2 = t(mat2)
@jakob-r
jakob-r / keybase.md
Created May 8, 2018 07:52
keybase.md

Keybase proof

I hereby claim:

  • I am jakob-r on github.
  • I am jakobr (https://keybase.io/jakobr) on keybase.
  • I have a public key ASA0uw1eePgkoFeZK9YbYrkjVOld8XKdql-qIFLAALvwWwo

To claim this, I am signing this object:

@jakob-r
jakob-r / makeRandomParsEnsemble.R
Created November 13, 2019 10:28
Builds an ensemble of n-times the same learner but each with sampled hyperparam settings.
#' @title Build ensemble of multiple learners with sampled hyperparameters.
#'
#' @description
#' Define a learner and define which hyperparameters should get sampled.
#' The ensemble will be build of multiple learners, each with different random hyperparamters.
#' The predictions for mean and se will be averaged.
#'
#' @template arg_learner
#' @param samplers (`list`)\cr
#' A named list of functions that create the random samples:
@jakob-r
jakob-r / beamerscape_example.Rnw
Created November 19, 2019 14:45
Shows how to use svg with multiple layers as animation steps
\documentclass{beamer}
\usepackage[absolute,overlay]{textpos}
\usepackage{import}
\setbeamercolor{background canvas}{bg=gray}
\setkeys{Gin}{width=\linewidth} % default of includegraphics width
\begin{document}