Size: a a a

R language and Statistical data analysis

2020 March 02

АК

Артём Клевцов in R language and Statistical data analysis
Всё, понял.
источник

АК

Артём Клевцов in R language and Statistical data analysis
Только вот с сортировкой вопрос остаётся открытым. То есть по условию задачи не понятно как разраешается ситуация мультимодальности.
источник

АК

Артём Клевцов in R language and Statistical data analysis
Вторая итерация Rcpp. Без сортировки. Результат отличается, но также выдаёт строку с частотй 5:
// [[Rcpp::plugins(cpp11)]]

#include <Rcpp.h>

using namespace Rcpp;

template <typename T>
class hasher {
public:
 std::size_t operator()(const T& vec) const {
   size_t seed = vec.size();
   for(auto& i : vec) {
     seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
   }
   return seed;
 }
};

// [[Rcpp::export]]
size_t count_rows(IntegerMatrix x) {
 size_t nrows = x.rows();
 hasher<IntegerMatrix::Row> hash_fn;
 std::unordered_map<size_t,size_t> hash_rows;
 std::unordered_map<size_t,size_t> hash_counts;
 using pair_type = decltype(hash_counts)::value_type;

 for (size_t i = 0; i < nrows; ++i) {
   IntegerMatrix::Row ri = x.row(i);
   size_t h = hash_fn(ri);
   hash_counts[h]++;
   hash_rows[h] = i;
 }

 auto comp = [](const pair_type& p1, const pair_type& p2) {
   return p1.second < p2.second;
 };
 auto hidx = std::max_element(hash_counts.begin(), hash_counts.end(), comp);
 size_t midx = hash_rows[hidx->first];
 return midx;
}

/*** R
set.seed(123)
N = 10000
x <- matrix(nrow = N, ncol = 3, data = sample(0:31, N*3, replace = TRUE))
x[count_rows(x),]
*/
источник

АК

Артём Клевцов in R language and Statistical data analysis
Григорий Демин
Хммм, если в функции f_tabulate вместо множителя 100 поставить 32, то будет почти в 10 раз быстрее data.table:
f_tabulate = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1]
   res = tabulate(splitter)
   res = which(res == max(res))[1]
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
# Unit: microseconds
# expr       min        lq       mean     median         uq       max neval
# dt            3098.166  3185.073  3728.0666  3334.8835  4014.0755 12412.863   100
# base     57688.870 58858.048 60201.7815 59508.4385 60817.6605 70619.858   100
# tabulate   314.518   352.260   409.4361   359.3785   364.3445  2914.091   100
Этот вариант быстрее моих раз в 8.
источник

АК

Артём Клевцов in R language and Statistical data analysis
which(res == max(res))[1]

Можно тут заменить на which.max. Он должен быстрее работать.
Unit: microseconds
          expr     min      lq     mean   median      uq      max neval cld
f_tabulate1(x) 291.056 311.396 497.9079 327.5375 516.369 4679.221   100   a
f_tabulate2(x) 228.249 241.458 364.9199 268.5990 376.807 6679.179   100   a
источник

АК

Артём Клевцов in R language and Statistical data analysis
Ещё чуть ускорил и сократил:
f_tabulate = function(mx) {
 mult = 32
 splitter = mx[,3] * mult * mult + mx[,2] * mult + mx[,1]
 res = tabulate(splitter)
 idx = which.max(splitter == which.max(res))
 mx[idx, ]
}
источник

ГД

Григорий Демин in R language and Statistical data analysis
Артём Клевцов
Этот вариант быстрее моих раз в 8.
Можно вместо хэша попробовать имитировать tabulate. То есть делаем splitter=splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1], создаем массив counter = 1:max(spliter) потом в цикле бежим по сплиттеру и counter[spliiter[i]] = counter[spliiter[i]] + 1
источник

АК

