[Numpy-discussion] newb question

Eric Firing efiring at hawaii.edu
Fri Sep 19 20:59:17 EDT 2008


paul taney wrote:
> Hi,
> 
> What am I doing wrong here?  The reshape doesn"t take.

Reshape does not act in place, it returns either a new view or a copy.

To reshape in place, you can assign to the shape attribute:

In [13]:a = np.arange(10)

In [14]:a.shape = (2,5)

In [15]:a
Out[15]:
array([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])

Eric

> 
> % cat test1.py
> import numpy as np
> 
> a = np.uint8([39, 39, 231,  239, 39, 231,  39, 39, 231, 
>               39, 39, 231,  239, 39, 231,  39, 39, 231,
>               39, 39, 231,  239, 39, 231,  39, 39, 231,
>               39, 39, 231,  239, 39, 231,  39, 39, 231,])
> a.reshape(3, 4, 3)
> print "a = %r" % (a)
> % 
> % python test1.py
> a = array([ 39, 39, 231, 239, 39, 231, 39, 39, 231,  
>             39, 39, 231, 239, 39, 231, 39, 39, 231,  
>             39, 39, 231, 239, 39, 231, 39, 39, 231,  
>             39, 39, 231, 239, 39, 231, 39, 39, 231], dtype=uint8)
> 
> ----
> 
> I am expecting:
> 
> a = array([[[39, 39, 231],  [239, 39, 231],  [39, 39, 231]], 
>            [[39, 39, 231],  [239, 39, 231],  [39, 39, 231]],
>            [[39, 39, 231],  [239, 39, 231],  [39, 39, 231]],
>            [[39, 39, 231],  [239, 39, 231],  [39, 39, 231]]], \
> dtype=np.uint8)
> 
> 
> paul
> 
> ----
> 
> def vanderWalt(a, f):
>     """thanks Stefan"""
>     RED, GRN, BLU = 0, 1, 2
>     bluemask = (a[...,BLU] > f*a[...,GRN]) & \
>                (a[...,BLU] > f*a[...,RED])
>     return np.array(bluemask.nonzero()).swapaxes(0,1)
> 
> 
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list