Monolith
Monolith

Reputation: 1147

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.

Upvotes: 0

Views: 52

Answers (1)

user2357112
user2357112

Reputation: 280251

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.

Upvotes: 2

Related Questions