如何向NumPy数组中添加新数据

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

How to add new data to numpy array

问题

Here's the translated code snippet without the translation of the code comments:

我想要将新数据添加到我的NumPy数组中这是我的数组示例

A = np.array([
    [4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
    [5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
    [5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
    [4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
    [4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
    [4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
])

B = np.array([
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
])

如何在每2次迭代中将B添加到A中

预期的A在添加B后如下所示

array([
    [4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
    [5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
    [4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
    [4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
])

我尝试使用了以下代码但我希望它能够更动态因为我想要使迭代动态化现在我想每2次迭代也许以后我想每50次迭代这是我的代码

result = np.empty((0, A.shape[1]))

for i in range(0, A.shape[0], 2):
    result = np.vstack([result, A[i:i+2], B, B])

print(result)

If you have any specific questions or need further assistance with this code, please let me know.

英文:

I want add new data to my numpy array. here is the example of my array.

A = np.array([
[4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
[5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
[5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
[4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
[4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
[4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
])
B = np.array([
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
])

How to Add B each 2 iteration on A.

expected A after add B:

array([
[4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
[5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
[4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
[4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
])

I've try used this code but I want make it dynamic. because I want make the iteration dynamic. now I want each 2 iteration, maybe later I want make it each 50 iteration. here my code:

result = np.empty((0, A.shape[1]))
for i in range(0, A.shape[0], 2):
result = np.vstack([result, A[i:i+2], B, B])
print(result)

答案1

得分: 2

从较小的数组创建一个大的NumPy数组需要将所有数据从较小的数组复制过来,因此在循环中使用vstack构建新数组效率低下。相反,预先分配一个较大的数组,然后分配给切片:

result = np.empty((A.shape[0] * 2, A.shape[1]))
result[::4] = A[::2]
result[1::4] = A[1::2]
result[2::4] = B
result[3::4] = B

现在,要软编码B行的频率和数量,您需要首先计算新数组的大小。由于我们每隔b_freq行(来自A)添加b_count行(来自B),可以计算new_row_count如下所示:

b_freq = 3  # 每三行添加一次B
b_count = 2 # 每b_freq行添加B 2次

old_row_count = A.shape[0]
new_row_count = old_row_count + int(np.ceil(old_row_count / b_freq) * b_count)

step = b_freq + b_count

result = np.empty((new_row_count, A.shape[1]))

# 设置来自A的行
for i in range(b_freq):
    result[i::step] = A[i::step]

# 设置来自B的行
for i in range(b_count):
    result[b_freq+i::step] = B

这将得到所需的结果。

另外,还可以创建一个包含result数组所有行的列表,并在完成列表后将行堆叠成数组:

rows = []

for i in range(0, A.shape[0], 2):
    rows.extend([A[i:i+2], B, B])

result = np.vstack(rows)

使用这种方法,软编码B行的频率更容易,因为不需要预先计算新数组的大小:只需更改循环中range的步长。

b_freq = 3  # 每三行添加一次B
b_count = 2 # 每b_freq行添加B 2次
rows = []

for i in range(0, A.shape[0], b_freq):
    rows.append(A[i:i+b_freq])
    rows.extend([B] * b_count)

result = np.vstack(rows)

这将在每三行中产生两行零值。

英文:

Creating a big numpy array from smaller arrays requires you to copy all the data from the smaller arrays, so it's inefficient to keep building new arrays using vstack in a loop. Instead, pre-allocate a larger array, and then assign to the slices:

result = np.empty((A.shape[0] * 2, A.shape[1]))
result[::4] = A[::2]
result[1::4] = A[1::2]
result[2::4] = B
result[3::4] = B

Now, to soft-code the frequency and number of B rows, you need to first calculate the size of the new array. Since we add b_count rows (from B) every b_freq rows of A, the new_row_count can be calculated as shown below:

b_freq = 3 # Add B every three rows
b_count = 2 # Add B 2 times every b_freq rows
old_row_count = A.shape[0]
new_row_count = old_row_count + int(np.ceil(old_row_count / b_freq) * b_count)
step = b_freq + b_count
result = np.empty((new_row_count, A.shape[1]))
# Set rows from A
for i in range(b_freq):
result[i::step] = A[i::step]
# Set rows from B
for i in range(b_count):
result[b_freq+i::step] = B

This gives the desired result:

[[ 4.752735   1.16801   -3.9976401 -0.319121  -7.4322705 -3.8692   ]
[ 5.2125     1.165454  -3.99143   -0.318649  -7.43615   -4.12229  ]
[ 5.16795    1.083616  -3.9877    -0.315453  -7.43191   -3.97633  ]
[ 0.         0.         0.         0.         0.         0.       ]
[ 0.         0.         0.         0.         0.         0.       ]
[ 4.711875   1.143996  -3.98545   -0.304904  -7.4152703 -3.90752  ]
[ 5.2125     1.165454  -3.99143   -0.318649  -7.43615   -4.12229  ]
[ 5.16795    1.083616  -3.9877    -0.315453  -7.43191   -3.97633  ]
[ 0.         0.         0.         0.         0.         0.       ]
[ 0.         0.         0.         0.         0.         0.       ]]

Alternatively, create a list containing all the rows of the result array, and stack the rows into an array after you've finished making the list:

rows = []
for i in range(0, A.shape[0], 2):
rows.extend([A[i:i+2], B, B])
result = np.vstack(rows)

With this method, soft-coding the frequency of B rows is easier since we don't need to pre-calculate the size of the new array: just change the step in the range of the for loop.

b_freq = 3  # Add B every three rows
b_count = 2 # Add B 2 times every b_freq rows
rows = []
for i in range(0, A.shape[0], b_freq):
rows.append(A[i:i+b_freq])
rows.extend([B] * b_count)
result = np.vstack(rows)

Which gives two zero rows every three rows:

[[ 4.752735    1.16801    -3.9976401  -0.319121   -7.4322705  -3.8692    ]
[ 5.2125      1.165454   -3.99143    -0.318649   -7.43615    -4.12229   ]
[ 5.16795     1.083616   -3.9877     -0.315453   -7.43191    -3.97633   ]
[ 0.          0.          0.          0.          0.          0.        ]
[ 0.          0.          0.          0.          0.          0.        ]
[ 4.660315    0.89965    -3.98708    -0.321073   -7.41803    -3.69156   ]
[ 4.839925    0.93606997 -3.9822102  -0.315018   -7.41289    -3.79918   ]
[ 4.711875    1.143996   -3.98545    -0.304904   -7.4152703  -3.90752   ]
[ 0.          0.          0.          0.          0.          0.        ]
[ 0.          0.          0.          0.          0.          0.        ]]

huangapple
  • 本文由 发表于 2023年5月10日 23:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220446.html
匿名

发表评论

匿名网友

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

确定