python max of list of arrays - python

I have a list of arrays like:
a = [array([6,2]),array([8,3]),array([4,2])]
I tried max(a) which returns the following error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I want it to return either a list or array like:
In: max(a)
Out: [8,3]
I don't want to convert the inner arrays to list, because the size of the list is very big. Also I purposefully created like that to perform array operations.

The easiest way is to convert to tuple/lists for the sake of comparison (or implement the comparison yourself):
>>> max(a, key=tuple)
array([8, 3])
Note this is the builtin max and not np.max
EDIT:
For multi dimensional arrays, use the .tolist method:
max(a, key=operator.methodcaller('tolist'))

Defining max on arrays is, as it says in the exception, ambiguous. If we have the following arrays: [6, 2], [5, 1], then I guess the output should be [6, 2], but if we have the following example: [6, 2], [7, 1] what would then the output have to be.
In fact there are so many different definitions of max here. If we take the arrays to be vectors then max can be the magnitude of them, or the vector that has maximum x coord, etc. Max can just as well compare the length of the arrays in the list and return the one with most elements or in the case of a tie the first one of the ones with equal length.
My suggestion is to create a class abstracting your array structures and define the max operation there with exactly the outcome you expect.

To have the max array along with the idx, this is what i'm using now:
a = [array([6,2]),array([8,3]),array([4,2])]
In: max_a = map(max, a)
Out: max_a = [6,8,4]
In: idx = max_a.index(max(max_a))
Out: idx = 1
In: result = a[idx]
Out: reusult = [8,3]

python 3.2
a=[([6,2]),([8,3]),([4,2])]
max(a)
Its working well

Related

python finding index of an array within a list

What I have is a list where the elements are an array like this:
([1,2,3],[4,5,6],[7,8,9])
What I want is to find the index of an element in this list, something like:
list.index([4,5,6]) #should return 1.
Problem is numpy array comparison throws up errors unless you put something like: (A==B).all()
But this comparison is inside the index function so i can't and don't really want to add the all() bit to the function. Is there an easier solution to this?
Your last error message indicates that you are still mixing lists and arrays. I'll try to recreate the situation:
Make a list of lists. Finding a sublist works just fine:
In [256]: ll=[[1,2,3],[4,5,6],[7,8,9]]
In [257]: ll.index([4,5,6])
Out[257]: 1
Make an array from it - it's 2d.
In [258]: la=np.array(ll)
In [259]: la
Out[259]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
It does not have an index method
In [260]: la.index([4,5,6])
...
AttributeError: 'numpy.ndarray' object has no attribute 'index'
Make it a list - but we get your ValueError:
In [265]: list(la).index([4,5,6])
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
That's because list(la) returns a list of arrays, and arrays produce multiple values in == expressions:
In [266]: list(la)
Out[266]: [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]
The correct way to produce a list from an array is tolist, which returns the original ll list of lists:
In [267]: la.tolist().index([4,5,6])
Out[267]: 1
If you are starting with a numpy array, you can get the result that you want by converting it to a list of lists before using the index() function, e.g.:
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
lst = [list(x) for x in arr]
print (lst.index([4,5,6]))
... which gives the expected output 1.

Index of multidimensional array

I have a problem using multi-dimensional vectors as indices for multi-dimensional vectors. Say I have C.ndim == idx.shape[0], then I want C[idx] to give me a single element. Allow me to explain with a simple example:
A = arange(0,10)
B = 10+A
C = array([A.T, B.T])
C = C.T
idx = array([3,1])
Now, C[3] gives me the third row, and C[1] gives me the first row. C[idx] then will give me a vstack of both rows. However, I need to get C[3,1]. How would I achieve that given arrays C, idx?
/edit:
An answer suggested tuple(idx). This work's perfectly for a single idx. But:
Let's take it to the next level: say INDICES is a vector where I have stacked vertically arrays of shape idx. tuple(INDICES) will give me one long tuple, so C[tuple(INDICES)] won't work. Is there a clean way of doing this or will I need to iterate over the rows?
If you convert idx to a tuple, it'll be interpreted as basic and not advanced indexing:
>>> C[3,1]
13
>>> C[tuple(idx)]
13
For the vector case:
>>> idx
array([[3, 1],
[7, 0]])
>>> C[3,1], C[7,0]
(13, 7)
>>> C[tuple(idx.T)]
array([13, 7])
>>> C[idx[:,0], idx[:,1]]
array([13, 7])

