开发者

numpy array conversion to pairs

I'm not sure how it is called in python i think its a pair Anyway I have a huge numpy array, its format is like

FFnetlayer0 =   [ 0,  243,    9,  243,   18,  243,    4,  244,   13,  244, ....etc.]

I need thi开发者_如何学编程s numpy array format to be:

FFnetlayer0 =   [ (0,  243),    (9,  243),   (18,  243),    (4,  244),   (13,  244), .....]

Nodepairs needs to be between ( ) for building manually a neural net FFnet for python I'm building a huge neural net, so i use a function to create the array but i dont get the ( ) symbols included

conec =[]
for i in range (3):
    conec = numpy.append(conec,[(i,243),(i+9,243),(i+18,243)])
    d = 4
    conec = numpy.append(conec,[(i+d,244),(i+9+d,244),(i+18+d,244)])
    d = 7
    ...
    ..
    . 


One way is to just convert it to a two-dimensional NumPy array:

FFnetlayer0 = FFnetlayer0.reshape(-1, 2)

Now, accessing FFnetlayer0[i] for some i will give you a NumPy array with two entries.


Well your FFnetlayer0 isn't an numpy array, its still just a python list. You can slice it

from numpy import array
FFnetlayer = [0,243, 9,243, 18,243]
first_array = array(FFnetlayer[0::2]) # array([0,9,18])
second_array = array(FFnetlayer[1::2]) # array([243,243,243])

Unless its a matrix, I don't see the benefit of making it a two-dimensional array rather than two separate arrays.

You can also zip the two lists together if you didn't need to use them in numpy as a:

list_of_tuples = zip(FFnetlayer0[0::2], FFnetlayer0[1::2]) # [(0,243), (9,243), (18,243)]
array_of_list_of_tuples = array(list_of_tuples) # array([[0,243],[9,243],[18,243]])

For more into extended slices (or strides) see: http://docs.python.org/release/2.3/whatsnew/section-slices.html

For more on zip see: http://docs.python.org/library/functions.html#zip


Noticed in the comments that you created the numpy array via append. Noted that numpy.append doesn't append in-place, so isn't an efficient way of extending long arrays.

E.g.,

ff_list = [(0,243), (9,243)]
orig_id = id(ff_list)
for i in range(1000):
    ff_list.append((i,243))
    assert(orig_id == id(ff_list)) # Assertion is always True
ff_array = numpy.array(ff_list) # This will copy the list into an array; but does this only once rather than N times.

whereas

ff_array = numpy.array([(0,243), (9,243)])
last_id = id(ff_array)
for i in range(1000):
    ff_array = numpy.append(ff_array, (i,243))
    assert(last_id != id(ff_array)) # Assertion is True as array is always different.
    last_id = id(ff_array)

id tells the memory location of a python object. Note, this may not be a big difference unless your arrays are large and frequently appended. Also if at all possible its best to do array math to construct large arrays, rather than element by element for loops or appending.


a = [ 0,  243,    9,  243,   18,  243]
zip(a[::2],a[1::2])


The default behavior when appending to numpy arrays is to flatten them. But once you have a 2-d numpy array, you can append to it without flattening it; you just have to specify the axis argument:

>>> conec = []
>>> for i in range(3):
...     conec = numpy.append(conec,[(i,243),(i+9,243),(i+18,243)])
...     conec = conec.reshape(-1, 2)
...     d = 4
...     conec = numpy.append(conec,[(i+d,244),(i+9+d,244),(i+18+d,244)], axis=0)
... 
>>> conec
array([[  0,   0], [  0, 243], [  9, 243], [ 18, 243], [  4, 244],
       [ 13, 244], [ 22, 244], [  1, 243], [ 10, 243], [ 19, 243],
       [  5, 244], [ 14, 244], [ 23, 244], [  2, 243], [ 11, 243],
       [ 20, 243], [  6, 244], [ 15, 244], [ 24, 244]])

Still, it may be easier to simply fill the array and then reshape.

EDIT: As jimbob correctly points out, this isn't the most efficient way to build a large array. As an alternative, consider numpy.fromiter()


You can achieve this using numpy records.

It seems that all of your numbers are under 255; therefore I assume that you will not need a datatype that can handle numbers larger than that. Should you require another datatype, you can replace np.int8 with the appropriate type.

import numpy as np

structure_type = np.dtype([('field1', np.int8), ('field2', np.int8)])
values = np.array([(0, 243), (9, 243), (18, 243)], dtype=structure_type)
print "Entire array:", values
print "First field only:", values['field1']
print "Second element:", values[1]

Output:

Entire array: [(0, 243) (9, 243) (18, 243)]
First field only: [  0   9  18]
Second element: (9, 243)

As an aside, you do not seem to be using numpy arrays in your code; rather, you are using the function numpy.append to extend a Python list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