Wednesday 15 May 2013

python - When to use .shape and when to use .reshape? -



python - When to use .shape and when to use .reshape? -

i ran memory problem when trying utilize .reshape on numpy array , figured if somehow reshape array in place great.

i realised reshape arrays changing .shape value. unfortunately when tried using .shape 1 time again got memory error has me thinking doesn't reshape in place.

i wondering when utilize 1 when utilize other?

any help appreciated.

if want additional info please allow me know.

edit:

i added code , how matrix want reshape created in case important.

change n value depending on memory.

import numpy np n = 100 = np.random.rand(n, n) b = np.random.rand(n, n) c = a[:, np.newaxis, :, np.newaxis] * b[np.newaxis, :, np.newaxis, :] c = c.reshape([n*n, n*n]) c.shape = ([n, n, n, n])

edit2: improve representation. apparently transpose seems of import changes arrays c-contiguous f-contiguous, , resulting multiplication in above case contiguous while in 1 below not.

import numpy np n = 100 = np.random.rand(n, n).t b = np.random.rand(n, n).t c = a[:, np.newaxis, :, np.newaxis] * b[np.newaxis, :, np.newaxis, :] c = c.reshape([n*n, n*n]) c.shape = ([n, n, n, n])

numpy.reshape re-create info if can't create proper view, whereas setting shape raise error instead of copying data.

it not possible alter shape of array without copying data. if want error raise if info copied, should assign new shape shape attribute of array.

python arrays numpy

No comments:

Post a Comment