Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - access elements from array of arrays, call function to execute array of arrays

If I have an array like:

a = np.array([ [A(2,3 , np.array([[C(2,3)], [C(5,6)] ]))],
               [A(4,5 , np.array([[C(1,2)],[C(9,7)]]))]
             ])

with other class instances, how can I access all the elements?

For example,

for idx,x in np.ndenumerate(a):

    print('Index :{0}'.format(idx))
    print('Elmt:  {0}'.format(x.the_c[idx].t))

returns:

Index :(0, 0)
Elmt: 2
Index :(1, 0)
Elmt: 9

so only 2 indices and 2 elements instead of 4.

Normally, I have to call another ndenumerate but I am not sure how to call it or if there is a better (more efficient) way.

The code:

import numpy as np

class A():
    def __init__(self, a, b,the_c):
        self.a = a
        self.b = b
        self.the_c = the_c

    def methodA(self):
        return self.the_c


class B():

    def __init__(self, c, d, the_a):
        self.c = c
        self.d = d
        self.the_a = the_a

    def evaluate(self):
        for idx, x in np.ndenumerate(self.the_a):
            x.methodA()
            return x.the_c

class C():
    def __init__(self,t,y):
        self.t = t
        self.y = y

And if I want to evaluate the a array by calling a function, how can I call it?

def evaluate(self):
    for idx, x in np.ndenumerate(self.the_a):
        x.methodA()
        return x.the_c

OR

def evaluate(self):
      for idx, x in np.ndenumerate(self.the_a):

           the_inst = A(x.a, x.b, x.self.the_c)
           the_inst.methodA()
           return the_inst.the_c

So, the evaluate method in class B will be the only one that gets called and it will execute the many A instances which contain the many C instances.

a = np.array([ [A(2,3 , np.array([[C(2,3)], [C(5,6)] ]))],
               [A(4,5 , np.array([[C(1,2)],[C(9,7)]]))]
             ])
b = B(1,2,a).evaluate()

for idx, x in np.ndenumerate(b):
    print(x.t)

which gives 2 and 5 instead of 2,5,1,9.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

a is 2x1 array containing 2 objects, both of class A:

In [162]: a
Out[162]: 
array([[<__main__.A object at 0xab20030c>],
       [<__main__.A object at 0xab20034c>]], dtype=object)

I can cast the method call as function with:

def foo(an_A):
     return an_A.methodA()

In [164]: a.shape
Out[164]: (2, 1)
In [165]: foo(a[0,0])
Out[165]: 
array([[<__main__.C object at 0xab2001cc>],
       [<__main__.C object at 0xab2002ec>]], dtype=object)

which is another 2x1 array, this time containing C objects. Is there a particular reason why these are all (2,1) as opposed to (2,)?

frompyfunc is a handy tool for applying a function to all elements of any array, especially when both inputs and outputs are object arrays:

In [166]: f=np.frompyfunc(foo,1,1)
In [167]: f(a)
Out[167]: 
array([[ array([[<__main__.C object at 0xab2001cc>],
       [<__main__.C object at 0xab2002ec>]], dtype=object)],
       [ array([[<__main__.C object at 0xab20096c>],
       [<__main__.C object at 0xab2003cc>]], dtype=object)]], dtype=object)

Like a, this is (2,1), but now it contains the 2 (2,1) C arrays.

I can convert it into a (4,1) array of C objects with.

In [176]: np.concatenate(f(a)[:,0])
Out[176]: 
array([[<__main__.C object at 0xab2001cc>],
       [<__main__.C object at 0xab2002ec>],
       [<__main__.C object at 0xab20096c>],
       [<__main__.C object at 0xab2003cc>]], dtype=object)

np.r_[tuple(f(a)[:,0])] also does this. https://stackoverflow.com/a/42091616/901925

We could apply the concatenate to the (2,1) f(a) array, but the result is messier.

You could also use ndenumerate to produce the same thing as f(a). First you need to create an array that will receive the individual foo(x) results:

In [186]: res=np.empty(a.shape, object)
In [187]: for idx,x in np.ndenumerate(a):
     ...:     res[idx] = foo(x)
     ...:     
In [188]: res
Out[188]: 
array([[ array([[<__main__.C object at 0xab2001cc>],
       [<__main__.C object at 0xab2002ec>]], dtype=object)],
       [ array([[<__main__.C object at 0xab20096c>],
       [<__main__.C object at 0xab2003cc>]], dtype=object)]], dtype=object)

On a 1d a or a[:,0] we can use a simple list comprehension:

In [189]: [foo(x) for x in a[:,0]]
Out[189]: 
[array([[<__main__.C object at 0xab2001cc>],
        [<__main__.C object at 0xab2002ec>]], dtype=object),
 array([[<__main__.C object at 0xab20096c>],
        [<__main__.C object at 0xab2003cc>]], dtype=object)]
In [190]: np.array([foo(x) for x in a[:,0]])
Out[190]: 
array([[[<__main__.C object at 0xab2001cc>],
        [<__main__.C object at 0xab2002ec>]],

       [[<__main__.C object at 0xab20096c>],
        [<__main__.C object at 0xab2003cc>]]], dtype=object)
In [191]: _.shape
Out[191]: (2, 2, 1)

I'm tempted to go back a make foo return an_A.method()[:,0], or simplify a:

In [192]: a1 = np.array([ A(2,3 , np.array([C(2,3), C(5,6) ])),
     ...:                A(4,5 , np.array([C(1,2),C(9,7)]))
     ...:              ])
In [195]: np.array([foo(x) for x in a1])    # (2,2) result
Out[195]: 
array([[<__main__.C object at 0xab1aefcc>,
        <__main__.C object at 0xab1ae94c>],
       [<__main__.C object at 0xab1ae0cc>,
        <__main__.C object at 0xab1eb2ac>]], dtype=object)

If I give your classes repr methods

def __repr__(self):
    return 'A<{0.a},{0.b},{0.the_c}>'.format(self)
def __repr__(self):
    return 'C<{0.t},{0.y}>'.format(self)

then a displays as

array([[A<2,3,[[C<2,3>]
 [C<5,6>]]>],
       [A<4,5,[[C<1,2>]
 [C<9,7>]]>]], dtype=object)

and f(a) as

[[array([[C<2,3>],
       [C<5,6>]], dtype=object)]
 [array([[C<1,2>],
       [C<9,7>]], dtype=object)]]

With a similar repr for B, b = B(1,2,a) displays as

B<1,2,[[A<2,3,[[C<2,3>]
 [C<5,6>]]>]
 [A<4,5,[[C<1,2>]
 [C<9,7>]]>]]>

and B(1,2,fA(a)).the_a as (the equivalent of implementing the B.evaluate with f:

[[array([[C<2,3>],
       [C<5,6>]], dtype=object)]
 [array([[C<1,2>],
       [C<9,7>]], dtype=object)]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...