Python 多维切片的紧凑表示法
假设我有3(或更多)维数组Python 多维切片的紧凑表示法,python,numpy,slice,Python,Numpy,Slice,假设我有3(或更多)维数组A和两个数组,它们具有最小和最大边左,右,用于切片 是否有比此更紧凑的符号来选择由“我的边”分隔的视图 V = A[left[0]:right[0], left[1]:right[1], left[2]:right[2]) 也许这样已经更好了 view = [ slice(a, b) for a,b in zip(left, right) ] V = A[view] 但我觉得有一种更为自然的方式我看不到。。。不知道,用np.s.的东西 编辑:预期结果的示例 A =
A
和两个数组,它们具有最小和最大边左
,右
,用于切片
是否有比此更紧凑的符号来选择由“我的边”分隔的视图
V = A[left[0]:right[0], left[1]:right[1], left[2]:right[2])
也许这样已经更好了
view = [ slice(a, b) for a,b in zip(left, right) ]
V = A[view]
但我觉得有一种更为自然的方式我看不到。。。不知道,用np.s.
的东西
编辑:预期结果的示例
A = np.arange(1000).reshape(10,10,10)
left = np.array([0, 0, 0])
right = np.array([2, 4, 3])
view = [ slice(a, b) for a,b in zip(left, right) ]
In [32]: A[view]
Out[32]:
array([[[ 0, 1, 2],
[ 10, 11, 12],
[ 20, 21, 22],
[ 30, 31, 32]],
[[100, 101, 102],
[110, 111, 112],
[120, 121, 122],
[130, 131, 132]]])
您可以子类化
np.s\uu
以支持“矢量化”切片表示法:
import numpy as np
import types
class Block_Slice(np.lib.index_tricks.IndexExpression):
@staticmethod
def expand_slice(slice_, result_type=types.GeneratorType, force_type=False):
bc = np.broadcast(slice_.start, slice_.stop, slice_.step)
if bc.size == 1 and not force_type:
return slice_
if result_type in ('generator', types.GeneratorType):
return (slice(*idx) for idx in bc)
result_type = tuple if result_type=='tuple' else result_type
return result_type(slice(*idx) for idx in bc)
def __getitem__(self, item):
if isinstance(item, slice):
item = self.expand_slice(item, tuple)
elif isinstance(item, tuple):
item = tuple(j for i in item for j in
(self.expand_slice(i, force_type=True)
if isinstance(i, slice) else (i,)))
return super().__getitem__(item)
bs_ = Block_Slice(maketuple=False)
a = sum(np.ogrid[:2000:1000, :300:100, :40:10, :6])
left = [1, 0, 0]
right = [2, 2, 4]
a[bs_[left:right]]
# array([[[[1000, 1001, 1002, 1003, 1004, 1005],
# [1010, 1011, 1012, 1013, 1014, 1015],
# [1020, 1021, 1022, 1023, 1024, 1025],
# [1030, 1031, 1032, 1033, 1034, 1035]],
# [[1100, 1101, 1102, 1103, 1104, 1105],
# [1110, 1111, 1112, 1113, 1114, 1115],
# [1120, 1121, 1122, 1123, 1124, 1125],
# [1130, 1131, 1132, 1133, 1134, 1135]]]])
a[bs_[..., range(3):5]]
# array([[[[ 12, 13, 14],
# [ 22, 23, 24],
# [ 32, 33, 34]],
# [[ 112, 113, 114],
# [ 122, 123, 124],
# [ 132, 133, 134]],
# [[ 212, 213, 214],
# [ 222, 223, 224],
# [ 232, 233, 234]]],
# [[[1012, 1013, 1014],
# [1022, 1023, 1024],
# [1032, 1033, 1034]],
# [[1112, 1113, 1114],
# [1122, 1123, 1124],
# [1132, 1133, 1134]],
# [[1212, 1213, 1214],
# [1222, 1223, 1224],
# [1232, 1233, 1234]]]])
a[bs_[:right, [4, 2, 1]]]
# array([[[[ 4, 2, 1],
# [ 14, 12, 11],
# [ 24, 22, 21],
# [ 34, 32, 31]],
# [[ 104, 102, 101],
# [ 114, 112, 111],
# [ 124, 122, 121],
# [ 134, 132, 131]]],
# [[[1004, 1002, 1001],
# [1014, 1012, 1011],
# [1024, 1022, 1021],
# [1034, 1032, 1031]],
# [[1104, 1102, 1101],
# [1114, 1112, 1111],
# [1124, 1122, 1121],
# [1134, 1132, 1131]]]])
您有示例数组和预期的输出吗?当然,请参阅编辑
np.s
,它只允许您从第一个表达式np.s_[left[0]:right[0],…]生成切片的元组。不管怎样,您都需要生成切片的元组。numpythonic方法只是一个Python编码任务。如果你想让它变得漂亮,把它包装在一个函数(或类)中。也许你是对的,它已经是这样了。。。在编写python/numpy代码时,我总是觉得自己比我应该/能够做的更冗长,而且我缺少一些使事情变得优雅的符号技巧A[…]
是A的缩写。uu getitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuatuple
有三个slice
对象。每个slice
都有3个属性。我不知道下一票是什么,我喜欢它,可能对我的需要有点过分,但它在我的生活中可能会很有用toolbox@filippo仅供参考,我已经发布了一个正确的版本。