Numpy split that returns an ndarray

Question:

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.

Asked By: Monolith

||

Answers:

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.

Answered By: user2357112
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.