[Numpy-discussion] Any help from Numpy community?

Birdada Simret birdada85 at gmail.com
Thu Mar 14 10:51:15 EDT 2013


Hi Ryan,Thank you very much indeed, I'm not sure if I well understood your
code, let say, for the example array matrix given represents  H3C-CH3
connection(bonding).
the result from your code is:
Rows:    [0 0 0 0 1 1 1]  # is these for C indices?
Columns: [1 2 3 4 5 6 7]   # is these for H indices? but it shouldn't be 6
H's?
Atomic Distances: [ 1.  1.  1.  1.  1.  1.  1.] # ofcourse this is the
number of connections or bonds.

In fact, if I write in the form of dictionary: row indices as keys and
column indices as values,
{0:1, 0:2, 0:3, 0:4, 1:5, 1:6, 1:7}, So, does it mean C[0] is connected to
H[1], C[0] is connected to H[2] , H[1],....,C[1] is connected to H[7]?  But
I have only 6 H's and two C's  in this example (H3C-CH3)

I have tried some thing like: but still no luck ;(
import numpy as np
from collections import defaultdict
dict = defaultdict(list)
x=....2d numpy array
I = x.shape[0]
J = x.shape[1]
d={}
for i in xrange(0, I, 1):
  for j in xrange(0, J, 1):
     if x[i,j] > 0:
        dict[i].append(j)
# the result is:
dict:  {0: [1, 2, 3, 4], 1: [0, 5, 6, 7], 2: [0], 3: [0], 4: [0], 5: [1],
6: [1], 7: [1]})
keys: [0, 1, 2, 3, 4, 5, 6, 7]
values:  [[1, 2, 3, 4], [0, 5, 6, 7], [0], [0], [0], [1], [1], [1]]

#The H indices can be found by
 H_rows = np.nonzero(x.sum(axis=1)== 1)
result=>H_rows : [2, 3, 4, 5, 6, 7]  # six H's
I am trying to connect this indices with the dict result but I am confused!
So, now I want to produce a dictionary or what ever to produce results as:
 H[2] is connected to C[?]

                                      H[3] is connected to C[?]

                                      H[4] is connected to C[?], .....
Thanks for any help
                                               .


On Thu, Mar 14, 2013 at 2:26 PM, Ryan <rnelsonchem at gmail.com> wrote:

>
> >
> > Birda,
> >
> > I think this will get you some of the way there:
> >
> > import numpy as np
> > x = ... # Here's your 2D atomic distance array
> > # Create an indexing array
> > index = np.arange( x.size ).reshape( x.shape )
> > # Find the non-zero indices
> > items = index[ x != 0 ]
> > # You only need the first half because your array is symmetric
> > items = items[ : items.size/2]
> > rows = items / x.shape[0]
> > cols = items % x.shape[0]
> > print 'Rows:   ', rows
> > print 'Columns:', cols
> > print 'Atomic Distances:', x[rows, cols]
> >
> > Hope it helps.
> >
> > Ryan
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion <at> scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
>
> Whoops.
> That doesn't quite work. You shouldn't drop half the items array like that.
> This will work better (maybe ?):
>
> import numpy as np
> x = ... # Here's your 2D atomic distance array
> index = np.arange( x.size ).reshape( x.shape )
> items = index[ x != 0 ]
> rows = items / x.shape[0]
> cols = items % x.shape[0]
> # This index mask should take better advantage of the array symmetry
> mask = rows < cols
> print 'Rows:   ', rows[mask]
> print 'Columns:', cols[mask]
> print 'Atomic Distances:', x[rows[mask], cols[mask]]
>
> Ryan
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130314/c391e764/attachment.html>


More information about the NumPy-Discussion mailing list