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

Numpy split that returns an ndarray

Is there a method analogous to numpy.split that returns a numpy.ndarray instead of a list? Assuming that the array splits evenly is fine (to prevent jagged arrays).

For example:

x = np.arange(9.0)
print(np.split(x, 3))
# [array([0.,  1.,  2.]), array([3.,  4.,  5.]), array([6.,  7.,  8.])]

print(np.???(x, 3))
# array([[0.,  1.,  2.], [3.,  4.,  5.], [6.,  7.,  8.]])

I’d rather not stack the list given from np.split together for performance reasons.

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

>Solution :

Sounds like you just want to reshape the array:

numpy.reshape(x, (3, -1))

Here, (3, -1) means 3 rows and an inferred number of columns.

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