আমি জানতে চাই যে কীভাবে আমি ন্যালি সংস্করণ 1.5.0 এর সাথে পাইথন ২.6. using ব্যবহার করে জিরোসের সাহায্যে একটি 2 ডি নম্পি অ্যারে প্যাড করতে পারি। দুঃখিত! তবে এগুলি আমার সীমাবদ্ধতা। অতএব আমি ব্যবহার করতে পারি না 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
?
সম্পাদনা করুন, MSeferts উত্তরের জন্য আপনাকে ধন্যবাদ। আমাকে এটি কিছুটা পরিষ্কার করতে হয়েছিল এবং আমি এটি পেয়েছি:
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
padded = np.zeros(b.shape)
padded[tuple(slice(0,n) for n in a.shape)] = a