Артём Клевцов in R language and Statistical data analysis
Григорий Демин
Можно вместо хэша попробовать имитировать tabulate. То есть делаем splitter=splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1], создаем массив counter = 1:max(spliter) потом в цикле бежим по сплиттеру и counter[spliiter[i]] = counter[spliiter[i]] + 1
Хорошая идея.
источник

ГД

Григорий Демин in R language and Statistical data analysis
Артём Клевцов
Хорошая идея.
Сделал такой цикл на R. Забавно, что хотя tabulate он не обходит, но вполне себе рвет data.table:
set.seed(123)
N = 10000
x <- matrix(nrow = N, ncol = 3, data = sample(0:31, N*3, replace = TRUE))
mx <- x
library(data.table)
f_dt <- function(mx) {
   mx <- as.data.frame(mx)
   setDT(mx)
   mx <- mx[, .N, by = list(V1, V2, V3)][order(-N)][1]
   mx[, cbind(V1, V2, V3)]
}
f_dt(x)

f_dt2 <- function(mx) {
   mx <- as.data.table(mx)
   mx[, .N, by = list(V1, V2, V3)][which.max(N)[1]][, cbind(V1, V2, V3)]

}
f_dt2(x)

f_base <- function(mx) {
   mx <- paste(mx[, 1], mx[, 2], mx[, 3])
   mx <- sort(table(mx), decreasing = TRUE)
   mx <- names(mx[1])
   mx <- as.numeric(strsplit(mx, '\\s')[[1]])
   matrix(nrow = 1, ncol = 3, data = mx)
}
f_base(x)

f_tabulate = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1]
   res = tabulate(splitter)
   res = which(res == max(res))[1]
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
f_tabulate(x)


f_tabulate2 = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1]
   res = tabulate(splitter)
   res = which.max(res)
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
f_tabulate2(x)

f_low_level = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1] + 1
   res = rep(0, max(splitter))
   for(i in seq_along(splitter)){
       res[splitter[i]] = res[splitter[i]] + 1
   }
   res = which(res == max(res))[1]
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
f_low_level(x)

library(microbenchmark)
microbenchmark(
   dt = f_dt(x),
   dt2 = f_dt2(x),
   base = f_base(x),
   tabulate = f_tabulate(x),
   tabulate2 = f_tabulate2(x),
   loop = f_low_level(x)
)
# Unit: microseconds
# expr          min         lq       mean    median        uq       max neval
# dt         3128.625  3250.1285  3912.6456  3821.227  4258.572  7981.140   100
# dt2        2601.559  2705.8470  3335.4357  3053.803  3683.170  7413.352   100
# base      59269.405 59960.8480 61325.7161 60555.287 61727.445 73447.540   100
# tabulate    325.112   355.5705   366.4962   360.868   367.655   519.451   100
# tabulate2   254.925   287.8670   371.1212   294.157   299.454  7335.219   100
# loop       1472.606  1560.1740  1660.2241  1575.569  1597.088  4391.001   100
источник
2020 March 03

PU

Philipp Upravitelev in R language and Statistical data analysis
Григорий Демин
Сделал такой цикл на R. Забавно, что хотя tabulate он не обходит, но вполне себе рвет data.table:
set.seed(123)
N = 10000
x <- matrix(nrow = N, ncol = 3, data = sample(0:31, N*3, replace = TRUE))
mx <- x
library(data.table)
f_dt <- function(mx) {
   mx <- as.data.frame(mx)
   setDT(mx)
   mx <- mx[, .N, by = list(V1, V2, V3)][order(-N)][1]
   mx[, cbind(V1, V2, V3)]
}
f_dt(x)

f_dt2 <- function(mx) {
   mx <- as.data.table(mx)
   mx[, .N, by = list(V1, V2, V3)][which.max(N)[1]][, cbind(V1, V2, V3)]

}
f_dt2(x)

