程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了python如何用零填充numpy数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决python如何用零填充numpy数组?

开发过程中遇到python如何用零填充numpy数组的问题如何解决?下面主要结合日常开发的经验,给出你关于python如何用零填充numpy数组的解决方法建议,希望对你解决python如何用零填充numpy数组有所启发或帮助;

很简单,使用参形状创建一个包含零的数组:

result = np.zeros(b.shapE)
# actually you can also use result = np.zeros_like(b) 
# but that also copIEs the dtype not only the 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

# 1 Dimension
a = np.ones(2)
b = np.ones(5)
offset = [3]
pad(a, b, offset)

# 3 Dimensions

a = np.ones((3,3,3))
b = np.ones((5,4,3))
offset = [1,0,0]
pad(a, b, offset)

解决方法

我想知道如何使用python 2.6.6和numpy版本1.5.0用零填充2D numpy数组。抱歉!
但是这些是我的局限性。因此我不能使用np.pad。例如,我想a用零填充以使其形状匹配b。我想这样做的原因是我可以这样做:

b-a

这样

>>> a
array([[ 1.,1.,1.],[ 1.,1.]])
>>> b
array([[ 3.,3.,3.],[ 3.,3.]])
>>> c
array([[1,1,0],[1,[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

大佬总结

以上是大佬教程为你收集整理的python如何用零填充numpy数组全部内容,希望文章能够帮你解决python如何用零填充numpy数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。