简体   繁体   中英

Python list comprehension over arrays

I have a list of numpy arrays and I'm trying to create a new list with all the elements of the original list, except one. I have the following code:

for i in xrange(FOLDS):
    #fold_sample_sets and fold_sample_labels are a list of 10 numpy arrays.
    training_samples = [s for s in fold_sample_sets if fold_sample_sets.index(s) != i]
    training_labels = [l for l in fold_label_sets if fold_label_sets.index(l) != i]

I've tried this with small examples in the interpreter and it's seemed to work. But here I get the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Any suggestions on how to fix this?

You can use array slicing,

for i in range(FOLDS):
    # omit set #i from the list
    training_samples = fold_sample_sets[:i] + fold_sample_sets[i+1:]
    training_labels  = fold_label_sets [:i] + fold_label_sets [i+1:]

In your comprehensions you have:

if fold_sample_sets.index(s) != i

The condition for an if has to be True/False. Typically this ValueError is produced when the condition is an array with more than one element. For example:

if np.array([True,False]):
    print(1)

Is this condition True, or is it False? Or is it both? Should it print 1 or not?

The error is suggesting that you use np.any or np.all to reduce the multiple values down to one. The alternative is to make sure that you are not applying the logical operator to a multiple element array.

What is fold_sample_sets.index(s) != i (for a typical value of s )?


sets = [np.ones((2,2))*i for i in range(5)]
[s for s in sets if sets.index(s)!=3]  # this error
sets.index(s)   # same error

Looks like list .index does not work with array elements. To calculate this index, Python compares s with each element in sets . When we compare one list with another, [1,2]==[3,4] , we get a simple boolean, False . But when we compare one array with another we get another array: np.array([1,2])==np.array([3,4]) , array([False, False], dtype=bool) . That's where the ambiguity arises.

A better way of doing the same comprehension (without this .index ) is:

[s for j,s in enumerate(sets) if j!=i]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM