Skip to content

Instantly share code, notes, and snippets.

View DexGroves's full-sized avatar

DG DexGroves

  • London
View GitHub Profile
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
N = 10_000
np.random.seed(42)
df = pd.DataFrame(index=np.arange(N))
df['x2'] = np.random.choice([1,2,3], size=N)
import numpy as np
from collections import deque
def dist_to(game_map, sources):
dist = np.empty_like(game_map.owners)
dist.fill(-1)
for sx, sy in sources:
dist[sx, sy] = 0
M1 <- matrix(seq(16), nrow = 4, ncol = 4)
M2 <- matrix(seq(16), nrow = 4, ncol = 4)
M1 * M2
M1 %*% M2
`*` <- function(lhs, rhs) {
if (is.matrix(lhs) & is.matrix(rhs)) {
return(lhs %*% rhs)
}
@DexGroves
DexGroves / predict_rf_subset.R
Last active December 27, 2016 12:42
Predict with a subset of RF trees
library("randomForest")
data(mtcars)
rf <- randomForest(mpg ~ ., data = mtcars, ntree = 10)
preds <- predict(rf, newdata = mtcars, predict.all = TRUE)
# Aggregate predictions
preds$aggregate
# Invididual tree predictions
preds$individual
for (i in 2:ncol(my_data)) {
varname <- colnames(my_data)[i]
# if you want to change varname somehow
# varname <- paste0(varname, "new")
my_data[[varname]] <- my_data[,i]/sd(my_data[,i])
}
# Make a 3D transition matrix T where z-dimension is time
T_states <- 3
T_terms <- 4
T <- array(runif(T_states * T_states * Tz)/1.5, dim = c(T_states, T_states, Tz))
# Normalise by rows
for (z in seq(Tz)) {
T[, , z] <- T[, , z] / apply(T[, , z], 1, sum)
}
@DexGroves
DexGroves / lupa.R
Created August 19, 2016 17:11
Lowest unique common integer code
generate_fks <- function(N) {
fki <- log(N + 1)
fks <- c(fki)
for (i in seq(N)) {
fki <- log(exp(fki) - fki)
fks <- c(fks, fki)
}
fks
# Instantiate local CRAN
localCRAN <- path.expand("QRAN")
dir.create(localCRAN)
contribDir <- file.path(localCRAN, "src", "contrib")
dir.create(contribDir, recursive = TRUE)
rVersion <- paste(unlist(getRversion())[1:2], collapse = ".")
binPaths <- list(
@DexGroves
DexGroves / spiralmxnet.R
Created April 15, 2016 16:28
Fit mxnet on a spiral
library("mlbench")
library("ggplot2")
library("mxnet")
plot_mxmodel <- function(model, data) {
x <- seq(from = min(data), to = max(data), length.out = 500)
d2 <- as.matrix(expand.grid(x, x))
mx_pred <- predict(model, d2)
@DexGroves
DexGroves / install_mxnet.sh
Last active March 18, 2017 11:01
install mxnet
sudo apt-get update
sudo apt-get install -y build-essential git libatlas-base-dev libopencv-dev
git clone --recursive https://github.com/dmlc/mxnet
cd mxnet;
cp make/config.mk .
#add CUDA options
echo "USE_CUDA=1" >>config.mk