Rocking chief
Rocking chief

Reputation: 1069

Use lambda expression with numpy array

I am trying to fill my numpy array with training labels. Say k = 0 and size = 3, k = 1 and size = 3, The result should look like: np.array([0], [0], [0], [1], [1], [1])

Now this code works:

np.array([0]*size1, [1]*size2, ...)

But this does not work:

train_labels = np.array([k]*size for k, size in enumerate(train_sizes))

When I call keras.utils.to_categorical like:

train_labels = keras.utils.to_categorical(train_labels, num_classes = class_size)

It would give me the following error:

TypeError: long() argument must be a string or a number, not 'generator'

Can someone please help me with this? Thanks!

Upvotes: 0

Views: 3378

Answers (2)

fferri
fferri

Reputation: 18940

Use a list comprehension:

>>> train_labels = np.array([[k]*size for k, size in enumerate(train_sizes)])

instead of a generator.

Upvotes: 2

abarnert
abarnert

Reputation: 365767

([k]*size for k, size in enumerate(train_sizes)) is a generator expression, which means it evaluates to an iterator, not a sequence. np.array wants sequences; if you give it an iterator, it treats it as a sequence of one object, the iterator itself, which is not very useful. (That's what keras is complaining about—it's expecting an array of numbers, and you've given it an array of iterator objects.)

But np.array will take lists, and you can trivially turn a generator expression into a list comprehension by using brackets instead of parens (or in addition to parens, where the extra parens are optional):

np.array([[k]*size for k, size in enumerate(train_sizes)])

Or, if you don't want to waste time and memory building that list, you can use np.fromiter, but that's a bit tricky to use for 2D arrays, and your sequence is small enough that I wouldn't worry about converting it to a list here.

Upvotes: 3

Related Questions