简体   繁体   English

如何创建一个沿着特定(第三)轴的3D(shape = m,n,o)数组,索引由2D数组(shape = m,n)给出?

[英]How to create a 3D (shape=m,n,o) array with ones along a specific (third) axis, the indices are given by a 2D array (shape=m,n)?

Let us say I have a 2D array with a shape (2, 2), containing indices 假设我有一个形状为(2,2)的2D数组,其中包含索引

x = np.array([[2, 0], [3, 1]])

What I would like to do is to create a 3D array with a shape (2, 2, 4), which has values 1 along the third axis, and their position is given by x , therefore: 我想做的是创建一个形状为(2,2,4)的3D数组,该数组沿第三轴的值为1,其位置由x给出,因此:

y = np.zeros(shape=(2,2,4))
myfunc(array=y, indices=x, axis=2)

array([[[0, 0, 1, 0],
        [1, 0, 0, 0]],
       [[0, 0, 0, 1],
        [0, 1, 0, 0]]])

So far I have not found any indexing method for this. 到目前为止,我还没有找到任何索引方法。 A for loop might be able to do this, but I am sure there is a faster, vectorized method. for循环也许可以做到这一点,但是我确信有一个更快的矢量化方法。

What you are looking for is called advanced indexing . 您正在寻找的被称为高级索引 To index with integer arrays properly, you need to have a set of arrays that broadcast to the right shape. 为了正确地对整数数组建立索引,您需要具有一组广播为正确形状的数组。 Since x is already lined up with two of the dimensions, you only need to make 2D arrays with the indices along each axis. 由于x已经与其中的两个维度对齐,因此只需要制作2D数组,并在每个轴上都带有索引。 np.ogrid helps with that since it creates minimal range arrays that broadcast to the correct shape: np.ogrid可以帮助解决这个问题,因为它创建了最小范围的数组,可以广播为正确的形状:

a, b = np.ogrid[:2, :2]
y[a, b, x] = 1

The results of ogrid are equivalent to ogrid的结果等于

a = np.arange(2).reshape(-1, 1)
b = np.arange(2).reshape(1, -1)

Or 要么

a = np.arange(2)[:, None]
b = np.arange(2)[None, :]

You can also write a one-liner: 您还可以编写单行代码:

y[(*tuple(slice(None, n) for n in x.shape), x)] = 1

With for loops: 使用for循环:

y = np.zeros(shape=(2,2,4))
for i in range(2):
    for j in range(2):
        y[i,j,x[i,j]] = 1

With advanced indexing: 使用高级索引:

y = np.zeros(shape=(2,2,4))
i, j = np.meshgrid(np.arange(2), np.arange(2))
y[i,j,x[i,j]] = 1

Output (in both cases): 输出(在两种情况下):

array([[[0., 0., 1., 0.],
        [1., 0., 0., 0.]],
       [[0., 0., 0., 1.],
        [0., 1., 0., 0.]]])

As for which will be actually faster, you need to try on your exact case... 至于实际上会更快,您需要尝试一下具体情况...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将具有形状(M,N,P)阵列的numpy int数组有效转换为具有(N,P)形状的2D对象数组 - Efficient converting of numpy int array with shape (M, N, P) array to 2D object array with (N, P) shape numpy:如何将(N,)维向量插入(N,M,D)维数组作为沿D轴的新元素? (N,M,D + 1) - Numpy: how to insert an (N,) dimensional vector to an (N, M, D) dimensional array as new elements along the D axis? (N, M, D+1) 如何将形状 n 的数组转换为 n,m - How to covert array of shape n, to n,m 如何沿着nD数组的轴选择具有该轴的(n-1)D索引数组的值? - How can I select values along an axis of an nD array with an (n-1)D array of indices of that axis? 将沿 ND 数组的特定轴的选定索引/行的 arrays 与其总和组合/替换 - combine/replace arrays of selected indices/rows along a specific axis of a N-D array with their sum Python Numpy初学者:获取形状为((M,N),(M,N))的数组 - Python Numpy beginner: getting an array with a shape ((M,N),(M,N)) 从形状(m,n)的numpy数组创建Pandas系列 - Create Pandas Series from numpy array of shape (m, n) 在不到 O(m * n) 的时间内反转二维数组中的每一行 - Reverse each row in a 2D array in less than O(m * n) time 在 O(1) 时间内计算 mxn 二维数组 A 的第 k 个元素 - Compute the kth element in spiral order for an m x n 2D array A in O(1) time 如何在形状为 (n,m) 的 numpy 数组的选定列中添加元素? - How to add element in a selected column of a numpy array of shape (n,m)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM