1

I am enrolled in a beginners class Python at my university. We've got a programming assignment which I'm stuck on.

We got an assignment to find a route between two point in a map, the map is an 2D numpy array. One of the first tasks is to convert the array consisting of free road(1) and buildings(0) to an array where all the locations surrounding a building (left, right, below, above) are converted to parking spots (-1)

I started with writing a function to generate a 2D numpy array in the right size:

def get_map():

    map = np.random.randint(2, size=(12, 10))

    return map

Now I want to write another function that takes the map as an argument, and returns the map where the parking spots are converted from 1's to -1's.

def adjusted_map(map): 

    map_adjusted = 

    return map_adjusted

I am mostly stuck on the elements above and below the 0's. Left and right I can do because that's no different from 1D arrays or normal lists, strings etc. Im sorry if this is a stupid question but I looked into the numpy documentation about indexing, slicing and iterating over numpy arrays, but I could not find a solution for my problem.

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
Laura
  • 45
  • 7
  • so you need to find the zeros and then mark the 4 contiguous positions as -1? – Yuca Oct 01 '18 at 19:59
  • Yes, if the element is a zero, the elements left from, right from, below and above this zero should be changed into -1. But only in case those elements are 1. If the element is also a zero, it should remain a zero because that represents two buildings standing next to each other. – Laura Oct 01 '18 at 20:03
  • ok so step one is to find all the indexes where value is 1, step two is two make -1 all the neighbors – Yuca Oct 01 '18 at 20:03

2 Answers2

2

Here is one simple way using standard numpy techniques:

1) Make a map consisting of 3x3 blocks with 80% road

>>> map_ = np.kron(np.random.random((6, 5)) < 0.8, np.ones((3, 3), int))
>>> map_
array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0]])

2) Invert the map and zero-pad it

>>> helper = np.pad(1-map_, ((1, 1), (1, 1)), 'constant')
>>> helper
array([[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, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

3) Cut out shifted versions (up, down, left, right) of the inverse map, use (bitwise) or to find all neighbors of buildings, use and to only keep neighbors that are road

>>> parking = map_ & (helper[2:, 1:-1] | helper[:-2, 1:-1] | helper[1:-1, 2:] | helper[1:-1, :-2])
>>> parking
array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0]])

4) Mark the parking spots on the map

>>> result = map_ - 2*parking
>>> result
array([[ 1,  1, -1,  0,  0,  0,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [ 1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1,  1,  1,  1],
       [-1, -1, -1,  0,  0,  0, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1, -1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1,  1, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0, -1,  1,  1, -1, -1, -1,  1,  1,  1,  1,  1,  1],
       [-1, -1, -1,  1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [ 1,  1,  1,  1,  1, -1,  0,  0,  0, -1,  1,  1,  1,  1,  1],
       [-1, -1, -1,  1,  1, -1,  0,  0,  0, -1,  1,  1, -1, -1, -1],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1, -1,  0,  0,  0],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1, -1,  0,  0,  0],
       [ 0,  0,  0, -1,  1, -1,  0,  0,  0, -1,  1, -1,  0,  0,  0]])

5) Bonus: Prettify

>>> symbols = np.array(('x', '.', 'P'))
>>> rowtype = f'U{map_.shape[1]}'
>>> rowtype
'U15'
>>> print('\n'.join(symbols[map_].view(rowtype).ravel()))
...xxxxxx......
...xxxxxx......
...xxxxxx......
...xxx.........
...xxx.........
...xxx.........
xxx...xxx......
xxx...xxx......
xxx...xxx......
xxx............
xxx............
xxx............
......xxx......
......xxx......
......xxx......
xxx...xxx...xxx
xxx...xxx...xxx
xxx...xxx...xxx
>>> print('\n'.join(symbols[result].view(rowtype).ravel()))
..PxxxxxxP.....
..PxxxxxxP.....
..PxxxxxxP.....
..PxxxPPP......
..PxxxP........
PPPxxxPPP......
xxxPPPxxxP.....
xxxP.PxxxP.....
xxxP.PxxxP.....
xxxP..PPP......
xxxP...........
xxxP..PPP......
PPP..PxxxP.....
.....PxxxP.....
PPP..PxxxP..PPP
xxxP.PxxxP.Pxxx
xxxP.PxxxP.Pxxx
xxxP.PxxxP.Pxxx
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • @Yuca I think the best way of understanding it is to look at the shifted bits `helper[2:, 1:-1]`, etc. ideally next to each other and to the whole non-sliced `helper`. Please try this and if afterwards there are still questions feel free to ask me again. – Paul Panzer Oct 01 '18 at 21:40
  • have to say that was not helpful, let me figure out a way to ask the right question to you – Yuca Oct 01 '18 at 21:48
  • 1
    @Yuca Oops, sorry. Ok, it would help if you could be more specific, but let me try: For example `helper[2:, 1:-1]` this has the same shape as `map_`. It marks upper neighbors of buildings, i.e. it has a `1` everywhere where the cell below in `map_` has a `0`. By `or`ing together the four shifted `helper`s we get _all_ (up down left right) neighbors of buildings. This still includes neighbors that are building themselves. To discard them we `and` with the original `map_`. – Paul Panzer Oct 01 '18 at 21:57
  • ok, this makes it easier to grasp. thank you for your patience! – Yuca Oct 01 '18 at 21:58
  • Thank you for your answer. Could you explain why you use the 3x3 blocks and the condition that 80% is road? I don't know if I'm allowed to set conditions like that in my assignment. I think my map should be totally random. – Laura Oct 02 '18 at 06:47
  • @Laura That was just to make it look nicer. You can use a totally random `map_` instead, the rest of the code (steps 2-5) should work unchanged. – Paul Panzer Oct 02 '18 at 06:51
  • In addition to my question: the code does work when I use a array in the size I need, thank you for taking the time. – Laura Oct 02 '18 at 07:08
  • Yes indeed, it works. Thanks for your additional explanation. – Laura Oct 02 '18 at 07:09
0

so you need something like this:

import numpy as np

mapper = np.random.randint(2, size=(12, 10))
buildings = np.nonzero(mapper)

nonzero_row = buildings[0]
nonzero_col = buildings[1]

for row, col in zip(nonzero_row, nonzero_col):
    if row > 0:
        mapper[row-1, col] = -1
    if col > 0:
        mapper[row, col-1] = -1
    if row < mapper.shape[0]:
        mapper[row+1,col] = -1
    if col < mapper.shape[1]:
        mapper[row,col+1] = -1
Yuca
  • 6,010
  • 3
  • 22
  • 42
  • I'm getting awful results, since you have a random setup, but this is technically what you need. Next time you post a question provide the desired output, otherwise figuring out the answer is harder than needed :) – Yuca Oct 01 '18 at 20:11
  • Thank you for answering my question and for your suggestion, I wil put the required output in the question next time. Your code doesn't work when I run it but I will study it to find what I need to adjust. – Laura Oct 02 '18 at 06:49
  • well that code runs as is. So not sure what you mean – Yuca Oct 02 '18 at 11:45