当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.ravel_multi_index()用法及代码示例


numpy.ravel_multi_index()函数将边界数组的元组转换为平面索引数组,并将边界模式应用于multi-index。

用法: numpy.ravel_multi_index(multi_index, dims, mode = ‘raise’, order = ‘C)
参数:
multi_index :[tuple of array_like] A tuple of integer arrays, one array for each dimension.
dims :[tuple of ints] The shape of array into which the indices from multi_index apply.
mode :[{‘raise’, ‘wrap’, ‘clip’}, optional] Specifies how out-of-bounds indices are handled. Can specify either one mode or a tuple of modes, one mode per index.
‘raise’ - raise an error (default)
‘wrap’ - wrap around
‘clip’ - clip to the range
In ‘clip’ mode, a negative index that would normally wrap will clip to 0 instead.
order :[{‘C’, ‘F’}, optional] Determines whether the multi-index should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order.

返回:[ndarray]索引数组,这些维度数组是维数组的扁平化版本。

代码1:

# Python program explaining 
# numpy.ravel_multi_index() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.array([[3, 6, 6], [4, 5, 1]]) 
  
gfg = geek.ravel_multi_index(arr, (7, 6)) 
  
print(gfg) 

输出:



[22 41 37]


代码2:

# Python program explaining 
# numpy.ravel_multi_index() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.array([[3, 6, 6], [4, 5, 1]]) 
  
gfg = geek.ravel_multi_index(arr, (7, 6), order = 'F') 
  
print(gfg) 

输出:

[31 41 13]


代码3:

# Python program explaining 
# numpy.ravel_multi_index() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.array([[3, 6, 6], [4, 5, 1]]) 
  
gfg = geek.ravel_multi_index(arr, (7, 6), mode = 'clip') 
  
print(gfg) 

输出:

[22 41 37]



相关用法


注:本文由纯净天空筛选整理自sanjoy_62大神的英文原创作品 numpy.ravel_multi_index() function | Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。