我试图使用以下功能:
def randomChose(bp, xsteps, ysteps, bs):
# Number of points to be chosen
s = int((bp * xsteps * ysteps) / (bs * bs))
# Generating an array representing the input indexes
indices = numpy.arange(xsteps * ysteps)
# Resampling without replacement
cs = npr.choice(indices, size=s, replace=False)
f = []
for idx in cs:
nb = indices[max(idx-(bs*bs/2), 0):min(idx+(bs*bs/2)+1, xsteps*ysteps)]
f.append(nb)
f = numpy.array(f).flatten()
fix = numpy.unique(numpy.array(f))
return fix
其中参数为数字bp,数据维度为xsteps * ysteps且为%bs。
我想要做的是选择一些有效的索引来考虑此图像中的某个邻域。
但是,我在调用numpy.unique
时仍然收到错误,但并非总是如此:
ValueError Traceback (most recent call last)
<ipython-input-35-1b5914c3cbc7> in <module>()
9 svf_y = []
10 for s in range(samples):
---> 11 fix = randomChose(bp, xsteps, ysteps, bs)
12 rs_z0, rs_z1, rs_z2 = interpolate(len(fix), xsteps, ysteps, mean_rs)
13 ds_z0, ds_z1, ds_z2 = interpolate(len(fix), xsteps, ysteps, mean_ds)
<ipython-input-6-def08adce84b> in randomChose(bp, xsteps, ysteps, bs)
14 f.append(nb)
15 f = numpy.array(f).flatten()
---> 16 fix = numpy.unique(numpy.array(f))
17
18 return f
/usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.pyc in unique(ar, return_index, return_inverse, return_counts)
198 ar.sort()
199 aux = ar
--> 200 flag = np.concatenate(([True], aux[1:] != aux[:-1]))
201
202 if not optional_returns:
ValueError: all the input arrays must have same number of dimensions
这就是我所说的:
nx = 57.2
ny = 24.0
xsteps = 144
ysteps = 106
bs = 5 # Block size
bp = 0.1 # Percentage of blocks
fix = randomChose(bp, xsteps, ysteps, bs)
我试图了解什么是错的。据我所知,这种方法期望输入ndarray
作为输入。
感谢您的帮助。
答案 0 :(得分:2)
首先:
f.append(nb)
应该成为:
f.append(list(nb))
这使得f列表中的列表,Numpy将有机会转换为Numpy整数数组,但是只有所有列表具有相同的长度。如果没有,你将只有一个Numpy列表的一维,而flatten()将没有效果。
您可以添加
print(type(f[0]))
展平后。
答案 1 :(得分:0)
问题在于边缘。例如,如果idx=0
,
nb = indices[max(idx-(bs*bs/2), 0):min(idx+(bs*bs/2)+1, xsteps*ysteps)]
将是[0]
- 即只有一个值而不是xy坐标。那么你将无法正确展平阵列。