Reputation: 1815
I have a numpy array as the following:
import numpy as np
arr = np.array([np.array([1]),np.array([1,2]),np.array([1,2,3]),np.array([1,3,4,2,4,2])])
I want a nice numpy function, which gives me the maximum length of the arrays inside my arr array. So I need a numpy function, which return for this example 6.
This is possible with iteration, but I am looking for a nicer way, perhaps even without map()
Any function inside tensorflow or keras would also be possible to use.
Upvotes: 1
Views: 1903
Reputation: 2472
Another simple trick is to use max() function with key argument to return the array with maximum length.
len(max(arr, key = len))
Upvotes: 1
Reputation: 1815
Another way would be to use the keras.preprocessing.sequence.pad_sequences()
function.
It pads the sequences to the lenght of the maximum, but in my opinion it creates a high memory usage and depending on the array it might take some time. However, this would be a way without looping:
len(keras.preprocessing.sequence.pad_sequences(arr)[0])
Upvotes: 0