[Numpy-discussion] just curious...why does numpy.where() return tuples?
Robert Kern
robert.kern@gmail....
Mon Sep 8 16:01:30 CDT 2008
On Mon, Sep 8, 2008 at 15:14, Mark Miller <markperrymiller@gmail.com> wrote:
> Just for my own benefit, I am curious about this.
>
> I am running into problems because I need to archive the result (tuple)
> returned by a numpy.where statement. Pickle does not seem to like to deal
> with numpy scalars, and numpy's archiving functions (memmap) can't work on
> the tuple that gets returned by the where functions (I think).
>
> Is there a way around this? All would be good if the where statements
> actually returned numpy arrays instead.
I assume that you are talking about where()'s single-argument form
which is equivalent to nonzero() (which I recommend using instead).
The reason that it returns a tuple is that the result is intended to
be usable as a "fancy" index. E.g.
In [4]: x = random.randint(0, 2, [3, 10])
In [5]: x
Out[5]:
array([[1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[1, 1, 0, 0, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 1, 0, 1, 0, 1]])
In [6]: nonzero(x)
Out[6]:
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2]),
array([0, 6, 7, 0, 1, 4, 5, 6, 9, 0, 3, 5, 7, 9]))
In [7]: x[nonzero(x)]
Out[7]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
In Python, x[i,j] is implicitly x[(i,j)]. In order to support both
x[i,j] and x[some_array] (i.e., some_array is an array indexing on the
first axis only), we differentiate the inputs by type.
In [8]: x[array(nonzero(x))]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/Users/rkern/svn/shell/ShellIO/<ipython console> in <module>()
IndexError: index (6) out of range (0<=index<2) in dimension 0
If you want to use memmap or numpy.lib.format to save your data, then
I recommend explicitly casting to an array, and possibly converting
back to a tuple when you read it again.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list