循环迭代 numpy 数组列(维度为 1D 或 2D)

a loop to iterate on a numpy array columns (with dimensionality of either 1D or 2D)

我正在尝试编写一个函数来获取一个 numpy 数组 INPUT,并将它的列一个一个地传递给另一个函数。 INPUT 数组是 1D 或 2D(不多) 第二个函数需要一维数组作为参数。 (len(param.shape)==1)

我读过一个类似的线程,其中 OP 想要对所有列求和并检查其他条件... 这个,可能需要另一个答案。

所需的伪代码操作:

def func(INPUT,a,b,...)
    for column in INPUT: #whether be a 1D or 2D
        result = another_func(column,...)

试过这个: 问题是如何不检查 func:

中 INPUT 数组的维度
if(len(INPUT.shape)==1):
    another_func(INPUT,....)
elif(len(INPUT.shape)==2):
    for c in range(INPUT.shape[1]):
        another_func(INPUT[:,c])

思路是:在1d输入的情况下,转化为1列2d数组,然后作为2d输入。

def func(INPUT, a, b):
    return np.apply_along_axis(
        lambda col: another_func(col, a, b),  # function to apply
        1,  # axis along which to apply; 1 = columns
        np.reshape(np.atleast_2d(H2), (len(H), -1))  # transform 1D->2D, if necessary
    )