Skip to content

Instantly share code, notes, and snippets.

View hoxo-m's full-sized avatar
🌅
旅人算

hoxo_m hoxo-m

🌅
旅人算
View GitHub Profile
@hoxo-m
hoxo-m / iter_index_matrix.R
Last active August 26, 2020 05:35
iterator for index matrix
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 {
@hoxo-m
hoxo-m / furrr_test.R
Last active May 8, 2020 09:58
furrr test
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)
@hoxo-m
hoxo-m / head.mcmc.R
Created December 18, 2019 09:31
head.mcmc
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 December 5, 2019 22:44
Sparkline in DT
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 November 17, 2019 14:32
Qiita の Organization のメンバーIDを取得する関数
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 September 13, 2019 13:03
機械学習やってるGIFつくるやつ
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 April 4, 2019 15:41
AdaBoost は訓練データの正答率が100%になった後も学習を続けるとテストデータの正答率が上がる (Hastie et al. 2008)
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 March 25, 2019 09:57
AdaBoost のアニメーション
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
insert_data_frame <- function(df, df_added, col)
{
index <- which(col == names(df))
data.frame(append(df, list(df_added = df_added), index))[, -index]
}
@hoxo-m
hoxo-m / prophet_shf
Created July 24, 2017 05:45
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,