Conweezy
Conweezy

Reputation: 21

How to pad a specific dimension of a numpy array?

I'm trying to create a class that Right Pads a Numpy array to have the shape (257, 87). Currently the array has shape (257, 24), so I only need to pad the 2nd dim. I've tried a few iterations of the below class, but it always pads both dimensions.

class Padder:
  def __init__(self, mode="constant"):
    self.mode = mode

  def right_pad(self, array):
    num_missing_items = 87 - array.shape[1]
    padded_array = np.pad(array,
                          (num_missing_items, 0),
                          mode=self.mode)
    return padded_array

This results in shape (320, 87).

I also tried indexing the input array

class Padder:
  def __init__(self, mode="constant"):
    self.mode = mode

  def right_pad(self, array):
    num_missing_items = 87 - array.shape[1]
    padded_array = np.pad(array[1],
                          (num_missing_items, 0),
                          mode=self.mode)
    return padded_array

But this only returns the padded 2nd dim, nothing in the first dim, shape = (87,). So I tried to create a new array with the first dim as the original array's 1st dim, and 2nd dim as the padded 2nd dim.

class Padder:
  def __init__(self, mode="constant"):
    self.mode = mode

  def right_pad(self, array):
    num_missing_items = 87 - array.shape[1]
    padded_array = np.array([array[0], np.pad(array[1],
                          (num_missing_items, 0),
                          mode=self.mode)])
    return padded_array

But this returns an array of shape (2,)

How can I use padding to get my array to shape (257, 870)?

Upvotes: 0

Views: 1275

Answers (1)

Chrysophylaxs
Chrysophylaxs

Reputation: 6583

Have a look at the docs for the pad_width parameter for np.pad: https://numpy.org/doc/stable/reference/generated/numpy.pad.html

You can pass it a sequence of (before, after) tuples. In your case, the (before, after) for the first dimension needs to be (0, 0), and you can choose yourself for the second. Here is an example:

import numpy as np

arr = np.arange(12).reshape(4, 3)
padded = np.pad(arr, ((0, 0), (2, 3)))
array([[ 0,  0,  0,  1,  2,  0,  0,  0],
       [ 0,  0,  3,  4,  5,  0,  0,  0],
       [ 0,  0,  6,  7,  8,  0,  0,  0],
       [ 0,  0,  9, 10, 11,  0,  0,  0]])

Upvotes: 1

Related Questions