我有一个numpy数组,除了第一个轴外,我想对所有的轴进行讨论。我有(我认为)一个解决办法,但我不知道是否有更好的办法。
import numpy as np
def argmax(array):
## Argmax along all axes except the first (ie axis 0)
last_axis = len(array.shape) - 1
if last_axis == 0:
return tuple(range(array.size))
if last_axis == 1:
return (ran
假设我有下面的numpy数组。
Q = np.array([[0,1,1],[1,0,1],[0,2,0])
问题:如何识别每个最大值沿轴1的位置?因此,所需的输出应该如下所示:
array([[1,2],[0,2],[1]]) # The dtype of the output is not required to be a np array.
使用np.argmax,我可以识别轴上最大值的第一次出现,但不能识别后续的值。
In: np.argmax(Q, axis =1)
Out: array([1, 0, 1])
我还看到了依赖于使用像这样一个术语的np.argwhere的答
我有一个形状为(20,20,6)的numpy数组。
import numpy as np
import matplotlib.cm as cm
num_channels = 6
a = np.random.choice(range(100),(20,20,num_channels))
我想要得到一个形状为(20,20,4),20x20乘以RGBA颜色的数组,表明a中的哪个值是沿着最后一个轴的argmax。因此,我生成了6种RGBA颜色,并在a上使用了argmax。
color_list = cm.rainbow(np.linspace(0, 1, num_channels))
a = a.a
我正在编写代码,以优化依赖于可变数量的参数的数量。为了进行优化,我想同时跨多个轴应用索引选择函数,如numpy.argmax和numpy.argmin。下面是我现在使用的代码。是否有更内置或更有效的方法来跨任意数量的轴执行此任务,这些轴可能是连续的,也可能不是?
def nd_arg_axes(func, array, start):
"""Applies an index selecting function over trailing axes from start."""
n_trail = len(array.shape
我正在使用Eigen库进行矩阵/张量计算,其中我希望返回沿深度轴的最大值的索引。类似于numpy.argmax()在Python中所做的事情。 张量维数如下:(行= 200,列= 200,depth=4) #include <Eigen/Dense>
int main(){
Eigen::Tensor<double, 3> table(4,200,200);
table.setRandom();
// How can I do this task for axis = 2, i.e depth of a tensor?
// int max_a