Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively defining an N-dimensional numpy array

I am trying to recursively define a numpy array of N dimensions. After researching for several hours, I have came across a couple of ways this might work (np.append and np.concatenate), however neither of these has given me the desired output. I've been getting either: [-0.6778734 -0.73517866 -0.73517866 0.6778734 ] (1-d array) or [array([-0.6778734 , -0.73517866]), array([-0.73517866, 0.6778734 ])] (a list of arrays)

My Input:

[(1.2840277121727839, array([-0.6778734, -0.73517866])), 
    (0.049083398938327472, array([-0.73517866, 0.6778734 ]))]

Desired output:

array([-0.6778734, -0.73517866], [-0.73517866, 0.6778734])

Is it possible to create a numpy array from arrays, because converting them to lists and back to arrays seems computationally inefficient?

Thanks in advance!

like image 874
kearnsw Avatar asked Jul 11 '26 08:07

kearnsw


1 Answers

Your input is a list of tuples, each tuple consisting of a number and an array. For some reason you want to throw away the number, and just combine the arrays into a larger array - is that right?

In [1067]: x=[(1.2840277121727839, np.array([-0.6778734, -0.73517866])), 
    (0.049083398938327472, np.array([-0.73517866, 0.6778734 ]))]

In [1068]: x
Out[1068]: 
[(1.2840277121727839, array([-0.6778734 , -0.73517866])),
 (0.04908339893832747, array([-0.73517866,  0.6778734 ]))]

A list comprehension does a nice job of extracting the desired elements for the tuples:

In [1069]: [y[1] for y in x]
Out[1069]: [array([-0.6778734 , -0.73517866]), array([-0.73517866,  0.6778734 ])]

and vstack is great for combining arrays into a larger one.

In [1070]: np.vstack([y[1] for y in x])
Out[1070]: 
array([[-0.6778734 , -0.73517866],
       [-0.73517866,  0.6778734 ]])

vstack is just concatenate with an added step that ensures the inputs are 2d.

np.array([y[1] for y in x]) also works, since you are adding a dimension.

I'm assuming that array([-0.6778734, -0.73517866], [-0.73517866, 0.6778734]) has a typo - that it is missing a set of []. The 2nd parameter to np.array is the dtype, not another list.

Note that both np.array and np.concatentate take a list. It can be list of lists, or list of arrays. It doesn't make much difference. And at this stage don't worry about computational efficiency. Any time you combine the data from 2 or more arrays there will be copying. Arrays have a fixed size, and can't 'grow' without making a new copy.


In [1074]: np.concatenate([y[1] for y in x]).reshape(2,2)
Out[1074]: 
array([[-0.6778734 , -0.73517866],
     [-0.73517866,  0.6778734 ]])

Lists are effectively 1d, so np.concatenate joins them on that dimension, producing a 4 element 1d array. reshape corrects that. vstack makes them both (1,2) and does a concatenate on the 1st dimension.

Another expression that joins the arrays on a new dimension:

np.concatenate([y[1][None,...] for y in x], axis=0)

The [None,...] adds a new dimension at the start.

like image 153
hpaulj Avatar answered Jul 13 '26 20:07

hpaulj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!