Skip to content

Instantly share code, notes, and snippets.

Avatar

hoxo_m hoxo-m

View GitHub Profile
@hoxo-m
hoxo-m / iter_index_matrix.R
Last active Aug 26, 2020
iterator for index matrix
View iter_index_matrix.R
iter_index_matrix <- function(dims, chunk_size = 1L) {
f_split <- cumprod(dims) <= chunk_size
has_next <- TRUE
if (all(f_split)) {
next_element <- function() {
if (!has_next) stop("StopIteration")
has_next <<- FALSE
expand.grid(lapply(dims, seq_len))
}
} else {
View furrr_test.R
library(purrr)
library(furrr)
plan(multiprocess)
x <- iris$Sepal.Length
n_cores <- parallel::detectCores() - 1
N <- length(x)
k <- ceiling(N / n_cores)
split_factor <- gl(n_cores, k) %>% head(N)
x_list <- split(x, split_factor)
View head.mcmc.R
head.mcmc <- function(x, n = 6L, ...) {
mcpar <- attr(x, "mcpar")
start <- mcpar[1]
max_end <- mcpar[2]
thin <- mcpar[3]
niter <- nrow(x)
end <- start + niter - 1
attr(x, "mcpar")[2:3] <- c(end, 1)
out <- window(x, end = min(start + n - 1, end))
@hoxo-m
hoxo-m / app.R
Created Dec 5, 2019
Sparkline in DT
View app.R
library(shiny)
library(DT)
library(sparkline)
library(tidyverse)
ui <- fluidPage(
titlePanel("Sparkline in DT"),
sidebarLayout(NULL,
mainPanel(
dataTableOutput("table_with_sparkline")
@hoxo-m
hoxo-m / qiita_get_organization_member.R
Created Nov 17, 2019
Qiita の Organization のメンバーIDを取得する関数
View qiita_get_organization_member.R
library(tidyverse)
library(rvest)
qiita_get_organization_member <- function(organization) {
target_url <- str_glue("https://qiita.com/organizations/{organization}/members")
sess <- html_session(target_url)
member_ids <- c()
while (!is.null(sess)) {
ids <- sess %>%
@hoxo-m
hoxo-m / svm_animation.R
Created Sep 13, 2019
機械学習やってるGIFつくるやつ
View svm_animation.R
library(dplyr)
library(ggplot2)
data <- iris %>% filter(Species != "setosa") %>%
select(Petal.Width, Sepal.Length, Species) %>%
sample_frac(1)
ggplot(data, aes(Petal.Width, Sepal.Length)) +
geom_point(aes(color = Species), size=4) + xlab("") + ylab("") +
scale_x_continuous(label=NULL) +
@hoxo-m
hoxo-m / AdaBoostM1_accuracy.R
Created Apr 4, 2019
AdaBoost は訓練データの正答率が100%になった後も学習を続けるとテストデータの正答率が上がる (Hastie et al. 2008)
View AdaBoostM1_accuracy.R
f <- function(X) {
X <- as.matrix(X)
limit <- qchisq(0.5, df = ncol(X))
apply(X, 1, function(row) {
if(sum(row^2) > limit) 1 else -1
})
}
D <- 2
M <- 2200
@hoxo-m
hoxo-m / AdaBoostM1.R
Created Mar 25, 2019
AdaBoost のアニメーション
View AdaBoostM1.R
f <- function(X) {
X <- as.matrix(X)
limit <- qchisq(0.5, df = ncol(X))
apply(X, 1, function(row) {
if(sum(row^2) > limit) 1 else -1
})
}
D <- 2
View gist:7cd5116a853259452a02594ca3aa9529
insert_data_frame <- function(df, df_added, col)
{
index <- which(col == names(df))
data.frame(append(df, list(df_added = df_added), index))[, -index]
}
View prophet_shf
prophet_shf <- function(model, periods, freq = "d", k = 3) {
N <- nrow(model$history)
if (periods %% 2 == 1) periods <- periods + 1
preserve <- (k + 1) * periods / 2
n_history <- N - preserve - 1
if (n_history < periods * 2) warning("History is too short.")
result <- data.frame()
while (n_history < N - periods) {
data_hist <- head(model$history, n_history)
m <- prophet(data_hist, growth = model$growth,
You can’t perform that action at this time.