How to change Numpy array shape [closed]

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.

• 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
• 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