Home > Software engineering >  Dealing a multidimensional array and trying to concatenate as desired efficiently
Dealing a multidimensional array and trying to concatenate as desired efficiently

Time:12-29

What we have:

import numpy as np
a = np.array([[1,2],[11,22],[111,222],[1111,2222]])
b = np.array([[3],[33],[333],[3333]])
c = np.array([[4,5,6],[44,55,66],[444,555,666],[4444,5555,6666]])


data = [a,b,c]

What we want:

[[   1    2    3    4    5    6]
 [  11   22   33   44   55   66]
 [ 111  222  333  444  555  666]
 [1111 2222 3333 4444 5555 6666]]

Code to perform task as required:

new_arr=[]
for i in range(data[0].shape[0]):
  index = []
  for j in range(len(data)):
    index.extend(data[j][i])
  new_arr.append(index)
new_data = np.array(new_arr)
print(new_data)

Note: The solution is correct and gets the output as desired, but is there any other way to do it which is more efficient (faster)?

Question: I am performing a machine learning task and training a model needs to go through these loops a lot many times, making the training process very slow. Is there any way we can have a more efficient solution to this problem?

CodePudding user response:

numpy.hstack is what you are after.

import numpy as np
a = np.array([[1,2],[11,22],[111,222],[1111,2222]])
b = np.array([[3],[33],[333],[3333]])
c = np.array([[4,5,6],[44,55,66],[444,555,666],[4444,5555,6666]])

d = np.hstack([a, b, c])
print(d)

Output:

[[   1    2    3    4    5    6]
 [  11   22   33   44   55   66]
 [ 111  222  333  444  555  666]
 [1111 2222 3333 4444 5555 6666]]

CodePudding user response:

You can use np.concatenate with axis=1

new_arr = np.concatenate([a, b, c], axis=1)

Output:

>>> new_arr
array([[   1,    2,    3,    4,    5,    6],
       [  11,   22,   33,   44,   55,   66],
       [ 111,  222,  333,  444,  555,  666],
       [1111, 2222, 3333, 4444, 5555, 6666]])
  • Related