← Back to team overview

larry-discuss team mailing list archive

Re: Index by label support added to experimental branch

 

On Mon, Feb 8, 2010 at 12:37 PM, Keith Goodman <kwgoodman@xxxxxxxxx> wrote:
> On Mon, Feb 8, 2010 at 12:27 PM, Keith Goodman <kwgoodman@xxxxxxxxx> wrote:
>> I made a branch called index-by-label:
>>
>> https://code.launchpad.net/~kwgoodman/larry/index-by-label
>>
>> and added support for indexing by label. It is all self contained in
>> one method: deflarry.py(lix). So easy to try.
>>
>> Here are a few one-off tests:
>>
>>>> y = la.larry([[1,2],[3,4]], [['a', 'b'], ['c', 'd']])
>>
>>>> y.lix[['a']]
>> label_0
>>    c
>>    d
>> x
>> array([1, 2])
>>
>>>> y.lix[['b', 'a']]
>> label_0
>>    b
>>    a
>> label_1
>>    c
>>    d
>> x
>> array([[3, 4],
>>       [1, 2]])
>>
>>>> y.lix[['a']:]
>> label_0
>>    a
>>    b
>> label_1
>>    c
>>    d
>> x
>> array([[1, 2],
>>       [3, 4]])
>>
>>>> y.lix[:['b']]
>> label_0
>>    a
>> label_1
>>    c
>>    d
>> x
>> array([[1, 2]])
>>
>>>> y.lix[['a'], ['c']:]
>> label_0
>>    c
>>    d
>> x
>> array([1, 2])
>>
>>>> y.lix[['a'], ['d', 'c']]
>> label_0
>>    d
>>    c
>> x
>> array([2, 1])
>>
>>>> y.lix['a':]  # <--- expected to crash
>> ValueError: The start element of a slice must be a list.
>
> I think the only thing left to add is the conversion of fancy indexing
> to rectangular:
>
>>> y.lix[['a', 'b'], ['d', 'c']]
> IndexError: tuple index out of range
>
> How do I identify rectangular indexing? If more than one list with
> more than one element are present anywhere in the index, then it is
> rectangular and I need to do your trick np.array(first list)[:,None]?

Ah,

>> x = np.arange(24).reshape(2,3,4)
>> x[[0,1], [0,1,2], [0,1,2,3]]
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>> x[np.array([0,1])[:,None,None], np.array([0,1,2])[:,None], [0,1,2,3]]

array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])



References