这样做可以使生成的数组的形状略小于所需的最大值,或者使它们完全具有所需的最大值,除了最后的一些余数。
基本逻辑是计算分割数组的参数,然后使用array_split沿着数组的每个轴(或维度)分割数组。
我们需要numpy 和math 模块以及示例数组:
import math
import numpy
a = numpy.random.choice([1,2,3,4], (1009,1009))
略小于最大值
逻辑
首先将最终块大小的形状沿要拆分的每个维度存储在一个元组中:
chunk_shape = (50, 50)
array_split 一次只沿一个轴(或维度)或一个数组拆分。所以让我们从第一个轴开始。
-
计算我们需要将数组拆分成的部分数:
num_sections = math.ceil(a.shape[0] / chunk_shape[0])
在我们的示例中,这是 21 (1009 / 50 = 20.18)。
-
现在拆分它:
first_split = numpy.array_split(a, num_sections, axis=0)
这为我们提供了 21 个(请求部分的数量)numpy 数组的列表,这些数组被拆分,因此它们在第一维中不大于 50:
print(len(first_split))
# 21
print({i.shape for i in first_split})
# {(48, 1009), (49, 1009)}
# These are the distinct shapes, so we don't see all 21 separately
在这种情况下,它们是沿该轴的 48 和 49。
-
我们可以对第二维的每个新数组做同样的事情:
num_sections = math.ceil(a.shape[1] / chunk_shape[1])
second_split = [numpy.array_split(a2, num_sections, axis=1) for a2 in first_split]
这给了我们一个列表列表。每个子列表都包含我们想要的大小的 numpy 数组:
print(len(second_split))
# 21
print({len(i) for i in second_split})
# {21}
# All sublists are 21 long
print({i2.shape for i in second_split for i2 in i})
# {(48, 49), (49, 48), (48, 48), (49, 49)}
# Distinct shapes
完整功能
我们可以使用递归函数实现任意维度:
def split_to_approx_shape(a, chunk_shape, start_axis=0):
if len(chunk_shape) != len(a.shape):
raise ValueError('chunk length does not match array number of axes')
if start_axis == len(a.shape):
return a
num_sections = math.ceil(a.shape[start_axis] / chunk_shape[start_axis])
split = numpy.array_split(a, num_sections, axis=start_axis)
return [split_to_approx_shape(split_a, chunk_shape, start_axis + 1) for split_a in split]
我们这样称呼它:
full_split = split_to_approx_shape(a, (50,50))
print({i2.shape for i in full_split for i2 in i})
# {(48, 49), (49, 48), (48, 48), (49, 49)}
# Distinct shapes
精确的形状加上余数
逻辑
如果我们想要更漂亮一点并且让所有新数组完全除了尾随剩余数组之外的指定大小,我们可以通过传递要拆分的索引列表来做到这一点array_split.
-
首先建立索引数组:
axis = 0
split_indices = [chunk_shape[axis]*(i+1) for i in range(math.floor(a.shape[axis] / chunk_shape[axis]))]
这给出了一个索引列表,从最后一个开始每个 50 个:
print(split_indices)
# [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000]
-
然后拆分:
first_split = numpy.array_split(a, split_indices, axis=0)
print(len(first_split))
# 21
print({i.shape for i in first_split})
# {(9, 1009), (50, 1009)}
# Distinct shapes, so we don't see all 21 separately
print((first_split[0].shape, first_split[1].shape, '...', first_split[-2].shape, first_split[-1].shape))
# ((50, 1009), (50, 1009), '...', (50, 1009), (9, 1009))
-
然后是第二个轴:
axis = 1
split_indices = [chunk_shape[axis]*(i+1) for i in range(math.floor(a.shape[axis] / chunk_shape[axis]))]
second_split = [numpy.array_split(a2, split_indices, axis=1) for a2 in first_split]
print({i2.shape for i in second_split for i2 in i})
# {(9, 50), (9, 9), (50, 9), (50, 50)}
完整功能
适配递归函数:
def split_to_shape(a, chunk_shape, start_axis=0):
if len(chunk_shape) != len(a.shape):
raise ValueError('chunk length does not match array number of axes')
if start_axis == len(a.shape):
return a
split_indices = [
chunk_shape[start_axis]*(i+1)
for i in range(math.floor(a.shape[start_axis] / chunk_shape[start_axis]))
]
split = numpy.array_split(a, split_indices, axis=start_axis)
return [split_to_shape(split_a, chunk_shape, start_axis + 1) for split_a in split]
而且我们的称呼完全一样:
full_split = split_to_shape(a, (50,50))
print({i2.shape for i in full_split for i2 in i})
# {(9, 50), (9, 9), (50, 9), (50, 50)}
# Distinct shapes
补充说明
性能
这些功能似乎相当快。我能够在 0.05 秒内使用任一函数将我的示例数组(包含超过 140 亿个元素)拆分为 1000 x 1000 个形状的块(产生超过 14000 个新数组):
print('Building test array')
a = numpy.random.randint(4, size=(55000, 250000), dtype='uint8')
chunks = (1000, 1000)
numtests = 1000
print('Running {} tests'.format(numtests))
print('split_to_approx_shape: {} seconds'.format(timeit.timeit(lambda: split_to_approx_shape(a, chunks), number=numtests) / numtests))
print('split_to_shape: {} seconds'.format(timeit.timeit(lambda: split_to_shape(a, chunks), number=numtests) / numtests))
输出:
Building test array
Running 1000 tests
split_to_approx_shape: 0.035109398348040485 seconds
split_to_shape: 0.03113800323300747 seconds
我没有测试高维数组的速度。
小于最大值的形状
如果任何维度的大小小于指定的最大值,这些函数都可以正常工作。这不需要特殊的逻辑。