Skip to content

Instantly share code, notes, and snippets.

View DexGroves's full-sized avatar

DG DexGroves

  • London
View GitHub Profile
# 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)
}
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])
}
@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
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)
}
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
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)