2

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

This works great now if I want to split the original array into 2 and concatentate it horizontally but is there some code or something that I can use this if I want to say split a larger array than the one I described into 100 or 500 and concatenate it horizontally? is there an easy way to do this without having to use np.hstack a lot of times? Maybe a loop or something?
One option would be to use reduce function from functools but surely you can also use a for loop to concatenate the result one by one.
Would using a combination of np.split and hstack do the same thing? If so I may have a simple solution... array shape of x is (1800,144) then x = np.split(x, 2) which gives a shape of (2,900,144) then x = np.hstack(x) which gives x a shape of (900,288). If this is the same then I can split x into any number this way?
Ah. Clever, I didn't know np.hstack could take a list of multiple arrays but it seems that it can. np.hstack(np.split(x, 2)) would be a nicer solution.
1

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.

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.