简体   繁体   中英

Getting error: ValueError: cannot convert float NaN to integer

I am trying to visualise the path it took to reach from top left to bottom right. I have calculated updated the cost of each cells.. now trying to visualise the path.

ie

[[ 4  4  7 10 13]
 [ 5  7  9 13 13]
 [ 5  9 11 12 12]
 [ 6  7  7  8 12]
 [ 9  7 10  8 10]]

map is the above matrix. Starting from bottom right, I want to check if neighbours is small or equal, then x,y moves to that position. And changing the value to Nan so that it will come up as bad in the visualisation on matplotlib.

But it is throwing "ValueError: cannot convert float NaN to integer".

Can someone please advice on how to solve this?

```

origingrid=np.ones((row,col),dtype=int)*np.nan

#Start backtracking to plot the path  
temp=map.astype(float)
x,y=row-1,col-1
path=[]
#set the lower right cell to be nan.
temp[np.int(x),np.int(y)]=np.nan

while x>0.0 or y>0.0:
  path.append([np.int(x),np.int(y)])
  #xy=np.unravel_index(np.int(map[np.int(x),np.int(y)]), (row,col))
  if map[x][y-1] <= map[x][y]:
    origingrid[x,y-1] = np.ravel_multi_index([x,y], (row,col)) 
    xy=np.unravel_index(np.int(origingrid[np.int(x),np.int(y)]), (row,col)) #<--ValueError: cannot convert float NaN to integer
    x,y = xy[0],xy[1]
  else: 
    origingrid[x-1,y] = np.ravel_multi_index([x,y], (row,col)) 
    xy=np.unravel_index(np.int(origingrid[np.int(x),np.int(y)]), (row,col))
    x,y = xy[0],xy[1]

temp[np.int(x),np.int(y)]=np.nan
path.append([np.int(x),np.int(y)])
```

The coordinates of map[x][y - 1] is simply (x, y - 1) .

The if-else should read:

  if map[x][y-1] <= map[x][y]:
        x, y = x, y - 1
  else: 
    x, y = x - 1, y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM