[英]Iterating numpy arrays and formatting into multi dimensional arrays
I am trying code a numpy function where it organizes the Appending_list
function below to a multi dimensional array for each iteration of Values
.我正在尝试编写一个 numpy function 代码,它将下面的Appending_list
function 组织为Values
的每次迭代的多维数组。 So in the first iteration is Number_array + 0
for the first row and the second row is Number_array + 1
and the third is Number_array + 2
.因此,在第一次迭代中,第一行是Number_array + 0
,第二行是Number_array + 1
,第三行是Number_array + 2
。
import numpy as np
from numpy import random
Values = np.arange(0,3,1)
Number_array = np.arange(1,5,1)
Appending_list = Number_array + Values * len(Number_array)
Expected Output预计 Output
[[1,2,3,4], [2,3,4,5], [3,4,5,6]]
values = np.arange(1, 5)
rows = 3
# creates 3 rows of `[1, 2, 3, 4]`
result = np.tile(values, rows).reshape((rows, len(values)))
shifts = np.arange(3)
# shifts the row values for each row by 0, 1, and 2 respectively
result += shifts[:,None]
Result结果
array([[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
You are looking into broadcasting:您正在研究广播:
Appending_list = Number_array + Values[:,None]
Output: Output:
array([[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.