我想知道如何使用python 2.6.6和numpy版本1.5.0用零填充2D numpy数组。抱歉! 但是这些是我的局限性。因此我不能使用np.pad。例如,我想a用零填充以使其形状匹配b。我想这样做的原因是我可以这样做:
b-a 这样
a array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]]) b array([[ 3., 3., 3., 3., 3., 3.], [ 3., 3., 3., 3., 3., 3.], [ 3., 3., 3., 3., 3., 3.], [ 3., 3., 3., 3., 3., 3.]]) c array([[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0]]) 我能想到的唯一方法是追加,但这看起来很丑。是否有可能使用更清洁的解决方案b.shape?
编辑,谢谢MSeiferts的答案。我不得不清理一下,这就是我得到的:
def pad(array, reference_shape, offsets): """ array: Array to be padded reference_shape: tuple of size of ndarray to create offsets: list of offsets (number of elements must be equal to the dimension of the array) will throw a ValueError if offsets is too big and the reference_shape cannot handle the offsets """
# Create an array of zeros with the reference shape
result = np.zeros(reference_shape)
# Create a list of slices from offset to offset + shape in each dimension
insertHere = [slice(offsets[dim], offsets[dim] + array.shape[dim]) for dim in range(array.ndim)]
# Insert the array in the result at the specified offsets
result[insertHere] = array
return result
问题来源于stack overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
很简单,使用参考形状创建一个包含零的数组:
result = np.zeros(b.shape)
然后在需要的地方插入数组:
result[:a.shape[0],:a.shape[1]] = a 瞧,您已经填充了它:
print(result) array([[ 1., 1., 1., 1., 1., 0.], [ 1., 1., 1., 1., 1., 0.], [ 1., 1., 1., 1., 1., 0.], [ 0., 0., 0., 0., 0., 0.]]) 如果您定义应该在左上方插入元素的位置,也可以使其更通用一些
result = np.zeros_like(b) x_offset = 1 # 0 would be what you wanted y_offset = 1 # 0 in your case result[x_offset:a.shape[0]+x_offset,y_offset:a.shape[1]+y_offset] = a result
array([[ 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 1., 1., 1.], [ 0., 1., 1., 1., 1., 1.], [ 0., 1., 1., 1., 1., 1.]]) 但请注意,偏移量不要超过允许的范围。例如x_offset = 2,这将失败。
如果您有任意数量的维,则可以定义切片列表以插入原始数组。我发现有趣的是,可以玩一下并创建一个填充函数,该函数可以填充(带有偏移量)任意形状的数组,只要该数组和参考具有相同的维数并且偏移量不会太大即可。
def pad(array, reference, offsets): """ array: Array to be padded reference: Reference array with the desired shape offsets: list of offsets (number of elements must be equal to the dimension of the array) """ # Create an array of zeros with the reference shape result = np.zeros(reference.shape) # Create a list of slices from offset to offset + shape in each dimension insertHere = [slice(offset[dim], offset[dim] + array.shape[dim]) for dim in range(a.ndim)] # Insert the array in the result at the specified offsets result[insertHere] = a return result 和一些测试用例:
import numpy as np
a = np.ones(2) b = np.ones(5) offset = [3] pad(a, b, offset)
a = np.ones((3,3,3)) b = np.ones((5,4,3)) offset = [1,0,0] pad(a, b, offset)