scipy.ndimage.labeled_comprehension¶
- scipy.ndimage.labeled_comprehension(input, labels, index, func, out_dtype, default, pass_positions=False)[源代码]¶
大致相当于 [func(input[labels == i] )用于索引中的i]。
按顺序将任意函数(适用于ARRAY_LIKE输入)应用于由指定的N维图像数组的子集 labels 和 index 。该选项用于为函数提供位置参数作为第二个参数。
- 参数
- inputarray_like
要从中选择的数据 labels 去处理。
- labelsARRAY_LIKE或NONE
将标签添加到以下位置中的对象 input 。如果不是无,则数组的形状必须与 input 。如果没有, func 应用于拆分的 input 。
- indexINT、INT序列或无
的子集 labels 要应用的对象 func 。如果是标量,则返回单个值。如果没有, func 的所有非零值。 labels 。
- func可调用
要应用到的Python函数 labels 从… input 。
- out_dtype数据类型
要用于的数据类型 result 。
- default整型、浮点型或无
的元素时的默认返回值 index 不存在于 labels 。
- pass_positions布尔值,可选
如果为True,则将线性索引传递给 func 作为第二个论点。默认值为False。
- 退货
- resultndarray
申请结果 func 发送到每个 labels 至 input 在……里面 index 。
示例
>>> a = np.array([[1, 2, 0, 0], ... [5, 3, 0, 4], ... [0, 0, 0, 7], ... [9, 3, 0, 0]]) >>> from scipy import ndimage >>> lbl, nlbl = ndimage.label(a) >>> lbls = np.arange(1, nlbl+1) >>> ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, 0) array([ 2.75, 5.5 , 6. ])
回落到 default :
>>> lbls = np.arange(1, nlbl+2) >>> ndimage.labeled_comprehension(a, lbl, lbls, np.mean, float, -1) array([ 2.75, 5.5 , 6. , -1. ])
传球位置:
>>> def fn(val, pos): ... print("fn says: %s : %s" % (val, pos)) ... return (val.sum()) if (pos.sum() % 2 == 0) else (-val.sum()) ... >>> ndimage.labeled_comprehension(a, lbl, lbls, fn, float, 0, True) fn says: [1 2 5 3] : [0 1 4 5] fn says: [4 7] : [ 7 11] fn says: [9 3] : [12 13] array([ 11., 11., -12., 0.])