- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个整数列表 (di
) 和另一个由 numpy
整数子数组组成的列表 (rang_indx
) (代码如下)。对于每个子数组,我需要在单独的列表 (indx
) 中存储一些由 di
列表给出的随机元素。
就我所见np.random.shuffle()
不会对子数组中的元素进行洗牌,而是对 rang_indx
中的子数组本身进行洗牌,这不是我所需要的。因此,我需要使用 for
循环首先对子数组进行洗牌(就地),然后使用另一个循环(与 zip()
组合)来生成indx
列表。
作为较大代码的一部分,该函数被调用数百万次。有什么办法可以加快这个过程吗?
import numpy as np
def func(di, rang_indx):
# Shuffle each sub-array in place.
for _ in rang_indx:
np.random.shuffle(_)
# For each shuffled sub-array, only keep as many elements as those
# indicated by the 'di' array.
indx = [_[:i] for (_, i) in zip(*[rang_indx, di.astype(int)])]
return indx
# This data is not fixed, and will change with each call to func()
di = np.array([ 4., 2., 0., 600., 12., 22., 13., 21., 25., 25., 12., 11.,
7., 12., 10., 13., 5., 10.])
rang_indx = [np.array([]), np.array([189, 195, 209, 214, 236, 237, 255, 286, 290, 296, 301, 304, 321,
323, 327, 329]), np.array([164, 171, 207, 217, 225, 240, 250, 263, 272, 279, 284, 285, 289]), np.array([101, 162, 168, 177, 179, 185, 258, 261, 264, 269, 270, 278, 281,
287, 293, 298]), np.array([111, 127, 143, 156, 159, 161, 181, 182, 183, 194, 196, 198, 204,
205, 210, 212, 235, 239, 267, 268, 297]), np.array([107, 116, 120, 128, 130, 136, 137, 144, 152, 155, 157, 166, 169,
170, 184, 186, 192, 218, 220, 226, 228, 241, 245, 246, 247, 251,
252, 253]), np.array([ 99, 114, 118, 121, 131, 134, 158, 216, 219, 221, 224, 231, 233,
234, 243, 244]), np.array([ 34, 37, 38, 48, 56, 78, 84, 100, 108, 117, 122, 123, 132,
149, 151, 153, 163, 178, 180, 191, 199, 202, 208, 211]), np.array([ 31, 40, 41, 45, 51, 53, 57, 60, 61, 66, 67, 69, 71,
75, 85, 90, 95, 96, 167, 173, 174, 176, 188, 190, 197, 206]), np.array([ 0, 1, 2, 3, 6, 11, 12, 13, 17, 25, 33, 36, 47,
58, 64, 76, 87, 94, 160, 165, 172, 175, 187, 193, 201, 203]), np.array([ 4, 16, 18, 19, 109, 113, 115, 124, 138, 142, 145, 150]), np.array([103, 105, 106, 112, 125, 135, 139, 140, 141, 146, 147, 154]), np.array([102, 104, 110, 119, 126, 129, 133, 148]), np.array([29, 32, 42, 43, 55, 63, 72, 77, 79, 83, 91, 92]), np.array([35, 49, 59, 73, 74, 81, 86, 88, 89, 97, 98]), np.array([30, 39, 44, 46, 50, 52, 54, 62, 65, 68, 80, 82, 93]), np.array([ 8, 10, 15, 27, 70]), np.array([ 5, 7, 9, 14, 20, 21, 22, 23, 24, 26, 28])]
func(di, rang_indx)
最佳答案
方法#1:这是一个想法,目的是在我们循环并仅使用一个循环时保持最少的工作量 -
[0,1)
内创建一个2D
随机数组以覆盖最大值。子数组的长度。1.0
。获取每一行的 argsort。那些无效位置对应的1将保留在后面,因为原始随机数组中没有1。这样,我们就有了索引数组。di
中列出的长度范围内。rang_indx
中的每个子数组进行切片。因此,实现 -
lens = np.array([len(i) for i in rang_indx])
di0 = np.minimum(lens, di.astype(int))
invalid_mask = lens[:,None] <= np.arange(lens.max())
rand_nums = np.random.rand(len(lens), lens.max())
rand_nums[invalid_mask] = 1
shuffled_indx = np.argpartition(rand_nums, lens-1, axis=1)
out = []
for i,all_idx in enumerate(shuffled_indx):
if lens[i]==0:
out.append(np.array([]))
else:
slice_idx = all_idx[:di0[i]]
out.append(rang_indx[i][slice_idx])
方法#2:在循环内以有效方式完成大部分设置工作的另一种方法 -
lens = np.array([len(i) for i in rang_indx])
di0 = np.minimum(lens, di.astype(int))
out = []
for i in range(len(lens)):
if lens[i]==0:
out.append(np.array([]))
else:
k = di0[i]
slice_idx = np.argpartition(np.random.rand(lens[i]), k-1)[:k]
out.append(rang_indx[i][slice_idx])
关于python - 加速子数组的洗牌和存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078995/
我想在我的 iPhone 应用程序中加入线性回归。经过一些搜索,我发现 Accelerate Framework 中的 LAPACK 和 BLAS 是正确的库。但是我很难将加速框架添加到我的 XCod
有什么方法可以加速 JS 脚本(我指的是一些复杂的 DOM 操作,比如游戏或动画)? 最佳答案 真的没有办法真正加快速度。您可以压缩它,但不会快很多。 关于Javascript 加速?,我们在Stac
有时,我必须为一个项目重新导入数据,从而将大约 360 万行读入 MySQL 表(目前是 InnoDB,但我实际上并不局限于这个引擎)。 “加载数据文件...”已被证明是最快的解决方案,但它有一个权衡
在尝试计算加速时,我被卡住了。所以给出的问题是: 问题 1 如果程序的 50% 增强了 2 倍,其余 50% 增强了 4 倍,那么由于增强而导致的整体加速是多少? Hints:考虑增强前(未增强)机器
目前我正在处理实时绘图,但可视化非常慢。我想知道你可以做些什么来加速 Matplotlib 中的事情: 后端如何影响性能?是否有后端 实时绘图比其他人更好吗? 我可以降低分辨率以提高 FPS 吗? 如
我有一个小型测试框架。它执行一个循环,执行以下操作: 生成一个小的 Haskell 源文件。 使用 runhaskell 执行此操作.该程序生成各种磁盘文件。 处理刚刚生成的磁盘文件。 这种情况发生了
这是我的网站:Instant-YouTube 如您所见,加载需要很长时间。在 IE8 及以下甚至有时会导致浏览器崩溃。我不确定是什么原因造成的。可能是 Clicksor 广告,但我认为是 swfobj
是否可以加速 SKSpriteNode? 我知道可以使用 node.physicsBody.velocity 轻松设置速度但是设置它的加速度有多难? 最佳答案 从牛顿第二定律倒推运动:F = m.a您
有没有人有加速 FCKEditor 的技术?是否有一些关键的 JavaScript 文件可以缩小或删除? 最佳答案 在最新版本 (3.0.1) 中,FCKEditor 已重命名为 CKEditor .
我有以下 MySQL 查询,需要一天多的时间才能执行: SELECT SN,NUMBER FROM a WHERE SN IN (SELECT LOWER_SN FROM b WHER
我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点: 事实上,我的图像是移动的,它的中心是这样定义的: imageView.c
我有一个 mysql 表,其中存储有 4 列的成员消息: message_id(主键,自增) sender_id( key ) receiver_id( key ) 消息内容 我做了很多 SELECT
我在 cuda_computation.cu 中有以下代码 #include #include #include #include void checkCUDAError(const char
我正在使用 BeautifulSoup 在 for 循环中解析数千个网站。这是我的代码片段: def parse_decision(link): t1 = time.time() de
我正在使用 OpenCV 2.4 (C++) 在灰度图像上进行寻线。这涉及一些基本的图像处理步骤,如模糊、阈值、Canny 边缘检测器、梯度滤波器或霍夫变换。我必须在数千张图像上应用寻线算法。 考虑到
当我试图连续生成四次相同的报告时,我刚刚分析了我的报告应用程序。第一个用了 1859 毫秒,而后面的只用了 400 到 600 毫秒。对此的解释是什么?我能以某种方式使用它来使我的应用程序更快吗?报告
当我打开 Storyboard文件时,由于其中包含的 VC 数量,打开它需要 1-2 分钟。加快速度的最佳做法是什么?我们应该将一些 VC 移动到不同的 Storyboard文件中吗?我们是否应该使用
我有一个包含多个页面的 UIPageViewController。每个页面都是相同的 View Controller ,但会跟踪页码并显示 PDF 的正确页面。问题是每个 PDF 页面都需要在 cur
这实际上是两个问题,但它们非常相似,为了简单起见,我想将它们放在一起: 首先:给定一个已建立的 Java 项目,除了简单的代码内优化之外,还有哪些不错的方法可以加快它的速度? 其次:在用Java从头写
我有一个包含 1000 个条目的文档,其格式类似于:
我是一名优秀的程序员,十分优秀!