python随机抽取数组⾥的数_在python中从2D数组中随机抽取
⼦数组
这是⼀个采样器,它从任何维度的数组中创建⼀个样本切割。它使⽤函数来控制从何处开始切割以及切割沿任何轴的宽度。在
以下是参数说明:arr-输⼊numpy数组。在
loc_sampler_fn-这是您想要⽤来设置框⾓的函数。如果要从沿轴的任意位置均匀地采样长⽅体的⾓,请使⽤np.random.uniform。如果希望⾓点更接近数组的中⼼,请使⽤al。但是,我们需要告诉函数采样的范围。这就引出了下⼀个参数。在
loc_dim_param-这将把每个轴的⼤⼩传递给loc_sampler_fn。如果我们使⽤np.random.uniform作为位置采样器,我们希望从轴的整个范围进⾏采样。np.random.uniform有两个参数:low和high,因此通过将轴的长度传递给high,它在整个轴上均匀地采样。换句话说,如果轴的长度是120,我们想要np.random.uniform(low=0, high=120),那么我们将设置loc_dim_param='high'。在
loc_params-这会将任何附加参数传递给loc_sampler_fn。与⽰例保持⼀致,我们需要将low=0传递给np.random.uniform,因此我们传递字典loc_params={'low':0}。在
从这⾥看,盒⼦的形状基本相同。如果希望将框的⾼度和宽度从3均匀地采样到10,请传⼊shape_sampler_fn=np.random.uniform,并使⽤shape_dim_param=None,因为我们没有将轴的⼤⼩⽤于任何东西,并且shape_params={'low':3, 'high':11}。在def
box_sampler(arr,
loc_sampler_fn,
loc_dim_param,
loc_params,
shape_sampler_fn,
shape_dim_param,
shape_params):
'''
Extracts a sample cut from `arr`.
Parameters:
-
loc_sampler_fn : function
The function to determine the where the minimum coordinate
for each axis should be placed.
loc_dim_param : string or None
The parameter in `loc_sampler_fn` that should use the axes
dimension size
loc_params : dict
Parameters to pass to `loc_sampler_fn`.
shape_sampler_fn : function
The function to determine the width of the sample cut
python获取数组长度along each axis.
shape_dim_param : string or None
The parameter in `shape_sampler_fn` that should use the
axes dimension size.
shape_params : dict
Parameters to pass to `shape_sampler_fn`.
Returns:
(slices, x) : A tuple of the slices used to cut the sample as well as
the sampled subsection with the same dimensionality of arr.
slice :: list of slice objects
x :: array object with the same ndims as arr
'''
slices = []
for dim in arr.shape:
if loc_dim_param:
loc_params.update({loc_dim_param: dim})
if shape_dim_param:
shape_params.update({shape_dim_param: dim})
start = int(loc_sampler_fn(**loc_params))
stop = start + int(shape_sampler_fn(**shape_params))
slices.append(slice(start, stop))
return slices, arr[slices]
宽度在3到9之间的⼆维阵列上的均匀切割⽰例:
^{pr2}$
从10x20x30 3D数组中获取2x2x2块的⽰例:a = np.random.randint(0,2,size=(10,20,30))
box_sampler(a, np.random.uniform, 'high', {'low':0},
np.random.uniform, None, {'low':2, 'high':2})
# returns:
([slice(7, 9, None), slice(9, 11, None), slice(19, 21, None)],
array([[[0, 1],
[1, 0]],
[[0, 1],
[1, 1]]]))
根据评论进⾏更新。
对于您的特定⽤途,似乎需要⼀个矩形采样,其中起始⾓点从阵列中的任何位置均匀采样,沿每个轴的采样宽度是均匀采样的,但可以限制采样宽度。在
下⾯是⼀个⽣成这些样本的函数。min_width和max_width可以接受整数的iterable(例如元组)或单个整数。在def uniform_box_sampler(arr, min_width, max_width):
'''
Extracts a sample cut from `arr`.
Parameters:
-
arr : array
The numpy array to sample a box from
min_width : int or tuple
The minimum width of the box along a given axis.
If a tuple of integers is supplied, it my have the
same length as the number of dimensions of `arr`
max_width : int or tuple
The maximum width of the box along a given axis.
If a tuple of integers is supplied, it my have the
same length as the number of dimensions of `arr`
Returns:
(slices, x) : A tuple of the slices used to cut the sample as well as
the sampled subsection with the same dimensionality of arr.
slice :: list of slice objects
x :: array object with the same ndims as arr
'''
if isinstance(min_width, (tuple, list)):
assert len(min_width)==arr.ndim, 'Dimensions of `min_width` and `arr` must match'
else:
min_width = (min_width,)*arr.ndim
if isinstance(max_width, (tuple, list)):
assert len(max_width)==arr.ndim, 'Dimensions of `max_width` and `arr` must match'
else:
max_width = (max_width,)*arr.ndim
slices = []
for dim, mn, mx in zip(arr.shape, min_width, max_width):
fn = np.random.uniform
start = int(np.random.uniform(0,dim))
stop = start + int(np.random.uniform(mn, mx+1))
slices.append(slice(start, stop))
return slices, arr[slices]
例如,在数组中的任意位置⽣成均匀开始的长⽅体切割,⾼度是从1到4的随机均匀绘制,宽度是从2到6的随机均匀绘制(只是为了显⽰)。在本例中,框的⼤⼩为3×4,从第66⾏和第19列开始。在x = np.random.randint(0,2,size=(100,100))
uniform_box_sampler(x, (1,2), (4,6))
# returns:
([slice(65, 68, None), slice(18, 22, None)],
array([[1, 0, 0, 0],
[0, 0, 1, 1],
[0, 1, 1, 0]]))