- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将索引数组划分为随机大小的 block (取自有限的可能大小范围),这些 block 也在彼此之间进行洗牌。我尝试了以下发现 here但它专注于同等大小的 block 。
a = np.arange(1, 100)
def chunk(xs, n): # to chunk the array xs in n parts
ys = list(xs)
random.shuffle(ys)
size = len(ys) // n
leftovers= ys[size*n:]
for c, xtra in enumerate(leftovers):
yield ys[c*size:(c+1)*size] + [ xtra ]
for c in xrange(c+1,n):
yield ys[c*size:(c+1)*size]
换句话说,我如何更改上述函数以具有一定数量的 block (随机数并在彼此之间进行洗牌),并具有从范围内随机获取的可变大小,例如
[5-10]
最佳答案
这会起作用:
from itertools import chain
import numpy as np
a = np.arange(1, 100)
def chunk(xs, nlow, nhigh, shuffle=True):
xs = np.asarray(xs)
if shuffle:
# shuffle, if you want
xs = xs.copy()
np.random.shuffle(xs)
# get at least enough random chunk sizes in the specified range, ie nlow <= n <= nhigh
ns = np.random.randint(nlow, nhigh+1, size=xs.size//nlow)
# add up the chunk sizes to get the indices at which we'll slice up the input array
ixs = np.add.accumulate(ns)
# truncate ixs so that its contents are all valid indices with respect to xs
ixs = ixs[:np.searchsorted(ixs, xs.size)]
# yield slices from the input array
for start,end in zip(chain([None], ixs), chain(ixs, [None])):
yield xs[start:end]
list(chunk(a, 5, 10))
输出:
[array([67, 79, 17, 62, 12, 37, 70, 24]),
array([98, 48, 88, 59, 47]),
array([52, 60, 89, 23, 43, 44]),
array([ 7, 27, 33, 74, 49, 2]),
array([ 6, 51, 40, 13, 56, 45]),
array([31, 3, 55, 10, 11, 46, 9, 42, 34]),
array([53, 22, 95, 41, 19, 32, 4, 69, 86]),
array([93, 68, 57, 65, 92, 76, 28, 63, 64, 58]),
array([91, 66, 18, 99, 21]),
array([36, 83, 15, 78, 1, 81, 97, 84]),
array([61, 71, 25, 94, 87, 20, 85, 38]),
array([ 8, 96, 75, 30, 77, 14, 72, 29]),
array([35, 90, 82, 73, 39, 5, 26, 50, 16]),
array([80, 54])]
我原来的答案没有对最终 block 的大小设置下限,所以有时它会小于指定的大小(尽管永远不会更大)。据我所知,没有直接的方法来处理这个问题。但是,一般来说,您可以通过拒绝来自该区域的任何样本来从随机分布中删除不需要的区域。换句话说,您可以通过丢弃任何建议的 block 来确保最后一个 block 足够大:
def getIxs(xsize, nlow, nhigh):
# get at least enough random chunk sizes in the specified range, ie nlow <= n <= nhigh
ns = np.random.randint(nlow, nhigh+1, size=xsize//nlow)
# add up the chunk sizes to get the indices at which we'll slice up the input array
ixs = np.add.accumulate(ns)
# truncate ixs so that its contents are all valid indices with respect to xs
ixs = ixs[:np.searchsorted(ixs, xsize)]
return ixs
def chunk(xs, nlow, nhigh):
xs = np.asarray(xs)
ixs = getIxs(xs.size, nlow, nhigh)
# rerun getIxs until the size of the final chunk is large enough
while (xs.size - ixs[-1]) < nlow:
ixs = getIxs(xs.size, nlow, nhigh)
# yield slices from the input array
for start,end in zip(chain([None], ixs), chain(ixs, [None])):
yield xs[start:end]
这种方法应该保留每个 block 大小的整体随机性。
关于python - python中numpy数组的随机大小分块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49943442/
我正在使用 WCF 并希望将大文件从客户端上传到服务器。我已经调查并决定遵循 http://msdn.microsoft.com/en-us/library/aa717050.aspx 中概述的分块方
我试图了解有关 Transfer-Encoding:chunked 的更多信息。引用了一些文章: http://zoompf.com/blog/2012/05/too-chunky和 "Transfe
我们正在评估 HDF5 在分块数据集方面的性能。 特别是我们试图弄清楚是否可以跨不同的连续块进行读取以及这样做会如何影响性能? 例如。我们有一个块大小为 10 的数据集,一个有 100 个值的数据集,
使用 Eloquent,如何根据 chunk 中的条件终止分块函数的关闭?我试过返回,但这似乎只终止当前块而不是所有块。此时,我想停止从数据库中检索记录。 $query->chunk(self::CH
有没有办法在不删除所选文件的情况下重新启动 plupload 上传? plupload.stop() 停止上传,但如果我使用 start() 再次启动上传,它会从上次停止的地方继续。相反,我希望它再次
我有带有“id,名称”的文件1和带有“id,地址”的文件2。我无法加载第一个文件(小于 2Gb):它在 76k 行(带有 block 连接)和只有 2 列后崩溃...我也无法在第二个文件上 read_
我正在尝试从头开始设计一个系统,我想在其中通过 servlet 加载文本行。生产线的生产需要一些时间。因此,我希望能够在它们到达时在我的浏览器中逐步显示它们,一次显示几个。我想从 javascript
能否请您提供一个示例,说明如何在 Android 中读取来自 Web 服务的分块响应 谢谢 编辑:我尝试调用一个 soap 网络服务,它用代表图像的 base64 编码字符串回复我 代码如下: Str
我想制作一个无限平铺 map ,从(-max_int,-max_int)到(max_int,max_int),所以我要制作一个基本结构: chunk,每个 chunk 包含 char tiles[w]
这是一个典型的场景:评估一个页面,并且有一个缓冲区 - 一旦缓冲区已满,评估的页面部分就会发送到浏览器。这使用 HTTP 1.1 分块编码。 但是,其中一个 block 中可能会发生错误(在第一个 b
如何从给定模式的句子中获取所有 block 。例子 NP:{} 标记的句子: [("money", "NN"), ("market", "NN") ("fund", "NN")] 如果我解析我得到 (
我正在使用以下代码将 CSV 文件拆分为多个 block (来自 here) def worker(chunk): print len(chunk) def keyfunc(row):
我想我已经很接近这个了,我有以下 dropzone 配置: Dropzone.options.myDZ = { chunking: true, chunkSize: 500000, ret
因为我在更常规的基础上使用 WebSocket 连接,所以我对事情在幕后的工作方式很感兴趣。因此,我研究了无休止的规范文档一段时间,但到目前为止,我真的找不到任何关于对传输流本身进行分 block 。
我有一个 slice ,其中包含约 210 万个日志字符串,我想创建一个 slice ,字符串尽可能均匀分布。 这是我目前所拥有的: // logs is a slice with ~2.1 mill
问题: 我有一个大约为 [350000, 1] 的向量,我希望计算成对距离。这导致 [350000, 350000] 整数数据类型的矩阵不适合 RAM。我最终想得到一个 bool 值(适合 RAM),
我想将 JSONP 用于具有 x 域脚本编写的项目,但不太关心 IE 中的 2048 个字符限制。 如果字符大小超过 2048,JSONP 是否自动支持“分块”?如果是的话,有人可以分享一些例子吗?
我目前正在开发 2d 角色扮演游戏,例如《最终幻想 1-4》。基本上,我的平铺 map 可以加载, Sprite 可以在 map 上自由行走。 如何处理与平铺 map 的碰撞? 我创建了三个独立的图
Treetagger 可以进行词性标记和文本分块,这意味着提取口头和名词性从句,如这个德语示例所示: $ echo 'Das ist ein Test.' | cmd/tagger-chunker-g
我应该从服务器流式传输端点,该端点返回带有传输编码的 json:分块。 我有以下代码,但无法读取响应。我尝试了 responseBody.streamBytes() 并将输入流转换为字符串,但我不能在
我是一名优秀的程序员,十分优秀!