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

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

How to add new data to numpy array

问题

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

  1. 我想要将新数据添加到我的NumPy数组中这是我的数组示例
  2. A = np.array([
  3. [4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
  4. [5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
  5. [5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
  6. [4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
  7. [4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
  8. [4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
  9. ])
  10. B = np.array([
  11. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  12. ])
  13. 如何在每2次迭代中将B添加到A
  14. 预期的A在添加B后如下所示
  15. array([
  16. [4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
  17. [5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
  18. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  19. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  20. [5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
  21. [4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
  22. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  23. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  24. [4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
  25. [4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
  26. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  27. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  28. ])
  29. 我尝试使用了以下代码但我希望它能够更动态因为我想要使迭代动态化现在我想每2次迭代也许以后我想每50次迭代这是我的代码
  30. result = np.empty((0, A.shape[1]))
  31. for i in range(0, A.shape[0], 2):
  32. result = np.vstack([result, A[i:i+2], B, B])
  33. 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.

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

How to Add B each 2 iteration on A.

expected A after add B:

  1. array([
  2. [4.752735, 1.16801, -3.9976401, -0.319121, -7.4322705, -3.8692],
  3. [5.2125, 1.165454, -3.99143, -0.318649, -7.43615, -4.12229],
  4. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  5. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  6. [5.16795, 1.083616, -3.9877, -0.315453, -7.43191, -3.97633],
  7. [4.660315, 0.89965, -3.98708, -0.321073, -7.41803, -3.69156],
  8. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  9. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  10. [4.839925, 0.93606997, -3.9822102, -0.315018, -7.41289, -3.79918],
  11. [4.711875, 1.143996, -3.98545, -0.304904, -7.4152703, -3.90752]
  12. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  13. [0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  14. ])

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:

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

答案1

得分: 2

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

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

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

  1. b_freq = 3 # 每三行添加一次B
  2. b_count = 2 # 每b_freq行添加B 2次
  3. old_row_count = A.shape[0]
  4. new_row_count = old_row_count + int(np.ceil(old_row_count / b_freq) * b_count)
  5. step = b_freq + b_count
  6. result = np.empty((new_row_count, A.shape[1]))
  7. # 设置来自A的行
  8. for i in range(b_freq):
  9. result[i::step] = A[i::step]
  10. # 设置来自B的行
  11. for i in range(b_count):
  12. result[b_freq+i::step] = B

这将得到所需的结果。

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

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

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

  1. b_freq = 3 # 每三行添加一次B
  2. b_count = 2 # 每b_freq行添加B 2次
  3. rows = []
  4. for i in range(0, A.shape[0], b_freq):
  5. rows.append(A[i:i+b_freq])
  6. rows.extend([B] * b_count)
  7. 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:

  1. result = np.empty((A.shape[0] * 2, A.shape[1]))
  2. result[::4] = A[::2]
  3. result[1::4] = A[1::2]
  4. result[2::4] = B
  5. 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:

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

This gives the desired result:

  1. [[ 4.752735 1.16801 -3.9976401 -0.319121 -7.4322705 -3.8692 ]
  2. [ 5.2125 1.165454 -3.99143 -0.318649 -7.43615 -4.12229 ]
  3. [ 5.16795 1.083616 -3.9877 -0.315453 -7.43191 -3.97633 ]
  4. [ 0. 0. 0. 0. 0. 0. ]
  5. [ 0. 0. 0. 0. 0. 0. ]
  6. [ 4.711875 1.143996 -3.98545 -0.304904 -7.4152703 -3.90752 ]
  7. [ 5.2125 1.165454 -3.99143 -0.318649 -7.43615 -4.12229 ]
  8. [ 5.16795 1.083616 -3.9877 -0.315453 -7.43191 -3.97633 ]
  9. [ 0. 0. 0. 0. 0. 0. ]
  10. [ 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:

  1. rows = []
  2. for i in range(0, A.shape[0], 2):
  3. rows.extend([A[i:i+2], B, B])
  4. 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.

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

Which gives two zero rows every three rows:

  1. [[ 4.752735 1.16801 -3.9976401 -0.319121 -7.4322705 -3.8692 ]
  2. [ 5.2125 1.165454 -3.99143 -0.318649 -7.43615 -4.12229 ]
  3. [ 5.16795 1.083616 -3.9877 -0.315453 -7.43191 -3.97633 ]
  4. [ 0. 0. 0. 0. 0. 0. ]
  5. [ 0. 0. 0. 0. 0. 0. ]
  6. [ 4.660315 0.89965 -3.98708 -0.321073 -7.41803 -3.69156 ]
  7. [ 4.839925 0.93606997 -3.9822102 -0.315018 -7.41289 -3.79918 ]
  8. [ 4.711875 1.143996 -3.98545 -0.304904 -7.4152703 -3.90752 ]
  9. [ 0. 0. 0. 0. 0. 0. ]
  10. [ 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:

确定