Getting error: ValueError: cannot convert float NaN to integer
Getting error: ValueError: cannot convert float NaN to integer
我正在尝试想象从左上角到右下角到达的路径。我已经计算更新了每个单元格的成本。现在尝试可视化路径。
即
[[ 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就是上面的矩阵。从右下角开始,我想检查邻居是否小或相等,然后 x,y 移动到那个位置。并将该值更改为 Nan,以便它在 matplotlib 的可视化中显示为糟糕。
但它抛出“ValueError:无法将 float NaN 转换为整数”。
有人可以请教如何解决这个问题吗?
```
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)])
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)])
```
map[x][y - 1]
的坐标就是(x, y - 1)
.
if-else 应为:
if map[x][y-1] <= map[x][y]:
x, y = x, y - 1
else:
x, y = x - 1, y
我正在尝试想象从左上角到右下角到达的路径。我已经计算更新了每个单元格的成本。现在尝试可视化路径。
即
[[ 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就是上面的矩阵。从右下角开始,我想检查邻居是否小或相等,然后 x,y 移动到那个位置。并将该值更改为 Nan,以便它在 matplotlib 的可视化中显示为糟糕。
但它抛出“ValueError:无法将 float NaN 转换为整数”。
有人可以请教如何解决这个问题吗?
```
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)])
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)])
```
map[x][y - 1]
的坐标就是(x, y - 1)
.
if-else 应为:
if map[x][y-1] <= map[x][y]:
x, y = x, y - 1
else:
x, y = x - 1, y