Why does argwhere return column vector?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does argwhere return column vector?

Mark Bakker
Hello list.

I don't understand why argwhere returns a column vector when I apply
it to a row vector:

>>> a = arange(5)
>>> argwhere(a>1)
array([[2],
       [3],
       [4]])

That seems odd and inconvenient. Any advantage that I am missing?

Mark
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Why does argwhere return column vector?

Stéfan van der Walt
Hi Mark

2008/5/27 mark <[hidden email]>:
> I don't understand why argwhere returns a column vector when I apply
> it to a row vector:
>
>>>> a = arange(5)
>>>> argwhere(a>1)
> array([[2],
>       [3],
>       [4]])

Each row of the result is a coordinate into your array.  So if you had

a = np.arange(12).reshape((4,3))

then you'd get

In [54]: np.argwhere(a>5)
Out[54]:
array([[2, 0],
       [2, 1],
       [2, 2],
       [3, 0],
       [3, 1],
       [3, 2]])

If you want to grab the elements larger than 5, just do

a[a>5]

Regards
Stéfan
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Why does argwhere return column vector?

Mark Bakker
OK, I get how it works for 2D arrays.

What I want to do is insert a number, say 7, before every value in the
array that is larger than, for example, 1.

Then I need to first find all the indices of values larger than 1, and
then I can do an insert:

>>> a = arange(5)
>>> i = argwhere( a>1 )
>>> insert(a,i[:,0],7)
array([0, 1, 7, 2, 7, 3, 7, 4])

Is there a better way to do this?
So for this instance, it is inconvenient that argwhere returns a
column vector. But I understand the issue for arrays with higher
dimensions.

Thanks for the explanation,

Mark

> Each row of the result is a coordinate into your array.  So if you had
>
> a = np.arange(12).reshape((4,3))
>
> then you'd get
>
> In [54]: np.argwhere(a>5)
> Out[54]:
> array([[2, 0],
>        [2, 1],
>        [2, 2],
>        [3, 0],
>        [3, 1],
>        [3, 2]])
>
> If you want to grab the elements larger than 5, just do
>
> a[a>5]
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Why does argwhere return column vector?

Stéfan van der Walt
Hi Mark

2008/5/27 mark <[hidden email]>:

> OK, I get how it works for 2D arrays.
>
> What I want to do is insert a number, say 7, before every value in the
> array that is larger than, for example, 1.
>
> Then I need to first find all the indices of values larger than 1, and
> then I can do an insert:
>
>>>> a = arange(5)
>>>> i = argwhere( a>1 )
>>>> insert(a,i[:,0],7)
> array([0, 1, 7, 2, 7, 3, 7, 4])
>
> Is there a better way to do this?

Inserting is slow, since a new array is allocated each time.  In the
case where you insert
at multiple indexes, it may be handled better, but I haven't timed it.

I would do it the following way:

import numpy as np
a = np.array([2,1,3,-1,2])

mask = a > 1
out = np.zeros(len(a) + sum(mask), dtype=int)
out.fill(7)
out[np.arange(len(a)) + mask.cumsum()] = a

Regards
Stéfan
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion