- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑显示的数组和函数定义:
import numpy as np
a = np.array([[2, 2, 5, 6, 2, 5],
[1, 5, 8, 9, 9, 1],
[0, 4, 2, 3, 7, 9],
[1, 4, 1, 1, 5, 1],
[6, 5, 4, 3, 2, 1],
[3, 6, 3, 6, 3, 6],
[0, 2, 7, 6, 3, 4],
[3, 3, 7, 7, 3, 3]])
def grpCountSize(arr, grpCount, grpSize):
count = [np.unique(row, return_counts=True) for row in arr]
valid = [np.any(np.count_nonzero(row[1] == grpSize) == grpCount) for row in count]
return valid
该函数的要点是返回数组 a
的行,这些行恰好具有 grpCount
组元素,每个元素都完全相同 grpSize
元素。
例如:
# which rows have exactly 1 group that holds exactly 2 identical elements?
out = a[grpCountSize(a, 1, 2)]
正如预期的那样,代码输出 out = [[2, 2, 5, 6, 2, 5], [3, 3, 7, 7, 3, 3]]
。第一个输出行恰好有 1 组 2(即:5,5),而第二个输出行也恰好有 1 组 2(即:7,7)。
类似地:
# which rows have exactly 2 groups that each hold exactly 3 identical elements?
out = a[grpCountSize(a, 2, 3)]
这会产生 out = [[3, 6, 3, 6, 3, 6]]
,因为只有这一行恰好有 2 个组,每个组恰好包含 3 个元素(即:3,3, 3 和 6,6,6)
问题:我的实际数组只有 6 列,但它们可以有数百万行。该代码按预期完美运行,但对于长数组来说非常慢。有什么办法可以加快速度吗?
最佳答案
np.unique
对数组进行排序,这会降低您的目的效率。使用 np.bincount
这样您很可能会节省一些时间(取决于您的数组形状和数组中的值)。您也将不再需要 np.any
:
def grpCountSize(arr, grpCount, grpSize):
count = [np.bincount(row) for row in arr]
valid = [np.count_nonzero(row == grpSize) == grpCount for row in count]
return valid
另一种甚至可以节省更多时间的方法是对所有行使用相同数量的 bin 并创建一个数组:
def grpCountSize(arr, grpCount, grpSize):
m = arr.max()
count = np.stack([np.bincount(row, minlength=m+1) for row in arr])
return (count == grpSize).sum(1)==grpCount
另一个升级是使用vectorized 2D bin count from this post .例如(请注意,上面帖子中测试的 Numba 解决方案更快。例如,我只是提供了 numpy 解决方案。您可以将函数替换为上面链接的帖子中建议的任何函数):
def grpCountSize(arr, grpCount, grpSize):
count = bincount2D_vectorized(arr)
return (count == grpSize).sum(1)==grpCount
#from the post above
def bincount2D_vectorized(a):
N = a.max()+1
a_offs = a + np.arange(a.shape[0])[:,None]*N
return np.bincount(a_offs.ravel(), minlength=a.shape[0]*N).reshape(-1,N)
以上所有解的输出:
a[grpCountSize2(a, 1, 2)]
#array([[2, 2, 5, 6, 2, 5],
# [3, 3, 7, 7, 3, 3]])
关于python - 带条件的每行唯一值的二维矢量化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66037744/
我正在尝试将字符串列表转换为字符向量的向量: import collection.breakOut def stringsToCharVectors(xs: List[String]) = x
我正在尝试使用 Pytorch 通过 2D 向量(嘈杂语音帧序列)的回归来预测 1D 向量(干净语音数据帧) data) - 之前已经完成过。帧序列为帧提供时间上下文,以更准确地预测干净帧。这些向量可
在尝试构建时,我收到此错误: Operator '+=' is ambiguous on operands of type 'Vector3' and 'Vector2' 这是问题出处的脚本代码: u
是否存在实现 FIFO 意义上的循环数组或向量的 R 包? 假设我们有这个数组: 2 4 7 1 当在位置 1 插入一个新的观察值(比如 3)时,我希望第 n 个元素被第 n-1 个元素替换: 3 2
我在游戏中有两个对象,为此可以将其视为 2d 平面上的点,但我使用 Vector3s,因为游戏本身是 3d。 我有一个游戏相机,我想将其与两个物体垂直(也在平面上)对齐,以便它们都在相机的视野中。由于
我做了一个Telegram robot ,它的工作之一是从音频文件创建样本。现在对于发送给它的大多数音频,样本都非常好;像这样: 但是,对于一些音频,样本看起来有点奇怪: 如您所见,此文件中的波形未显
由于对 JavaScript 非常陌生,我在使用 JQuery VectorMaps 时遇到了以下问题: 当我用这种语法突出显示一个国家时,一切都很完美: jQuery('#vmap').vector
我正在使用 ChartJS 在我的网站中包含一些 map ,但 ChartJS 库没有我想要的 map 。 我想知道这种类型的矢量 map 是否很容易在网上免费找到,还是必须从头开始构建? Chart
我需要创建一个函数。在此范围内,我需要发生以下事情: List 1: '(a 5 6) List 2: '(c 8 10) List 3: '(d 4 9) 以上是列表。我需要忽略每个列表的第一列(这
我在地球表面有一个点,我正在将其从地球中心转换为向量。 我有一个以度数表示的真北航向,描述了该点将在地球表面行进的路径。 我需要计算一个向量,该向量垂直于该点沿地球表面的路径所创建的平面。 我尝试
大家好,这是我的 JavaScript 代码,用于为矢量 map 制作 ip 标记以显示在线 ip.. 所有 ips 都有 3 个不同的端口,例如:ip1:1020 或 ip2:5050 或 ip3:
我正在使用 Three.js 透视相机,我需要了解相机所注视的点。 如何使用相机的矩阵/旋转向量计算它? 最佳答案 相机向下看它的内部负 z 轴。所以选择相机负 z 轴上的任意点,如下所示: var
重要提示:请注意这个问题是关于 VECTOR map 的。不是高度图。 我正在尝试在 Scenekit 中实现 Vector 位移,如 apple 演示文稿中所述: https://www.youtu
我正在处理一个稳定增长的语料库。我使用用 Python 实现的 Doc2Vec 来训练我的文档向量。 是否可以更新文档向量? 我想使用文档向量进行文档推荐。 最佳答案 单个向量可以更新,但是 gens
我正在努力寻找一种比较(测量)两个不同信号之间相似性的好方法。我不想找出一个信号到另一个信号的时间延迟,但我想看看它们之间有何相似之处。例如,我有以下两个信号,比如说 s1 ans s2。这两个信号看
我想绘制 y 与 x 线,然后在它上面我想绘制向量。我可以使用 matplotlib 的 plot 和 quiver 函数来做到这一点。但是,矢量将始终绘制在线的后面,而不是在线的顶部。也就是说,线将
包含复数的向量 a 的大小为 N×1。任务是找到乘法a * a^HA (N-by-N) >,其中 H 是 Hermitian 算子(共轭转置),因此矩阵 A 是 Hermitian。 有没有比 O(N
三天来,我一直在努力从我的响应中获取复杂类型(列表),但总是收到 ClassCastException D/SOAPEnvelope(1552): Error: java.lang.ClassCast
在我的 android 项目中,我想要离线 map 。使用图 block ,我的 map 占用 500 MB 的空间,我还想在 map 上离线搜索地址。我认为减小尺寸并使搜索成为可能的唯一方法是矢量
什么是 Android Compose 方法来平铺图像以用小图案填充我的背景? 没有旋转的位图的天真方法可能是这样的: @Composable fun TileImage() { val pa
我是一名优秀的程序员,十分优秀!