0

I have a numpy array called 'MEL' of shape (94824,).

These values contain arrays of different shapes such as (99,13), (54, 13) (87, 13). I'd like to fill the arrays that are smaller than (99,13) with zeros or even better the mean value of that array.

MEL = numpy.ndarray and
for i in MEL: i = <class 'numpy.ndarray'> (i.shape = 99, 13) except for the ones that need to be filled
for j in i: j = <class 'numpy.ndarray'>

Up till now I have this:

max_len = np.max([len(a) for a in MEL])
for i in MEL:
    i = np.asarray([np.pad(a, (0, max_len - len(a)), 'constant', constant_values=0) for a in i])

But the shapes remain the same. any suggestions?

4
  • 1
    How can your numpy array MEL contain other arrays ? I guess Mel is a list of arrays. In that case, be carefull that len(a) only returns the length along the first dimension of the array. How do you want to fill the small arrays ? Commented Dec 14, 2018 at 13:48
  • I know it is a weird structure and i don't really know how to tackle it either I have added some information about it in the question Commented Dec 14, 2018 at 13:53
  • I actually do not understand how you created such a structure whitout encountering 'ValueError: setting an array element with a sequence.' Commented Dec 14, 2018 at 13:58
  • I didn't create the structure myself but imported it as an .npy file. Commented Dec 14, 2018 at 14:02

1 Answer 1

1

From what I understand of your question, MEL is a list of 94824 two dimensional arrays that have different shapes. You'd like to return arrays that have the same shape than the largest one, but filled with 0.

I guess the simplest it to create new arrays that have the appropriate shape and fill them with the former ones. A small example would be:

max_dim = [np.max([a.shape[0] for a in MEL]), np.max([a.shape[1] for a in MEL])]
new_MEL = []
for a in MEL:
    temp = np.zeros((max_dim[0], max_dim[1]))
    temp[:a.shape[0], :a.shape[1]] = a
    new_MEL.append(temp)
Sign up to request clarification or add additional context in comments.

1 Comment

Apparently you understood correctly cause it worked. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.