Reputation: 709
I have an array (a) that is the shape (1800,144)
where a[0:900,:]
are all real numbers and the second half of the array a[900:1800,:]
are all zeros. I want to take the second half of the array and put it next to the first half horizontally and push them together so that the new array shape (a) will be (900,288)
and the array, a, will look like this:
[[1,2,3,......,0,0,0],
[1,2,3,......,0,0,0],
...
]
if that makes sense.
when I try to use np.reshape(a,(900,288))
it doesn't exactly do what I want. It makes the array all real numbers from a[0:450,:]
and zeros from a[450:900,:]
. I want all of the zeros to be tacked onto the second dimension so that from a[0:900,0:144]
is all real numbers and a[0:900,144:288]
are all zeros.
Is there an easy way to do this?
Upvotes: 2
Views: 1235
Reputation: 2332
sorry, this is too big for a comment, so I will post it here. If you have a long array and you need to split it and reassemble it, there are other methods that can accomplish this. This example shows how to assemble an equally sized sequence of numbers into a single array.
a = np.arange(100)
>>> b = np.split(a,10)
>>> c = np.c_[b]
>>> c
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
so you can split a sequence easily and reassemble it easily. You could reorder the sequence of stacking if you want. Perhaps that is easier to show in this sequence.
d = np.r_[b[5:],b[:5]].ravel()
>>> d
array([50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
This example simply shows that you can take the last five split sequences and throw them into the front of the pile. It shouldn't take long to figure out that if you have a series of values, even of unequal length, you can place them in a list and reassemble them using np.c_ and np.r_ convenience functions (np.c_ would normally expect equal sized arrays).
So not a solution to your specific case perhaps, but some suggestions on how to reassemble samples in various ways.
Upvotes: 1
Reputation: 215107
You can use numpy.hstack()
to concatenate the two arrays:
import numpy as np
np.hstack([a[0:900,], a[900:1800,]])
If you'd like to split the array into more than two sub arrays, you can combine the usage of np.split
and np.hstack
, as @HM14 has commented:
np.hstack(np.split(a, n)) # assuming len(a) % n == 0 here
Upvotes: 1