替代样品

时间:2017-10-16 13:10:32

标签: r loops sapply

我有以下使用sample的{​​{1}}代码需要很长时间才能处理(因为执行了很多次):

sapply

问题在于我必须从矩阵中的权重中提取,这取决于samples = sapply(rowIndices, function(idx){ sample(vectorToDrawFrom, 1, TRUE, weights[idx, ]) }) 中的索引。

是否有人有更好的想法从矩阵的行中绘制?

可再现的例子:

rowIndices

1 个答案:

答案 0 :(得分:1)

所以这是一种加快代码速度的方法。有一点需要注意:采样值不会与rowIndices“匹配”,尽管以正确的顺序排列是很简单的。 2)您只存储最后一次迭代,但这可能只是因为这是一个最小的可重复示例......

基本上,您应该只需samplerowIndices一次rowIndices,因为rowIndices <- sort(rowIndices) ##sort the row indices and then loop for (i in 1:15){ samples = unlist(sapply(unique(rowIndices), function(idx){ sample(vectorToDrawFrom, sum(rowIndices %in% idx), TRUE, weights[idx, ]) })) } Unit: milliseconds expr min lq mean median uq max neval cld newForLoop 263.5668 266.6329 292.8301 268.8920 275.3378 515.899 100 a OriginalForLoop 698.2982 705.6911 792.2846 712.9985 887.9447 1263.779 100 b 的范围是1-99,这是99次调用而不是1000次,这提供了巨大的加速。

我们可以在开始之前对行索引进行排序

rowIndices

修改

维持原始向量排序的方法是保存索引或原始set.seed(8675309) weights = matrix(c(1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0), nrow = 5, ncol = 3, byrow = T) rowIndices = c(2,1,2,4) vectorToDrawFrom = runif(3, 0.0, 2.0) set.seed(8675309) ##This is the origal code sample2 = sapply(rowIndices, function(idx){ sample(vectorToDrawFrom, 1, TRUE, weights[idx, ]) }) rowIndx <- order(rowIndices) #get ordering index rowIndices <- sort(rowIndices) set.seed(8675309) samples = unlist(sapply(unique(rowIndices), function(idx){ sample(vectorToDrawFrom, sum(rowIndices %in% idx), TRUE, weights[idx, ]) })) samples = samples[order(rowIndx)] all(samples == sample2) #[1] TRUE 向量。然后对行索引进行排序并继续。

from asyncio import streams, transports, get_event_loop


class CustomTransport(transports.Transport):

    def __init__(self, loop, protocol, *args, **kwargs):
        self._loop = loop
        self._protocol = protocol

    def get_protocol(self):
        return self._protocol

    def set_protocol(self, protocol):
        return self._protocol

    # Implement read/write methods

    # [...]


async def custom_create_connection(protocol_factory, *args, **kwargs):
    loop = get_event_loop()
    protocol = protocol_factory()
    transport = CustomTransport(loop, protocol, *args, **kwargs)
    return transport, protocol


async def custom_open_connection(*args, **kwargs):
    reader = streams.StreamReader()
    protocol = streams.StreamReaderProtocol(reader)
    factory = lambda: protocol
    transport, _ = await custom_create_connection(factory, *args, **kwargs)
    writer = streams.StreamWriter(transport, protocol, reader)
    return reader, writer