I have this code that essentially splits a two-dimensional numpy array horizontally and makes a new two-dimensional numpy array out of it.
JavaScript
x
17
17
1
array1 = np.asarray([[1, 2, 3]])
2
array2 = np.asarray([[4, 5, 6]])
3
array3 = np.asarray([[7, 8, 9]])
4
5
concatenated = np.concatenate((array1, array2, array3), axis=0)
6
7
print(concatenated)
8
9
column_split = np.hsplit(concatenated, array1.size)
10
11
td_array = []
12
13
for array in column_split:
14
td_array.append(array.flatten())
15
16
print(np.asarray(td_array))
17
Output of my code:
JavaScript
1
7
1
[[1 2 3]
2
[4 5 6]
3
[7 8 9]]
4
[[1 4 7]
5
[2 5 8]
6
[3 6 9]]
7
How can I do this with less lines of code? I assume it could be very resource intensive, as soon as I apply this example to my larger task.
Advertisement
Answer
I suppose using numpy.hsplit in this case is not necessary and what I am trying to do is covered by the numpy.transpose function.
JavaScript
1
3
1
concatenated = np.concatenate((array1, array2, array3), axis=0)
2
td_array = concatenated.T
3
Thank you j1-lee for pointing this out.