2

I want to "join" two 2-dimensional numpy arrays of same shape to create a dimensional numpy array. I can easily do this using loop but I am looking for faster way. Here is the toy example. The two numpy arrays are:

data1 = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
data2 = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])*100

data1

> array([[ 1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10],
         [11, 12, 13, 14, 15]])

data2

> array([[ 100,  200,  300,  400,  500],
         [ 600,  700,  800,  900, 1000],
         [1100, 1200, 1300, 1400, 1500]])

Both have a shape (3,5). I want to create (3,5,2) shape numpy array. That is:

data3 = []
for irow in range(data1.shape[0]):
    data3_temp = []
    for icol in range(data1.shape[1]):
        data3_temp.append([data1[irow,icol],
                           data2[irow,icol]])
    data3.append(data3_temp)
data3 = np.array(data3)

data3.shape
> (3, 5, 2)

data3

>array([[[   1,  100],
    [   2,  200],
    [   3,  300],
    [   4,  400],
    [   5,  500]],

   [[   6,  600],
    [   7,  700],
    [   8,  800],
    [   9,  900],
    [  10, 1000]],

   [[  11, 1100],
    [  12, 1200],
    [  13, 1300],
    [  14, 1400],
    [  15, 1500]]])

Please let me know.

3 Answers 3

3

Use numpy.dstack to stack arrays in sequence depth wise (along third axis): https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.dstack.html

import numpy as np

data1 = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
data2 = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])*100

print np.dstack((data1,data2))

Output:

[[[   1  100]
  [   2  200]
  [   3  300]
  [   4  400]
  [   5  500]]

 [[   6  600]
  [   7  700]
  [   8  800]
  [   9  900]
  [  10 1000]]

 [[  11 1100]
  [  12 1200]
  [  13 1300]
  [  14 1400]
  [  15 1500]]]
Sign up to request clarification or add additional context in comments.

2 Comments

And now with a bit more flexibility, np.stack((), axis=-1).
@hpaulj nice trick! Particularly for this question, np.stack((), axis=2) is much clear. But, using axis=-1 is applicable in a general setting :)
1

one method:

data3= np.zeros([ data1.shape[0],data1.shape[1], 2])

data3[:,:,0],data3[:,:,1] = data1,data2

print data3.shape
print data3

results in

(3L, 5L, 2L)

>array([[[   1,  100],
    [   2,  200],
    [   3,  300],
    [   4,  400],
    [   5,  500]],

   [[   6,  600],
    [   7,  700],
    [   8,  800],
    [   9,  900],
    [  10, 1000]],

   [[  11, 1100],
    [  12, 1200],
    [  13, 1300],
    [  14, 1400],
    [  15, 1500]]])

Comments

1

Or you can use the r_ concatenator:

np.r_['2,3,0', data1, data2]
# array([[[   1,  100],
          [   2,  200],
          [   3,  300],
          [   4,  400],
          [   5,  500]],

         [[   6,  600],
          [   7,  700],
          [   8,  800],
          [   9,  900],
          [  10, 1000]],

         [[  11, 1100],
          [  12, 1200],
          [  13, 1300],
          [  14, 1400],
          [  15, 1500]]])

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.