2

I have a tuple which contains many tuples. Each tuple within my main tuple has two elements -- the first element is an array with a shape of (700,) and the second element is a integer.

Here is a small representation of my tuple:

x =( (np.array[3,3,3],1), (np.array[4,4,4],2), (np.array[5,5,5],3))

I'm looking to combine all the arrays into one big matrix, and all the integers into one column vector, which all fit into one tuple.

So my output should be something like this:

y= (np.array([[3,3,3],[4,4,4], [5,5,5]]),   np.array([1,2,3]))

One tuple with the first element as an array with shape (3,3), and the second element as an array with a shape of (3,)

I'm assuming we can use one of numpy's stack methods but I can't wrap my head around how to access all elements of the tuples to do so.

Thank you.

8
  • So, the output would be a regular 3x4 shaped array, right? Commented Sep 8, 2017 at 4:03
  • No the output would be a tuple with two arrays -- 3x3 and a 3. Hopefully that makes sense. Commented Sep 8, 2017 at 4:04
  • Could you write down the actual expected output, because what you have shown isn't a valid one. Commented Sep 8, 2017 at 4:06
  • I will look it over. I thought I wrote everything correctly. I'm dealing with a very large data set so I tried to create a sample. Commented Sep 8, 2017 at 4:08
  • @Divakar I got confused when I read it too; ignore the [ and ]s and just pretend they wrote it with the spacing as two separate arrays. Commented Sep 8, 2017 at 4:10

2 Answers 2

3
>>> x = ((np.array([3,3,3]),1), (np.array([4,4,4]),2), (np.array([5,5,5]),3))
>>> y = (np.array([e for e, _ in x]), np.array([i for _, i in x]))
(array([[3, 3, 3],
        [4, 4, 4],
        [5, 5, 5]]), array([1, 2, 3]))

Or, without comprehensions:

>>> map(np.array, zip(*x))
[array([[3, 3, 3],
        [4, 4, 4],
        [5, 5, 5]]), array([1, 2, 3])]
Sign up to request clarification or add additional context in comments.

2 Comments

Map works brilliantly here, +1. I'll delete my answer since the OP changed his example from lists to arrays.
A list comprehension version of the map: [np.array(i) for i in zip(*x)]. The key is that zip(*x) is effectively a list transpose.
1

A structured array approach:

First the tuple, with corrected syntax:

In [392]: x =( (np.array([3,3,3]),1), (np.array([4,4,4]),2), (np.array([5,5,5]),3))

Input to structured array is a list of tuples, each tuple containing data for a record:

In [393]: arr=np.array(list(x), np.dtype('3i,i'))
In [394]: arr
Out[394]: 
array([([3, 3, 3], 1), ([4, 4, 4], 2), ([5, 5, 5], 3)],
      dtype=[('f0', '<i4', (3,)), ('f1', '<i4')])

Accessing the array by field name:

In [395]: arr['f0']
Out[395]: 
array([[3, 3, 3],
       [4, 4, 4],
       [5, 5, 5]], dtype=int32)
In [396]: arr['f1']
Out[396]: array([1, 2, 3], dtype=int32)

or as a tuple of fields:

In [397]: (arr['f0'], arr['f1'])
Out[397]: 
(array([[3, 3, 3],
        [4, 4, 4],
        [5, 5, 5]], dtype=int32), array([1, 2, 3], dtype=int32))

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.