numpy函数`array_split`在数学上如何工作?

我需要编写一个Python函数,当传递一个数组和一个整数N时,该函数返回该数组的内容,该内容分为相等大小的N个子数组。

如果无法将数组的长度除以N,则最终的子数组必须具有合适的长度以容纳其余元素。

示例: split_array(array=[1,2,3,4,5,6,7,8,9,10],n=4)

应输出:[[1,3],[4,6],[7,8],[9,10]]

我的研究表明numpy.array_split函数确实做到了这一点,我查看了GitHub上的源代码,发现它首先组成了一个包含所有子数组大小的数组,然后它迭代以拆分原始数组。

来自numpy.array_split的删节样本

def array_split(ary,indices_or_sections,axis=0):
    # indices_or_sections is a scalar,not an array.
    Nsections = int(indices_or_sections)
    if Nsections <= 0:
        raise ValueError('number sections must be larger than 0.')
    Neach_section,extras = divmod(Ntotal,Nsections)
    section_sizes = ([0] +
                     extras * [Neach_section+1] +
                     (Nsections-extras) * [Neach_section])
    div_points = _nx.array(section_sizes,dtype=_nx.intp).cumsum()

    sub_arys = []
    sary = _nx.swapaxes(ary,axis,0)
    for i in range(Nsections):
        st = div_points[i]
        end = div_points[i + 1]
        sub_arys.append(_nx.swapaxes(sary[st:end],0))

    return sub_arys

我唯一要理解的是变量section_sizes是如何数学创建的。 对于示例split_array(array=[1,n=4),它构建了一个[3,2]大小列表,这正是我需要的大小,但是我不明白为什么它起作用。

我知道divmod(Ntotal,Nsections)将为您提供除法计算的商(Neach_section)和余数(extras)。

但是为什么quotient * [remainder+1]总是给您正确数量的正确大小的“商”子数组大小(在此示例[3,3]中)?

为什么[quotient-remainder] * quotient为您提供正确数量的正确大小的“剩余”子数组大小(在本示例中为[2,2])?

有人甚至可以告诉我这种运算是什么,或者它涉及的是数学的哪个分支,因为这不是我以前遇到的。

zongjun0221 回答:numpy函数`array_split`在数学上如何工作?

为清楚起见,我将参考以下内容:

Neach_section,extras = divmod(Ntotal,Nsections)
section_sizes = ([0] +
                 extras * [Neach_section+1] +
                 (Nsections-extras) * [Neach_section])

quotient,remainder = divmod(Ntotal,Nsections)
section_sizes = ([0] +
                 remainder * [quotient+1] +
                 (Nsections- remainder) * [quotient])

首先,让我们想象一下与您的问题中所示情况类似的情况。 (修改为商!=余数)

print(np.array_split(np.arange(1,15),4) 
>>>[array([1,2,3,4]),array([5,6,7,8]),array([ 9,10,11]),array([12,13,14])]

从最终代表的分歧的角度考虑它更容易。

14 = 4 * 3 + 2

相同

14 =(3 + 3 + 3 + 3)+ 2

=(3 + 3 + 3 + 3)+(1 +1)

至关重要的是,我们可以将这些内容添加到第一个括号中的前两个术语中。

14 = 4 + 4 + 3 + 3

通常,我们要做的是在输出列表的第一(剩余)项中添加一个,从而获得代码段

...remainder * [quotient+1]...

在输出中的(商)项中,我们添加了第一个(余数)项,从而为我们填充了下一个(商数-余数)项

...(Nsections- remainder) * [quotient])

留下最终代码。

  

有人甚至可以告诉我这种运算是什么,或者它涉及的是数学的哪个分支,因为这不是我以前遇到的。

我相信这与数论之间存在松散的关系,商-余数定理可能是您首先学到的东西之一。

无论如何,我希望对您有所帮助:)

本文链接:https://www.f2er.com/3141835.html

大家都在问