This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
OlderNewer