Python numpy: Add elements of a numpy array of arrays to elements of another array of arrays initialized to at the specified positions

huangapple go评论67阅读模式
英文:

Python numpy: Add elements of a numpy array of arrays to elements of another array of arrays initialized to at the specified positions

问题

以下是翻译后的内容:

假设我们有一个由零组成的numpy数组的numpy数组,如下所示:

arr1 = np.zeros((len(Train), L))

其中 Train 是一个固定长度的整数数组的 (dataset) numpy 数组。我们还有另一个长度为 len(Train) 的1维numpy数组 positions

现在,我们希望将 Train 中的元素添加到 arr1 中,位置由 positions 指定。

一种方法是在 Train 数组上使用for循环,如下所示:

k = len(Train[0])
for i in range(len(Train)):
    arr1[i, int(positions[i]):int((positions[i] + k))] = Train[i, 0:k]

然而,使用显式的for循环遍历整个 Train 集合速度较慢,我想要优化它。

英文:

Suppose we have a numpy array of numpy arrays of zeros as

arr1=np.zeros((len(Train),(L))

where Train is a (dataset) numpy array of arrays of integers of fixed length.
We also have another 1d numpy array, positions of length as len(Train).

Now we wish to add elements of Train to arr1 at the positions specified by positions.

One way is to use a for loop on the Train array as:

k=len(Train[0])
for i in range(len(Train)):
    arr1[i,int(positions[i]):int((positions[i]+k))]=Train[i,0:k])]

However, going over the entire Train set using the explicit for loop is slow and I would like to optimize it.

答案1

得分: 1

这是一种通过生成要分配的所有索引的方法。设置:

import numpy as np

n = 12   # 训练样本的数量
l = 8    # 输出数组的列数
k = 4    # 训练样本的列数

arr       = np.zeros((n, l), dtype=int)
train     = np.random.randint(10, size=(n, k))
positions = np.random.randint(l - k, size=n)

随机示例数据:

>>> train
array([[3, 4, 3, 2],
       [3, 6, 4, 1],
       [0, 7, 9, 6],
       [4, 0, 4, 8],
       [2, 2, 6, 2],
       [4, 5, 1, 7],
       [5, 4, 4, 4],
       [0, 8, 5, 3],
       [2, 9, 3, 3],
       [3, 3, 7, 9],
       [8, 9, 4, 8],
       [8, 7, 6, 4]])
>>> positions
array([3, 2, 3, 2, 0, 1, 2, 2, 3, 2, 1, 1])

使用广播技巧的高级索引:

rows = np.arange(n)[:, None]             # 形状 (n, 1)
cols = np.arange(k) + positions[:, None] # 形状 (n, k)
arr[rows, cols] = train

输出:

>>> arr
array([[0, 0, 0, 3, 4, 3, 2, 0],
       [0, 0, 3, 6, 4, 1, 0, 0],
       [0, 0, 0, 0, 7, 9, 6, 0],
       [0, 0, 4, 0, 4, 8, 0, 0],
       [2, 2, 6, 2, 0, 0, 0, 0],
       [0, 4, 5, 1, 7, 0, 0, 0],
       [0, 0, 5, 4, 4, 4, 0, 0],
       [0, 0, 0, 8, 5, 3, 0, 0],
       [0, 0, 0, 2, 9, 3, 3, 0],
       [0, 0, 3, 3, 7, 9, 0, 0],
       [0, 8, 9, 4, 8, 0, 0, 0],
       [0, 8, 7, 6, 4, 0, 0, 0]])
英文:

Here is one way by generating all the indexes you want to assign to. Setup:

import numpy as np

n = 12   # Number of training samples
l = 8    # Number of columns in the output array
k = 4    # Number of columns in the training samples

arr       = np.zeros((n, l), dtype=int)
train     = np.random.randint(10, size=(n, k))
positions = np.random.randint(l - k, size=n)

Random example data:

>>> train
array([[3, 4, 3, 2],
       [3, 6, 4, 1],
       [0, 7, 9, 6],
       [4, 0, 4, 8],
       [2, 2, 6, 2],
       [4, 5, 1, 7],
       [5, 4, 4, 4],
       [0, 8, 5, 3],
       [2, 9, 3, 3],
       [3, 3, 7, 9],
       [8, 9, 4, 8],
       [8, 7, 6, 4]])
>>> positions
array([3, 2, 3, 2, 0, 1, 2, 2, 3, 2, 1, 1])

Advanced indexing with broadcasting trickery:

rows = np.arange(n)[:, None]             # Shape (n, 1)
cols = np.arange(k) + positions[:, None] # Shape (n, k)
arr[rows, cols] = train

output:

>>> arr
array([[0, 0, 0, 3, 4, 3, 2, 0],
       [0, 0, 3, 6, 4, 1, 0, 0],
       [0, 0, 0, 0, 7, 9, 6, 0],
       [0, 0, 4, 0, 4, 8, 0, 0],
       [2, 2, 6, 2, 0, 0, 0, 0],
       [0, 4, 5, 1, 7, 0, 0, 0],
       [0, 0, 5, 4, 4, 4, 0, 0],
       [0, 0, 0, 8, 5, 3, 0, 0],
       [0, 0, 0, 2, 9, 3, 3, 0],
       [0, 0, 3, 3, 7, 9, 0, 0],
       [0, 8, 9, 4, 8, 0, 0, 0],
       [0, 8, 7, 6, 4, 0, 0, 0]])

huangapple
  • 本文由 发表于 2023年2月18日 17:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定