K41F4r
K41F4r

Reputation: 1551

NumPy Array Conversion

I'm new to python and I'm trying to convert a (m,n,1) multidimensional array to (m,n) in a fast way, how can I go about it?

Also given a (m,n,k) array how can I split it to k (m,n) arrays? (each of the k members belongs to a different array)

Upvotes: 0

Views: 73

Answers (2)

nkaushik
nkaushik

Reputation: 71

To reshape array a you can use a.reshape(m,n).

To split array a along the depth dimension, you can use numpy.dsplit(a, a.shape[2]).

https://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html https://docs.scipy.org/doc/numpy/reference/generated/numpy.dsplit.html#numpy.dsplit

Upvotes: 3

randomwalker
randomwalker

Reputation: 1703

To reshape a NumPy Array arr with shape (m, n, 1) to the shape (m, n) simply use:

arr = arr.reshape(m, n)

You can get a list of (m, n)-shaped arrays out of a (m, n, k) shaped array arr_k by:

array_list = [arr_k[:, :, i] for i in range(arr_k.shape[2])]

Upvotes: 1

Related Questions