1

I have a Numpy array of arrays and each array inside the main array has a different size. I need to pad the end with 0s at each array so that all arrays that are inside the main array are of equal length but I do not know the max length. I want it to be done in a cleaner way instead of finding the max length and assigning 0's at the end as and when required. a below is already a Numpy aray

a=[
   [1,2,3,4],
   [3,56],
   [8,4,8,4,9,33,55]
  ] .
   In this case maxlength is 7(the length of third array) . 
   I want final array to look like follows 
a=[
   [1,2,3,4,0,0,0],
   [3,56,0,0,0,0,0],
   [8,4,8,4,9,33,55]
  ]
0

2 Answers 2

1

For numpy.pad solution I think we need to ensure your input is exactly as you have it so we can get a proper solution. Then it will just be:

a=[
      np.asarray([1,2,3,4]),
      np.asarray([3,56]),
      np.asarray([8,4,8,4,9,33,55])
 ]


max_len = max([len(x) for x in a])

output = [np.pad(x, (0, max_len - len(x)), 'constant') for x in a]

print(output)

>>> [
     array([1, 2, 3, 4, 0, 0, 0]), 
     array([ 3, 56,  0,  0,  0,  0,  0]), 
     array([ 8,  4,  8,  4,  9, 33, 55])
    ]
Sign up to request clarification or add additional context in comments.

5 Comments

i want to use np.pad ... not sure if I can use. I wanted a cleaner way and my array is Numpy array , i will need that to be numpy for future also
Your input array cannot be a numpy array. Is it a list of numpy arrays?
yes list of numpy array [Numpy array,Numpy array, Numpy array]
ok working on it
this works. Thanks !!
0

Quick and dirty:

a=[
   [1,2,3,4],
   [3,56],
   [8,4,8,4,9,33,55]
  ]

n = len(a)
m = max([len(x) for x in a])

A = np.zeros((n,m))
for i in range(n):
    A[i,:len(a[i])] = a[i]
print A

As I saw in your comment on @Scott Skiles solution, that you want to use np.pad for this, wou can change the line within the for loop by:

A[i] = np.pad(a[i],(0,m-len(a[i])),'constant', constant_values=0)

4 Comments

can i use np.pad and automatically do it instead of m=max([len(x) for x in a]) and for loop?
I donot think so, as np.pad needs a numpy array (or alike) as input. Your input is not alike. If you need it more often, just make a function
it works , but I see that in the process a becomes list of list at the end
print type(A) gives <type 'numpy.ndarray'> for me. I am not sure if I understood your expected output correctly. I thought you wanted a numpy array in the end. If you want a list of np.arrays just stick to Scott's solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.