考虑以下从类中缓存和删除属性的系统:
class cached_property(object):
"""
Descriptor (non-data) for building an attribute on-demand on first use.
"""
def __init__(self, factory):
"""
<factory> is called such: factory(instance) to build the attribute.
"""
self._attr_name = factory.__name__
self._factory = factory
def __get__(self, instance, owner):
# Build the attribute.
attr = self._factory(instance)
# Cache the value; hide ourselves.
setattr(instance, self._attr_name, attr)
return attr
class test():
def __init__(self):
self.kikou = 10
@cached_property
def caching(self):
print("computed once!")
return True
def clear_cache(self):
try: del self.caching
except: pass
b = …
Run Code Online (Sandbox Code Playgroud) 有没有一种配置方法furrr::future_map
可以允许嵌套用例?考虑以下代码:
library(furrr)
library(tictoc)
# The problem is easier to reason about if you take N
# smaller than your number of cores, and M big.
N = 2
M = 100
plan(sequential)
tic()
x = future_map(1:N, function(i){
furrr::future_map(1:M,function(j){
Sys.sleep(1/M)
return(1)
})
})
toc() # 2sec + overhead
plan(multiprocess)
tic()
x = future_map(1:N, function(i){
furrr::future_map(1:M,function(j){
Sys.sleep(1/M)
return(1)
})
})
toc() # one sec + overhead !!
Run Code Online (Sandbox Code Playgroud)
第一个应该需要 2 秒多一点的时间。还行吧。但是,即使在千核机器上,有没有办法让第二个运行时间少于 1 秒?
我的用例如下:一些子任务比其他子任务需要更长的时间才能完成,当一些子任务完成时,一些核心可以自由地进一步分派更长的任务。
但furrr默认情况下不会这样做,并且长期运行的任务最终只在一个核心上。该问题与上面代码中显示的问题相同:是否有一种方法可以在某些核心空闲的情况下让furrr重新分派内部任务?
这是不可能做到的,还是我错过了furrr/future 调用的参数?
在我的包上,R CMD CHECK 返回一条关于 DECRIPTION 中可能拼写错误的单词的注释:
\n\n* checking CRAN incoming feasibility ... NOTE \n> Possibly mis-spelled words in DESCRIPTION: \n> Laverny (10:602) \n> Rulli\xc3\xa8re (10:641)\n
Run Code Online (Sandbox Code Playgroud)\n\n这两个词是我引用的一篇论文中的作者姓名,描述字段如下:
\n\nDescription: Provides S4 classes and methods to fit several copula models: The classic empirical checkerboard copula and the empirical checkerboard copula with known margins, see Cuberos, Masiello and Maume-Deschamps (2019) <doi:10.1080/03610926.2019.1586936> are proposed. These two models allow to fit copulas in high dimension with a small number of observations, and they …
Run Code Online (Sandbox Code Playgroud) 我实际上遇到了 T 形管的问题。我正在尝试在同一链中做 3 件事:
所以我正在尝试以下语法:
my_variable <- data %>%
glm(Responce ~ Variables, family) %T>%
summary
Run Code Online (Sandbox Code Playgroud)
哪些不能按预期工作。glm get 已安装,但摘要不会显示出来。所以我不得不把它分成 2 个链:
my_variable <- data %>%
glm(Responce ~ Variables, family)
my_variable %>% summary
Run Code Online (Sandbox Code Playgroud)
所以我在想:要么我没有得到 T 管的功能,要么它没有正确编码并且与摘要功能混淆。
因为如果我尝试:
my_variable <- data %>%
glm(Responce ~ Variables, family) %T>%
plot
Run Code Online (Sandbox Code Playgroud)
它运作良好。
一些想法?
TL; DR:我想通过左边的给定字符将列表的每个字符串填充到给定的大小.我想快点.请参阅下面的代码并举例说明
我有很大的字符串向量,包含......很好的东西,但最大(已知)字符数.我希望通过将Zero's留给给定大小来完成thoose字符串(优于char的最大数量)
假设:
c("yop",NA,"1234567","19","12AN","PLOP","5689777")
Run Code Online (Sandbox Code Playgroud)
假设客观大小为10,我希望:
[1] "0000000yop" NA "0001234567" "0000000019" "00000012AN" "000000PLOP" "0005689777"
Run Code Online (Sandbox Code Playgroud)
结果,尽可能快.
我试着写自己的,但它不是很快......你能帮助我加快速度吗?我有数十亿的待办事项.
这是我的实际代码:
library(purrr)
zero_left <- function(field,nb){
map2_chr(
map(abs(nb-nchar(field)),~ rep("0",.x)),
field,
~ paste0(c(.x,.y),collapse=""))
}
trial <- c("yop","1234567","19","12AN","PLOP","5689777")
zero_left(trial,10)
Run Code Online (Sandbox Code Playgroud)
这段代码甚至不处理NA案例...但没有它它可行,但太慢了.
我有一段代码搜索矩阵的哪些行boxes
等于给定的向量x
.这段代码使用了这个apply
功能,我想知道它是否可以更优化?
x = floor(runif(4)*10)/10
boxes = as.matrix(do.call(expand.grid, lapply(1:4, function(x) {
seq(0, 1 - 1/10, length = 10)
})))
# can the following line be more optimised ? :
result <- which(sapply(1:nrow(boxes),function(i){all(boxes[i,] == x)}))
Run Code Online (Sandbox Code Playgroud)
我自己没有设法摆脱这个apply
功能,但也许你会有比我更好的想法:)
我正在尝试使用 Rcpp 将 R 函数转换为 C++,但遇到了一些我不太理解的错误。
下面的代码给出了我的 R 函数、我(糟糕的)翻译它的尝试以及最后的一些使用示例(测试这两个函数返回相同的东西......)
我的 R 代码功能:
intersect_rectangles <- function(x_min, x_max, y_min, y_max) {
rez <- list()
rez$min <- pmax(x_min, y_min)
rez$max <- pmin(x_max, y_max)
if (any(rez$min > rez$max)) {
return(list(NULL))
}
return(rez)
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用Rcpp创建相同的函数。
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List Cpp_intersect_rectangles(NumericVector x_min,NumericVector
x_max,NumericVector y_min,NumericVector y_max) {
// Create a list :
NumericVector min = pmax(x_min,y_min);
NumericVector max = pmin(x_max,y_max);
List L = List::create(R_NilValue);
if (! any(min > max)) {
L = …
Run Code Online (Sandbox Code Playgroud)