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 this 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With