Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dealing a multidimensional array and trying to concatenate as desired efficiently

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading