1

I have lists of different size like

a = [1,2,3]
b = [4,5]

and i want to create a two dimensional array with these values and a default value at places where the short lists have no data available.

c = do_something_with_a_b()
In: c 
Out: array([[1,2,3],
            [4,5, DEFAULT_VALUE]]) 

At the moment i use the following but i think this is over complicated:

all_ar = []
all_ar.append(a)
all_ar.append(b)
# Get the size of all arrays for masking
len_ar = np.array([array.size for array in all_ar])
# Create a mask according to the length of the arrays
mask = np.arange(len_ar.max()) < len_ar[:,None]
# Create an array filled with the default value, here -1
c = np.full(mask.shape, -1, dtype='int')
# Use the mask to overwrite the the default values with the 
# data from the arrays 
c[mask] = np.concatenate(all_ar)
In: c
Out: array([[1,2,3],
            [4,5,-1]]) 

Is there an easier way to convert irregular sized lists to a numpy array with regular shape and a default value at missing data points ?

0

1 Answer 1

1

You can use the pad function from numpy:

import numpy as np

a = [[1,2,3], [4, 5]]

# To what length do we need to pad?
max_len = np.array([len(array) for array in a]).max()

# What value do we want to fill it with?
default_value = 0

b = [np.pad(array, (0, max_len - len(array)), mode='constant', constant_values=default_value) for array in a]
b

What is the code doing?

The (0, max_len - len(array)) argument to pad says that we want to add 0 columns and max_len - len(array) rows to make sure the array matches the biggest array in our dataset.

Not the answer you're looking for? Browse other questions tagged or ask your own question.