[Tutor] Using numpy stack functions

Peter Otten __peter__ at web.de
Sat Feb 18 19:18:33 EST 2017


Bruhnke, Angelica wrote:

> I'm new to python programming so this question will probably be a no
> brainer for the experienced programmer. I'm trying to create a 10x10 array
> of zeros and then framing it with a border of ones.

So the final array is 12x12.

> 
> Output should look like this:
> 
> [[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
> 
> [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
> 
> 
> 
> This is the code I've written:
> 
> a = np.ones ((1,10))
> 
> print a
> 
> b = np.zeros ((10,10))
> 
> print b
> 
> c = np.vstack ((b,a))
> 
> print c
> 
> 
> This is my output:
> 
> [[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]]
> [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
> [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
>  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]]?
> 
> As you can see I have too many zeros once I stack them and don't have the
> ones framing the sides vertically.
> 
> Any guidance on where I'm going astray?

Below I'm using the dimensions 4x4. To get

1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1

you can vstack()

>>> np.vstack(([1,1], [[0,0],[0,0]], [1,1]))
array([[1, 1],
       [0, 0],
       [0, 0],
       [1, 1]])

and use hstack() to add ones to the sides:

>>> side = [[1]]*4
>>> np.hstack((side, mid, side))
array([[1, 1, 1, 1],
       [1, 0, 0, 1],
       [1, 0, 0, 1],
       [1, 1, 1, 1]])

The same with ones() and zeros(), put into a function:

>>> def frame(N):
...     horz = np.ones((1, N))
...     inner = np.zeros((N, N))
...     vert = np.ones((N+2, 1))
...     mid = np.vstack((horz, inner, horz))
...     return np.hstack((vert, mid, vert))
... 
>>> frame(3)
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
>>> frame(1)
array([[ 1.,  1.,  1.],
       [ 1.,  0.,  1.],
       [ 1.,  1.,  1.]])

Personally I'd probably use a simpler approach. Start with all ones and then 
set the inner values to 0:

>>> def frame2(N):
...     a = np.ones((N+2, N+2))
...     a[1: -1, 1: -1] = 0
...     return a
... 
>>> frame2(2)
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  1.],
       [ 1.,  1.,  1.,  1.]])




More information about the Tutor mailing list