numpy的:一个阵列的增量元素给予递增所需的索引 [英] Numpy: increment elements of an array given the indices required to increment

查看:299
本文介绍了numpy的:一个阵列的增量元素给予递增所需的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把第二阶张成二进制三阶张量。给定一个二阶张量amxn numpy的数组:A,我需要把各要素值:X,A和用矢量替换为:V,其尺寸等于A的最大值,但增加值1以v的对应于x值的指数(即v [X] = 1)。在矩阵,其中涉及增量给定指数:我一直在关注这个问题,在生产由2维坐标给定指数与增量的数组。我一直在阅读的答案,尝试使用np.ravel_multi_index()和np.bincount()做相同,但与3维坐标,但是我一直得到一个ValueError错误:在坐标数组无效条目。这是我一直使用的是什么:

I am trying to turn a second order tensor into a binary third order tensor. Given a second order tensor as a m x n numpy array: A, I need to take each element value: x, in A and replace it with a vector: v, with dimensions equal to the maximum value of A, but with a value of 1 incremented at the index of v corresponding to the value x (i.e. v[x] = 1). I have been following this question: Increment given indices in a matrix, which addresses producing an array with increments at indices given by 2 dimensional coordinates. I have been reading the answers and trying to use np.ravel_multi_index() and np.bincount() to do the same but with 3 dimensional coordinates, however I keep on getting a ValueError: "invalid entry in coordinates array". This is what I have been using:

def expand_to_tensor_3(array):
    (x, y) = array.shape
    (a, b) = np.indices((x, y))
    a = a.reshape(x*y)
    b = b.reshape(x*y)
    tensor_3 = np.bincount(np.ravel_multi_index((a, b, array.reshape(x*y)), (x, y, np.amax(array))))
    return tensor_3

如果你知道这里有什么问题或知道一个更好的方法来完成我的目标,无论是真正有用的,谢谢。

If you know what is wrong here or know an even better method to accomplish my goal, both would be really helpful, thanks.

推荐答案

您可以使用(A [:,:,np.newaxis] == np.arange(A.max()+ 1 ))。astype(INT)

下面是一个演示:

In [52]: A
Out[52]: 
array([[2, 0, 0, 2],
       [3, 1, 2, 3],
       [3, 2, 1, 0]])

In [53]: B = (A[:,:,np.newaxis] == np.arange(A.max()+1)).astype(int)

In [54]: B
Out[54]: 
array([[[0, 0, 1, 0],
        [1, 0, 0, 0],
        [1, 0, 0, 0],
        [0, 0, 1, 0]],

       [[0, 0, 0, 1],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]],

       [[0, 0, 0, 1],
        [0, 0, 1, 0],
        [0, 1, 0, 0],
        [1, 0, 0, 0]]])

检查几个单个元素 A

In [55]: A[0,0]
Out[55]: 2

In [56]: B[0,0,:]
Out[56]: array([0, 0, 1, 0])

In [57]: A[1,3]
Out[57]: 3

In [58]: B[1,3,:]
Out[58]: array([0, 0, 0, 1])

这位前pression A [:,:,np.newaxis] == np.arange(A.max()+ 1)使用的广播来的每个元素比较 A np.arange(A.max()+ 1)。对于单个值,这看起来像:

The expression A[:,:,np.newaxis] == np.arange(A.max()+1) uses broadcasting to compare each element of A to np.arange(A.max()+1). For a single value, this looks like:

In [63]: 3 == np.arange(A.max()+1)
Out[63]: array([False, False, False,  True], dtype=bool)

In [64]: (3 == np.arange(A.max()+1)).astype(int)
Out[64]: array([0, 0, 0, 1])

A [:,:,np.newaxis] A 与形状的三维视图(3,4,1)。的额外的维度被添加,使得比较 np.arange(A.max()+ 1)将广播到每个元素,从而具有形状的结果(3,4,A.max()+ 1)

A[:,:,np.newaxis] is a three-dimensional view of A with shape (3,4,1). The extra dimension is added so that the comparison to np.arange(A.max()+1) will broadcast to each element, giving a result with shape (3, 4, A.max()+1).

通过一个微不足道的变化,这将对于n维阵列工作。索引的省略号 ... A numpy的数组是指所有其他方面。因此,

With a trivial change, this will work for an n-dimensional array. Indexing a numpy array with the ellipsis ... means "all the other dimensions". So

(A[..., np.newaxis] == np.arange(A.max()+1)).astype(int)

转换n维数组的(N + 1)维数组,其中最后一维是整数的 A 二元指标。这里有一个一维数组的例子:

converts an n-dimensional array to an (n+1)-dimensional array, where the last dimension is the binary indicator of the integer in A. Here's an example with a one-dimensional array:

In [6]: a = np.array([3, 4, 0, 1])

In [7]: (a[...,np.newaxis] == np.arange(a.max()+1)).astype(int)
Out[7]: 
array([[0, 0, 0, 1, 0],
       [0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0]])

这篇关于numpy的:一个阵列的增量元素给予递增所需的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