lrnv
lrnv

Reputation: 1106

Reshape a variable numpy array

Suppose i have a numpy array u with a given shape, a a divisor d of the total number of entries in u. How can i fastly reshape u to be shaped (something,d) ?

The case where u is just a double should be included as well -> (1,1)

The case where u is empty should become a (0,d) shaped array

Upvotes: 0

Views: 45

Answers (1)

Nathan Furnal
Nathan Furnal

Reputation: 2410

You want to use reshape

u.reshape(-1, d)

There is no double in Python you do you mean float ?

In short :

import numpy as np

def div_reshape(arr, div):
    if arr.size == 0:
        return np.empty(shape=(0, div))
    elif arr.size == 1:
        return arr.reshape(1, 1)
    else:
        return arr.reshape(-1, d)

Upvotes: 1

Related Questions