f_base <- function(mx) {
   mx <- paste(mx[, 1], mx[, 2], mx[, 3])
   mx <- sort(table(mx), decreasing = TRUE)
   mx <- names(mx[1])
   mx <- as.numeric(strsplit(mx, '\\s')[[1]])
   matrix(nrow = 1, ncol = 3, data = mx)
}
f_base(x)

f_tabulate = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1]
   res = tabulate(splitter)
   res = which(res == max(res))[1]
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
f_tabulate(x)


f_tabulate2 = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1]
   res = tabulate(splitter)
   res = which.max(res)
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
f_tabulate2(x)

f_low_level = function(mx){
   mult = 32
   splitter = mx[,3]*mult*mult + mx[,2]*mult + mx[,1] + 1
   res = rep(0, max(splitter))
   for(i in seq_along(splitter)){
       res[splitter[i]] = res[splitter[i]] + 1
   }
   res = which(res == max(res))[1]
   mx[splitter==res, ,drop = FALSE][1, ,drop = FALSE]
}
f_low_level(x)

library(microbenchmark)
microbenchmark(
   dt = f_dt(x),
   dt2 = f_dt2(x),
   base = f_base(x),
   tabulate = f_tabulate(x),
   tabulate2 = f_tabulate2(x),
   loop = f_low_level(x)
)
# Unit: microseconds
# expr          min         lq       mean    median        uq       max neval
# dt         3128.625  3250.1285  3912.6456  3821.227  4258.572  7981.140   100
# dt2        2601.559  2705.8470  3335.4357  3053.803  3683.170  7413.352   100
# base      59269.405 59960.8480 61325.7161 60555.287 61727.445 73447.540   100
# tabulate    325.112   355.5705   366.4962   360.868   367.655   519.451   100
# tabulate2   254.925   287.8670   371.1212   294.157   299.454  7335.219   100
# loop       1472.606  1560.1740  1660.2241  1575.569  1597.088  4391.001   100
очень круто, спасибо %)
правда, очень уж нетривиальный ход с mx[,3]*mult*mult + mx[,2]*mult + mx[,1]
я не могу навскидку понять, насколько оно устойчиво и нет ли каких-нибудь ситуаций, когда ломаться будет :(
источник

ГД

Григорий Демин in R language and Statistical data analysis
При NA точно будет ломаться. Ну если за пределы диапазона 0:31 значения выйдут. В остальных случаях должно быть все хорошо. Подсмотрено в tapply
источник

PU

Philipp Upravitelev in R language and Statistical data analysis
ну, NA в цветах быть не должно, по идее

любопытно, спасибо
я как-то сто лет tapply не трогал
источник

ВК

Владимир Калинин in R language and Statistical data analysis
tapply = aggregate
источник
2020 March 04

Е

Евгений in R language and Statistical data analysis
Всем привет, подскажите при авторизации в Google Analytics API не создается файл ".httr-oauth"
источник

AS

Alexey Seleznev in R language and Statistical data analysis
Евгений
Всем привет, подскажите при авторизации в Google Analytics API не создается файл ".httr-oauth"
а какой пакет используете?
источник

Е

Евгений in R language and Statistical data analysis
library(googleAuthR)
источник

Е

Евгений in R language and Statistical data analysis
для данных library(googleAnalyticsR)
источник

AS

Alexey Seleznev in R language and Statistical data analysis
Евгений
library(googleAuthR)
googleAuthR это устаревший пакет, сейчас там через gargle автроизация идёт, и он уже в другой директории и другие файлы кеширует

https://netpeak.net/ru/blog/kak-zagruzit-dannyye-iz-api-google-analytics-v-r-chast-2/
источник

Е

Евгений in R language and Statistical data analysis
спасибо, работало до сегодня, поменял аккаунт и перестал сохранять
источник

AS

Alexey Seleznev in R language and Statistical data analysis
Евгений
спасибо, работало до сегодня, поменял аккаунт и перестал сохранять
там в статье описаны разные способы авторизации
источник