python max of list of arrays with lists [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
python max of list of arrays
I have a list of arrays like:
a = [array([ [6,2] , [6,2] ]),array([ [8,3],[8,3] ]),array([ [4,2],[4,2] ])]
I tried max(a) which returns the following error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I want it to return either a list or array like:
In: max(a)
Out: [[8,3],[8,3]]
I don't want to convert the inner arrays to list, because the size of the list is very big. Also I purposefully created like that to perform array operations.
In your example you can do something like this:
max(a, key=lambda i: i[0][0])
Depends on what result do you want (what key to use for sorting) you probably have to 'play' with indexes.
http://docs.python.org/2/library/functions.html#max
Anyay, not sure how your previous answer wasn't sufficient, but if you're dealing with numpy arrays, then why isn't the whole lot an array, instead of a list of arrays... Then just use the appropriate numpy function:
from numpy import array
a = array(
[
array([ [6,2], [6,2] ]),
array([ [8,3], [8,3] ]),
array([ [4,2], [4,2] ])
]
)
print a.max(axis=0)
#[[8 3]
# [8 3]]
what about:
max(a, key=lambda x:np.max(x))
# array ([[8, 3], [8, 3]])
please note: the fist max is 'normal max' and the second one is np's max... the logic here is that max uses np.max as it's key for comparison, np.max returns the highest number in the array.
is that what you want?

Argmax of numpy array returning non-flat indices

I'm trying to get the indices of the maximum element in a Numpy array.
This can be done using numpy.argmax. My problem is, that I would like to find the biggest element in the whole array and get the indices of that.
numpy.argmax can be either applied along one axis, which is not what I want, or on the flattened array, which is kind of what I want.
My problem is that using numpy.argmax with axis=None returns the flat index when I want the multi-dimensional index.
I could use divmod to get a non-flat index but this feels ugly. Is there any better way of doing this?
You could use numpy.unravel_index() on the result of numpy.argmax():
>>> a = numpy.random.random((10, 10))
>>> numpy.unravel_index(a.argmax(), a.shape)
(6, 7)
>>> a[6, 7] == a.max()
True
np.where(a==a.max())
returns coordinates of the maximum element(s), but has to parse the array twice.
>>> a = np.array(((3,4,5),(0,1,2)))
>>> np.where(a==a.max())
(array([0]), array([2]))
This, comparing to argmax, returns coordinates of all elements equal to the maximum. argmax returns just one of them (np.ones(5).argmax() returns 0).
To get the non-flat index of all occurrences of the maximum value, you can modify eumiro's answer slightly by using argwhere instead of where:
np.argwhere(a==a.max())
>>> a = np.array([[1,2,4],[4,3,4]])
>>> np.argwhere(a==a.max())
array([[0, 2],
[1, 0],
[1, 2]])

Acquiring the Minimum array out of Multiple Arrays by order in Python

Say that I have 4 numpy arrays
[1,2,3]
[2,3,1]
[3,2,1]
[1,3,2]
In this case, I've determined [1,2,3] is the "minimum array" for my purposes, as it is one of two arrays with lowest value at index 0, and of those two arrays it has the the lowest index 1. If there were more arrays with similar values, I would need to compare the next index values, and so on.
How can I extract the array [1,2,3] in that same order from the pile?
How can I extend that to x arrays of size n?
Thanks
Using the python non-numpy .sort() or sorted() on a list of lists (not numpy arrays) automatically does this e.g.
a = [[1,2,3],[2,3,1],[3,2,1],[1,3,2]]
a.sort()
gives
[[1,2,3],[1,3,2],[2,3,1],[3,2,1]]
The numpy sort seems to only sort the subarrays recursively so it seems the best way would be to convert it to a python list first. Assuming you have an array of arrays you want to pick the minimum of you could get the minimum as
sorted(a.tolist())[0]
As someone pointed out you could also do min(a.tolist()) which uses the same type of comparisons as sort, and would be faster for large arrays (linear vs n log n asymptotic run time).
Here's an idea using numpy:
import numpy
a = numpy.array([[1,2,3],[2,3,1],[3,2,1],[1,3,2]])
col = 0
while a.shape[0] > 1:
b = numpy.argmin(a[:,col:], axis=1)
a = a[b == numpy.min(b)]
col += 1
print a
This checks column by column until only one row is left.
numpy's lexsort is close to what you want. It sorts on the last key first, but that's easy to get around:
>>> a = np.array([[1,2,3],[2,3,1],[3,2,1],[1,3,2]])
>>> order = np.lexsort(a[:, ::-1].T)
>>> order
array([0, 3, 1, 2])
>>> a[order]
array([[1, 2, 3],
[1, 3, 2],
[2, 3, 1],
[3, 2, 1]])

Categories