Home > OS >  Numpy split that returns an ndarray
Numpy split that returns an ndarray

Time:01-19

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.

CodePudding user response:

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.

  • Related