I have an array (Numpy of shape (100, 256, 256). How can I change this to (100, 256, 256,3)? I tried doing reshape but it doesn't work, Can anyone help me.

closed as off-topic by radouxju, Kersten, nmtoken, ArMoraer, xunilk Jan 3 '17 at 10:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – radouxju, Kersten, nmtoken, ArMoraer, xunilk
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Dstack is what you need – John Powell Jan 3 '17 at 7:46
  • But dstack takes two numpy array here i have a full final array and just want to change the dimensions. – Anup Panwar Jan 3 '17 at 8:04
  • 1
    you have 3 times more data in your second array, so you do need to duplicate some of your information. I guess that @JohnBarça suggestion is to use Dstack with your input array, three times. – radouxju Jan 3 '17 at 8:28
  • can you explain with an example? – Anup Panwar Jan 3 '17 at 8:32

It's impossible to do this kind of reshape [(100, 256, 256) -> (100, 256, 256,3)]. It's only possible a compatible reshape. This is an example:

>>>import numpy as np
>>>list = range(100*256*256)
>>>array = np.reshape(list, (100, 256, 256))
>>>array = np.reshape(list, (100, 256, 256, 3)) #your reshape: I got an error!
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/numpy-1.11.0-py2.7-linux-i686.egg/numpy/core/fromnumeric.py", line 224, in reshape
    return _wrapit(a, 'reshape', newshape, order=order)
  File "/usr/local/lib/python2.7/dist-packages/numpy-1.11.0-py2.7-linux-i686.egg/numpy/core/fromnumeric.py", line 48, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
ValueError: total size of new array must be unchanged
>>>array = np.reshape(list, (10, 256, 256, 10)) # compatible reshape: no error
  • How dstack will help me can you with an explain like .. last point was clear to me – Anup Panwar Jan 3 '17 at 9:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.