3

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!

3
  • Do you mean a 2-d array? Commented Aug 10, 2015 at 22:19
  • Well an n x n or n x k dimensional array is what I am going for, I used a 2 x 2 as a simple example to reduce my computational time for the rest of my algorithm while in prototyping stage. Commented Aug 10, 2015 at 22:22
  • I think @Dunes question comes from your desired output being impossible. I think you are missing an extra pair of braces that would make it a 2-d array: array([[-0.6778734, -0.73517866], [-0.73517866, 0.6778734]]) Commented Aug 10, 2015 at 23:14

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, if I understand correctly you are saying that vstack only works for the n x 2 dimensional array? Otherwise, I should use concatenate.
I tried np.concatenate([y[1] for y in ei_rearranged]) which didn't work, but the np.array works and I assume will work for N dimensions.
You need to reshape the result of concatenate.
0

Try this:

import numpy as np

a = np.array([1, 2])
b = np.array([3, 4])
print(repr(np.vstack((a, b))))

Gives:

array([[1, 2],
       [3, 4]])

3 Comments

a = ei_rearranged[0][1] b = ei_rearranged[1][1] print(np.vstack((a,b))) <br/> gives the output: [[-0.6778734 -0.73517866] [-0.73517866 0.6778734 ]] <br/> where ei_rearranged = [(1.2840277121727839, array([-0.6778734, -0.73517866])), (0.049083398938327472, array([-0.73517866, 0.6778734 ]))] regardless I still need to define this recursively so that I can get n dimensions. Still weird it isn't including a comma between rows.
There are no commas anywhere. To get the commas, try: print(repr(arr)).
Gives: [array([-0.6778734 , -0.73517866]), array([-0.73517866, 0.6778734 ])]
0

You can form the desired 2D array given a list input_data of the form

input_data = [(1.2840277121727839, np.array([-0.6778734, -0.73517866])), 
              (0.049083398938327472, np.array([-0.73517866, 0.6778734 ]))]

via

nparr = np.array(list(row[1] for row in input_data))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.