- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 np.ndarray 如下:
[[ inf 1. 3. 2. 1.]
[ inf inf 2. 3. 2.]
[ inf inf inf 5. 4.]
[ inf inf inf inf 1.]
[ inf inf inf inf inf]]
有没有办法获取那个nd数组中m个最小项的索引和值?所以,如果我想要 4 个最小的,它将是
[(0,1,1),(0,4,1),(3,4,1),(0,3,2)]
其中 (row,col,val) 是上面的符号。
如果有多个值,则随机选择其中一个。例如,有 3 个,然后下一个最小的是值 2,但 (0,3,2)、(1,2,2)、(1,4,2) 都是可能的选择。
本质上,我能否有效地从上三角矩阵中提取该格式的 k 个最小值(该矩阵比上面的示例大得多)。我尝试使用正方形 nsmallest 将其展平,但无法使索引和值对齐。谢谢!
最佳答案
对于 Inf
填充数组 -
r,c = np.unravel_index(a.ravel().argsort()[:4], a.shape)
out = zip(r,c,a[r,c])
为了提高性能,请考虑使用 np.argpartition
。因此,将 a.ravel().argsort()[:4]
替换为 np.argpartition(a.ravel(), range(4))[:4]
.
sample 运行-
In [285]: a
Out[285]:
array([[ inf, 1., 3., 2., 1.],
[ inf, inf, 2., 3., 2.],
[ inf, inf, inf, 5., 4.],
[ inf, inf, inf, inf, 1.],
[ inf, inf, inf, inf, inf]])
In [286]: out
Out[286]: [(0, 1, 1.0), (0, 4, 1.0), (3, 4, 1.0), (0, 3, 2.0)]
对于一般情况 -
R,C = np.triu_indices(a.shape[1],1)
idx = a[R,C].argsort()[:4]
r,c = R[idx], C[idx]
out = zip(r,c,a[r,c])
sample 运行-
In [351]: a
Out[351]:
array([[ 68., 67., 81., 23., 16.],
[ 84., 83., 20., 66., 48.],
[ 58., 72., 98., 63., 30.],
[ 61., 40., 1., 86., 22.],
[ 29., 95., 38., 22., 95.]])
In [352]: out
Out[352]: [(0, 4, 16.0), (1, 2, 20.0), (3, 4, 22.0), (0, 3, 23.0)]
为了提高性能,请考虑使用 np.argpartition
。因此,将 a[R,C].argsort()[:4]
替换为 np.argpartition(a[R,C], range(4))[:4]
.
关于python - m 上三角矩阵中的最小值,其索引为元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970426/
在过去的几个月里,我一直在研究 Haskell,我遇到了一个我不太确定如何处理的单子(monad)的情况。 我有一个 a -> m a 类型的值第二个类型为 m (a -> a)我需要对它们进行组合,
仿函数有 (a -> b) -> m a -> m b 应用程序有 f (a -> b) -> f a -> f b Monad 有 m a -> (a -> m b) -> m b 但是,是否有扩展
我是 Haskell 的新手,我想知道是否有比 Hoogle 更好的方法来确定一个库功能是否重复? 举个例子:我有很多函数f :: Monad a => a -> m a我想链接在一起,比如 f123
将存储在一系列列表中的 m、m、n 维数组组合成一个 m、m、n 维数组的方法是什么? 示例: 这是三个包含 m,m,n 维数组的列表: list1 <- array (1, dim = c(5, 5
有没有办法写一个函数f::(a -> b -> ... -> t) -> (Monad m => m a -> m b -> ... -> m t ),基本上是 liftMn 对于任何 n? (编辑:
我有一个像这样的 pandas 数据框: df = pd.DataFrame({'A':[1,3,2,9],'B':[2,1,2,7],'C':[7,2,4,6],'D':[8,1,6,4]},ind
这个问题来自文章“Trivial Monad”,地址:http://blog.sigfpe.com/2007/04/trivial-monad.html 。提供的答案是 h x y = x >>= (
所以>>= :: m a -> (a -> m b) -> m b和>> :: m a -> m b -> m b . 而 f b -> f a . 但我想要一些能m a -> (a -> m b)
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 3 年前。 Improve
当我安装 rakudo来源: $ git clone git@github.com:rakudo/rakudo.git $ cd rakudo $ perl Configure.pl --gen-mo
我正在尝试通过查看一些练习来提高我的 Idris 技能 Software Foundations (最初是为 Coq 设计的,但我希望对 Idris 的翻译不会太糟糕)。我在使用 "Exercise:
我想知道以下是否可行。 与服务器交换密码时,应保护密码。因此,用户可以使用生成的 key kUser 来加密密码。 Encrypt(m, kUser) 生成加密消息 eU(m)。现在用户将此信息发送到
这两个表之间存在什么样的关系(1:1、1:m、m:m,等等)? CREATE TABLE IF NOT EXISTS `my_product` ( `id` int(11) NOT NULL au
有人可以解释类型的含义以及如何实现吗? class Foldable f where foldMap :: (Monoid m) => (a -> m) -> f a -> m 基于 https:
例如,在 MVC 应用程序中,我可以使用 Html 助手来创建这样的标签: @Html.LabelFor(m => m.ProductName) 我没有在任何地方声明变量“m”,但 IDE 会自动找出
更新:澄清、更明确的重点和缩短的示例: 我可以避免 M op+(M&&,M&&) 过载吗?假设,我想很好地处理 RValues?我想其他三个重载是必需的。 我首先使用 (&&,&&) 重载的原因: 通
假设我有一个函数,它接受两个向量并返回一个整数,例如一个向量中也存在另一个向量中的元素数量。喜欢: f m [,1] [,2] [,3] [1,] "c" "i" "c" [2,] "
我想将字符串(字幕)转换为: 585 00:59:59,237 --> 01:00:01,105 - It's all right. - He saw us! 586 01:00:01,139 -->
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
是否可以将 Linux 中的大文件将 d.m.Y h:m:s 转换为 Y-d-m h:m:s? 示例数据 "30.07.2016 00:00:00",DN123,PAPN,PAPN,TEST,9189
我是一名优秀的程序员,十分优秀!